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-alpha'
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
) :
29 #logfile = os.path.join(logsdir, datetime.datetime.now().strftime('%Y-%m-%d-%H_%M_%S.log'))
30 #logfile = open(logfile, 'w')
33 class MinWiiStreamHandler(logging
.StreamHandler
) :
39 logging
.Handler
.__init
__(self
)
40 if self
.stream
is None :
44 def setNewStream(cls
) :
45 if cls
.stream
and not cls
.stream
.closed
:
47 logfile
= os
.path
.join(logsdir
, datetime
.datetime
.now().strftime('%Y-%m-%d-%H_%M_%S.log'))
48 cls
.stream
= open(logfile
, 'a')
50 def emit(self
, record
) :
51 if record
.name
== 'minwii.env' :
52 if record
.getMessage()=='NEW_LOG_FILE' :
55 logging
.StreamHandler
.emit(self
, rec
)
58 self
.env
.append(record
)
59 logging
.StreamHandler
.emit(self
, record
)
61 class EventLoggerAdapter(logging
.LoggerAdapter
) :
63 FORMAT
= "EVT %(ticks)-10s %(eventName)-15s %(message)s"
65 def process(self
, msg
, kwargs
) :
66 pp
= pprinters
.get(msg
.type, lambda e
: str(e
))
68 extra
['eventType'] = msg
.type
69 extra
['eventName'] = eventNames
[msg
.type]
70 extra
['ticks'] = pygame
.time
.get_ticks()
72 return pp(msg
), kwargs
79 def filter(self
, record
) :
80 return not hiddenEvents
.has_key(record
.eventType
)
83 def initAppConsole() :
85 console
= logging
.getLogger('minwii.console')
86 console
.setLevel(logging
.DEBUG
)
87 handler
= MinWiiStreamHandler()
88 handler
.setFormatter(logging
.Formatter('APP %(message)s'))
89 console
.addHandler(handler
)
93 envLogger
= logging
.getLogger('minwii.env')
94 envLogger
.setLevel(logging
.DEBUG
)
95 handler
= MinWiiStreamHandler()
96 handler
.setFormatter(logging
.Formatter('ENV %(message)s'))
97 envLogger
.addHandler(handler
)
100 def initEventLogger():
101 _eventLogger
= logging
.getLogger('minwii.events')
102 _eventLogger
.setLevel(logging
.INFO
)
103 _eventHandler
= MinWiiStreamHandler()
104 _eventFormatter
= logging
.Formatter(EventLoggerAdapter
.FORMAT
)
105 _eventHandler
.setFormatter(_eventFormatter
)
106 _eventLogger
.addHandler(_eventHandler
)
107 _eventLogger
.addFilter(EventFilter())
109 eventLogger
= EventLoggerAdapter(_eventLogger
, {})