Affichage du HID dans l'application d'analyse des logs.
[minwii.git] / src / minwii / 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 COLOVER, eventNames
10 from event_pprinters import pprinters
11 import sys
12 import pygame
13 import logging
14 import os
15 import datetime
16 from config import LOGS_DIR as logsdir
17 LOG_FORMAT_VERSION='1.0'
18
19 hiddenEvents = {pygame.MOUSEMOTION:True,
20 pygame.MOUSEBUTTONDOWN:True,
21 pygame.MOUSEBUTTONUP:True,
22 #COLOVER:True,
23 }
24
25 if not os.path.exists(logsdir) :
26 os.mkdir(logsdir)
27
28 if os.environ.has_key('WINWII_NO_LOG') :
29 class MinWiiStreamHandler(logging.Handler) :
30 def emit(record) : pass
31 else :
32 class MinWiiStreamHandler(logging.StreamHandler) :
33 logfile = None
34 stream = None
35 env = []
36
37 def __init__(self) :
38 logging.Handler.__init__(self)
39 if self.stream is None :
40 self.setNewStream()
41
42 @classmethod
43 def setNewStream(cls) :
44 if cls.stream and not cls.stream.closed :
45 cls.stream.close()
46 logfile = os.path.join(logsdir, datetime.datetime.now().strftime('%Y-%m-%d-%H_%M_%S.log'))
47 cls.stream = open(logfile, 'a')
48
49 def emit(self, record) :
50 if record.name == 'minwii.env' :
51 if record.getMessage()=='NEW_LOG_FILE' :
52 self.setNewStream()
53 for rec in self.env :
54 logging.StreamHandler.emit(self, rec)
55 return
56 else :
57 self.env.append(record)
58 logging.StreamHandler.emit(self, record)
59
60 class EventLoggerAdapter(logging.LoggerAdapter) :
61
62 FORMAT = "EVT %(ticks)-10s %(eventName)-15s %(message)s"
63
64 def process(self, msg, kwargs) :
65 pp = pprinters.get(msg.type, lambda e: str(e))
66 extra = {}
67 extra['eventType'] = msg.type
68 extra['eventName'] = eventNames[msg.type]
69 extra['ticks'] = pygame.time.get_ticks()
70 kwargs['extra']=extra
71 return pp(msg), kwargs
72
73 class EventFilter :
74
75 def __init__(self):
76 pass
77
78 def filter(self, record) :
79 return not hiddenEvents.has_key(record.eventType)
80
81
82 def initAppConsole() :
83 global console
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)
89
90 def initEnvLogger() :
91 global envLogger
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)
97
98
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())
107 global eventLogger
108 eventLogger = EventLoggerAdapter(_eventLogger, {})
109
110 def initLoggers() :
111 initAppConsole()
112 initEnvLogger()
113 initEventLogger()
114
115 initLoggers()