plus de décalage entre exécution des noteon/off et leur log.
[minwii.git] / src / app / log.py
1 # -*- coding: utf-8 -*-
2 """
3 utilitaires pour tracer l'activité de miiwii
4
5 $Id$
6 $URL$
7 """
8
9 from events import TIMEOUT, COLOVER, eventNames
10 from event_pprinters import pprinters
11 import sys
12 import pygame
13 import logging
14 import os
15 import datetime
16 LOG_FORMAT_VERSION='1.0-alpha'
17
18 hiddenEvents = {pygame.MOUSEMOTION:True,
19 pygame.MOUSEBUTTONDOWN:True,
20 pygame.MOUSEBUTTONUP:True,
21 TIMEOUT:True,
22 #COLOVER:True,
23 }
24
25 logsdir = os.path.join(os.path.expanduser('~'), 'minwii_logs')
26 if not os.path.exists(logsdir) :
27 os.mkdir(logsdir)
28
29 #logfile = os.path.join(logsdir, datetime.datetime.now().strftime('%Y-%m-%d-%H_%M_%S.log'))
30 #logfile = open(logfile, 'w')
31
32
33 class MinWiiStreamHandler(logging.StreamHandler) :
34 logfile = None
35 stream = None
36 env = []
37
38 def __init__(self) :
39 logging.Handler.__init__(self)
40 if self.stream is None :
41 self.setNewStream()
42
43 @classmethod
44 def setNewStream(cls) :
45 if cls.stream and not cls.stream.closed :
46 cls.stream.close()
47 logfile = os.path.join(logsdir, datetime.datetime.now().strftime('%Y-%m-%d-%H_%M_%S.log'))
48 cls.stream = open(logfile, 'a')
49
50 def emit(self, record) :
51 if record.name == 'minwii.env' :
52 if record.getMessage()=='NEW_LOG_FILE' :
53 self.setNewStream()
54 for rec in self.env :
55 logging.StreamHandler.emit(self, rec)
56 return
57 else :
58 self.env.append(record)
59 logging.StreamHandler.emit(self, record)
60
61 class EventLoggerAdapter(logging.LoggerAdapter) :
62
63 FORMAT = "EVT %(ticks)-10s %(eventName)-15s %(message)s"
64
65 def process(self, msg, kwargs) :
66 pp = pprinters.get(msg.type, lambda e: str(e))
67 extra = {}
68 extra['eventType'] = msg.type
69 extra['eventName'] = eventNames[msg.type]
70 extra['ticks'] = pygame.time.get_ticks()
71 kwargs['extra']=extra
72 return pp(msg), kwargs
73
74 class EventFilter :
75
76 def __init__(self):
77 pass
78
79 def filter(self, record) :
80 return not hiddenEvents.has_key(record.eventType)
81
82
83 def initAppConsole() :
84 global console
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)
90
91 def initEnvLogger() :
92 global envLogger
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)
98
99
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())
108 global eventLogger
109 eventLogger = EventLoggerAdapter(_eventLogger, {})
110
111 def initLoggers() :
112 initAppConsole()
113 initEnvLogger()
114 initEventLogger()
115
116 initLoggers()