1 # -*- coding: utf-8 -*-
3 utilitaires pour tracer l'activité de miiwii
9 from events
import TIMEOUT
, COLOVER
, eventNames
10 from event_pprinters
import pprinters
16 LOG_FORMAT_VERSION
='1.0'
18 hiddenEvents
= {pygame
.MOUSEMOTION
:True,
19 pygame
.MOUSEBUTTONDOWN
:True,
20 pygame
.MOUSEBUTTONUP
:True,
25 logsdir
= os
.path
.join(os
.path
.expanduser('~'), 'minwii_logs')
26 if not os
.path
.exists(logsdir
) :
30 class MinWiiStreamHandler(logging
.StreamHandler
) :
36 logging
.Handler
.__init
__(self
)
37 if self
.stream
is None :
41 def setNewStream(cls
) :
42 if cls
.stream
and not cls
.stream
.closed
:
44 logfile
= os
.path
.join(logsdir
, datetime
.datetime
.now().strftime('%Y-%m-%d-%H_%M_%S.log'))
45 cls
.stream
= open(logfile
, 'a')
47 def emit(self
, record
) :
48 if record
.name
== 'minwii.env' :
49 if record
.getMessage()=='NEW_LOG_FILE' :
52 logging
.StreamHandler
.emit(self
, rec
)
55 self
.env
.append(record
)
56 logging
.StreamHandler
.emit(self
, record
)
58 class EventLoggerAdapter(logging
.LoggerAdapter
) :
60 FORMAT
= "EVT %(ticks)-10s %(eventName)-15s %(message)s"
62 def process(self
, msg
, kwargs
) :
63 pp
= pprinters
.get(msg
.type, lambda e
: str(e
))
65 extra
['eventType'] = msg
.type
66 extra
['eventName'] = eventNames
[msg
.type]
67 extra
['ticks'] = pygame
.time
.get_ticks()
69 return pp(msg
), kwargs
76 def filter(self
, record
) :
77 return not hiddenEvents
.has_key(record
.eventType
)
80 def initAppConsole() :
82 console
= logging
.getLogger('minwii.console')
83 console
.setLevel(logging
.DEBUG
)
84 handler
= MinWiiStreamHandler()
85 handler
.setFormatter(logging
.Formatter('APP %(message)s'))
86 console
.addHandler(handler
)
90 envLogger
= logging
.getLogger('minwii.env')
91 envLogger
.setLevel(logging
.DEBUG
)
92 handler
= MinWiiStreamHandler()
93 handler
.setFormatter(logging
.Formatter('ENV %(message)s'))
94 envLogger
.addHandler(handler
)
97 def initEventLogger():
98 _eventLogger
= logging
.getLogger('minwii.events')
99 _eventLogger
.setLevel(logging
.INFO
)
100 _eventHandler
= MinWiiStreamHandler()
101 _eventFormatter
= logging
.Formatter(EventLoggerAdapter
.FORMAT
)
102 _eventHandler
.setFormatter(_eventFormatter
)
103 _eventLogger
.addHandler(_eventHandler
)
104 _eventLogger
.addFilter(EventFilter())
106 eventLogger
= EventLoggerAdapter(_eventLogger
, {})