f07e3d21dc62813bbc1ce1b2e6fd26bed8a5b46f
[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
15 hiddenEvents = {pygame.MOUSEMOTION:True,
16 pygame.MOUSEBUTTONDOWN:True,
17 pygame.MOUSEBUTTONUP:True,
18 TIMEOUT:True,
19 #COLOVER:True,
20 }
21
22
23 class EventLoggerAdapter(logging.LoggerAdapter) :
24
25 FORMAT = "%(ticks)-10s %(eventName)-15s %(message)s"
26
27 def process(self, msg, kwargs) :
28 pp = pprinters.get(msg.type, lambda e: str(e))
29 extra = {}
30 extra['eventType'] = msg.type
31 extra['eventName'] = eventNames[msg.type]
32 extra['ticks'] = pygame.time.get_ticks()
33 kwargs['extra']=extra
34 return pp(msg), kwargs
35
36 class EventFilter :
37
38 def __init__(self):
39 pass
40
41 def filter(self, record) :
42 return not hiddenEvents.has_key(record.eventType)
43
44 #logging.basicConfig(level=logging.DEBUG, stream = sys.stdout)
45 console = logging.getLogger('minwii.console')
46 console.setLevel(logging.DEBUG)
47 console.addHandler(logging.StreamHandler())
48
49 _eventLogger = logging.getLogger('minwii.events')
50 _eventLogger.setLevel(logging.INFO)
51 _eventHandler = logging.StreamHandler()
52 _eventFormatter = logging.Formatter(EventLoggerAdapter.FORMAT)
53 _eventHandler.setFormatter(_eventFormatter)
54 _eventLogger.addHandler(_eventHandler)
55 _eventLogger.addFilter(EventFilter())
56
57 eventLogger = EventLoggerAdapter(_eventLogger, {})
58 #eventLogger = logging.getLogger('minwii.events')