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