1 # -*- coding: utf-8 -*-
3 utilitaires pour tracer l'activité de miiwii
9 from events
import COLOVER
, eventNames
10 from event_pprinters
import pprinters
16 from config
import LOGS_DIR
as logsdir
17 LOG_FORMAT_VERSION
='1.0'
19 hiddenEvents
= {pygame
.MOUSEMOTION
:True,
20 pygame
.MOUSEBUTTONDOWN
:True,
21 pygame
.MOUSEBUTTONUP
:True,
25 if not os
.path
.exists(logsdir
) :
28 if os
.environ
.has_key('WINWII_NO_LOG') :
29 class MinWiiStreamHandler(logging
.Handler
) :
30 def emit(record
) : pass
32 class MinWiiStreamHandler(logging
.StreamHandler
) :
38 logging
.Handler
.__init
__(self
)
39 if self
.stream
is None :
43 def setNewStream(cls
) :
44 if cls
.stream
and not cls
.stream
.closed
:
46 logfile
= os
.path
.join(logsdir
, datetime
.datetime
.now().strftime('%Y-%m-%d-%H_%M_%S.log'))
47 cls
.stream
= open(logfile
, 'a')
49 def emit(self
, record
) :
50 if record
.name
== 'minwii.env' :
51 if record
.getMessage()=='NEW_LOG_FILE' :
54 logging
.StreamHandler
.emit(self
, rec
)
57 self
.env
.append(record
)
58 logging
.StreamHandler
.emit(self
, record
)
60 class EventLoggerAdapter(logging
.LoggerAdapter
) :
62 FORMAT
= "EVT %(ticks)-10s %(eventName)-15s %(message)s"
64 def process(self
, msg
, kwargs
) :
65 pp
= pprinters
.get(msg
.type, lambda e
: str(e
))
67 extra
['eventType'] = msg
.type
68 extra
['eventName'] = eventNames
[msg
.type]
69 extra
['ticks'] = pygame
.time
.get_ticks()
71 return pp(msg
), kwargs
78 def filter(self
, record
) :
79 return not hiddenEvents
.has_key(record
.eventType
)
82 def initAppConsole() :
84 console
= logging
.getLogger('minwii.console')
85 console
.setLevel(logging
.DEBUG
)
86 handler
= MinWiiStreamHandler()
87 handler
.setFormatter(logging
.Formatter('APP %(message)s'))
88 console
.addHandler(handler
)
92 envLogger
= logging
.getLogger('minwii.env')
93 envLogger
.setLevel(logging
.DEBUG
)
94 handler
= MinWiiStreamHandler()
95 handler
.setFormatter(logging
.Formatter('ENV %(message)s'))
96 envLogger
.addHandler(handler
)
99 def initEventLogger():
100 _eventLogger
= logging
.getLogger('minwii.events')
101 _eventLogger
.setLevel(logging
.INFO
)
102 _eventHandler
= MinWiiStreamHandler()
103 _eventFormatter
= logging
.Formatter(EventLoggerAdapter
.FORMAT
)
104 _eventHandler
.setFormatter(_eventFormatter
)
105 _eventLogger
.addHandler(_eventHandler
)
106 _eventLogger
.addFilter(EventFilter())
108 eventLogger
= EventLoggerAdapter(_eventLogger
, {})