Les notes sont jouées. bugfix temps de d'attente.
[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
30 class MinWiiStreamHandler(logging.StreamHandler) :
31 logfile = None
32 stream = None
33 env = []
34
35 def __init__(self) :
36 logging.Handler.__init__(self)
37 if self.stream is None :
38 self.setNewStream()
39
40 @classmethod
41 def setNewStream(cls) :
42 if cls.stream and not cls.stream.closed :
43 cls.stream.close()
44 logfile = os.path.join(logsdir, datetime.datetime.now().strftime('%Y-%m-%d-%H_%M_%S.log'))
45 cls.stream = open(logfile, 'a')
46
47 def emit(self, record) :
48 if record.name == 'minwii.env' :
49 if record.getMessage()=='NEW_LOG_FILE' :
50 self.setNewStream()
51 for rec in self.env :
52 logging.StreamHandler.emit(self, rec)
53 return
54 else :
55 self.env.append(record)
56 logging.StreamHandler.emit(self, record)
57
58 class EventLoggerAdapter(logging.LoggerAdapter) :
59
60 FORMAT = "EVT %(ticks)-10s %(eventName)-15s %(message)s"
61
62 def process(self, msg, kwargs) :
63 pp = pprinters.get(msg.type, lambda e: str(e))
64 extra = {}
65 extra['eventType'] = msg.type
66 extra['eventName'] = eventNames[msg.type]
67 extra['ticks'] = pygame.time.get_ticks()
68 kwargs['extra']=extra
69 return pp(msg), kwargs
70
71 class EventFilter :
72
73 def __init__(self):
74 pass
75
76 def filter(self, record) :
77 return not hiddenEvents.has_key(record.eventType)
78
79
80 def initAppConsole() :
81 global console
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)
87
88 def initEnvLogger() :
89 global envLogger
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)
95
96
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())
105 global eventLogger
106 eventLogger = EventLoggerAdapter(_eventLogger, {})
107
108 def initLoggers() :
109 initAppConsole()
110 initEnvLogger()
111 initEventLogger()
112
113 initLoggers()