74500ec4524bc803306430eceacb9139ad61842a
[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
17 hiddenEvents = {pygame.MOUSEMOTION:True,
18 pygame.MOUSEBUTTONDOWN:True,
19 pygame.MOUSEBUTTONUP:True,
20 TIMEOUT:True,
21 #COLOVER:True,
22 }
23
24 class EventLoggerAdapter(logging.LoggerAdapter) :
25
26 FORMAT = "EVT %(ticks)-10s %(eventName)-15s %(message)s"
27
28 def process(self, msg, kwargs) :
29 pp = pprinters.get(msg.type, lambda e: str(e))
30 extra = {}
31 extra['eventType'] = msg.type
32 extra['eventName'] = eventNames[msg.type]
33 extra['ticks'] = pygame.time.get_ticks()
34 kwargs['extra']=extra
35 return pp(msg), kwargs
36
37 class EventFilter :
38
39 def __init__(self):
40 pass
41
42 def filter(self, record) :
43 return not hiddenEvents.has_key(record.eventType)
44
45 logsdir = os.path.join(os.path.expandvars('$HOME'), 'minwii_logs')
46 if not os.path.exists(logsdir) :
47 os.mkdir(logsdir)
48
49 logfile = os.path.join(logsdir, datetime.datetime.now().strftime('%Y-%m-%d-%H_%M_%S.log'))
50 logfile = open(logfile, 'w')
51
52 console = logging.getLogger('minwii.console')
53 console.setLevel(logging.DEBUG)
54 handler = logging.StreamHandler(logfile)
55 handler.setFormatter(logging.Formatter('APP %(message)s'))
56 console.addHandler(handler)
57
58 _eventLogger = logging.getLogger('minwii.events')
59 _eventLogger.setLevel(logging.INFO)
60 _eventHandler = logging.StreamHandler(logfile)
61 _eventFormatter = logging.Formatter(EventLoggerAdapter.FORMAT)
62 _eventHandler.setFormatter(_eventFormatter)
63 _eventLogger.addHandler(_eventHandler)
64 _eventLogger.addFilter(EventFilter())
65
66 eventLogger = EventLoggerAdapter(_eventLogger, {})