X-Git-Url: https://scm.cri.mines-paristech.fr/git/minwii.git/blobdiff_plain/e6ef7fb704667d8266e6d881ff5e39be0e4188fa..90331dd4caff60e9517c862101a37d1ae4ee00d1:/src/app/log.py diff --git a/src/app/log.py b/src/app/log.py index b2cd407..abbb858 100755 --- a/src/app/log.py +++ b/src/app/log.py @@ -6,38 +6,107 @@ $Id$ $URL$ """ -from events import TIMEOUT, COLOVER, eventNames +from events import COLOVER, eventNames from event_pprinters import pprinters import sys import pygame import logging +import os +import datetime +LOG_FORMAT_VERSION='1.0' + +hiddenEvents = {pygame.MOUSEMOTION:True, + pygame.MOUSEBUTTONDOWN:True, + pygame.MOUSEBUTTONUP:True, + #COLOVER:True, + } + +logsdir = os.path.join(os.path.expanduser('~'), 'minwii_logs') +if not os.path.exists(logsdir) : + os.mkdir(logsdir) +class MinWiiStreamHandler(logging.StreamHandler) : + logfile = None + stream = None + env = [] + + def __init__(self) : + logging.Handler.__init__(self) + if self.stream is None : + self.setNewStream() + + @classmethod + def setNewStream(cls) : + if cls.stream and not cls.stream.closed : + cls.stream.close() + logfile = os.path.join(logsdir, datetime.datetime.now().strftime('%Y-%m-%d-%H_%M_%S.log')) + cls.stream = open(logfile, 'a') + + def emit(self, record) : + if record.name == 'minwii.env' : + if record.getMessage()=='NEW_LOG_FILE' : + self.setNewStream() + for rec in self.env : + logging.StreamHandler.emit(self, rec) + return + else : + self.env.append(record) + logging.StreamHandler.emit(self, record) + class EventLoggerAdapter(logging.LoggerAdapter) : - FORMAT = "%(ticks)-15s %(eventName)s %(message)s" + FORMAT = "EVT %(ticks)-10s %(eventName)-15s %(message)s" def process(self, msg, kwargs) : - pp = pprinters.get(msg.type, lambda e: '') + pp = pprinters.get(msg.type, lambda e: str(e)) extra = {} + extra['eventType'] = msg.type extra['eventName'] = eventNames[msg.type] extra['ticks'] = pygame.time.get_ticks() kwargs['extra']=extra return pp(msg), kwargs -#logging.basicConfig(level=logging.DEBUG, stream = sys.stdout) +class EventFilter : + + def __init__(self): + pass + + def filter(self, record) : + return not hiddenEvents.has_key(record.eventType) -console = logging.getLogger('minwii.console') -_eventLogger = logging.getLogger('minwii.events') -_eventLogger.setLevel(logging.INFO) -_eventHandler = logging.StreamHandler() -_eventFormatter = logging.Formatter(EventLoggerAdapter.FORMAT) -_eventHandler.setFormatter(_eventFormatter) -_eventLogger.addHandler(_eventHandler) -eventLogger = EventLoggerAdapter(_eventLogger, {}) +def initAppConsole() : + global console + console = logging.getLogger('minwii.console') + console.setLevel(logging.DEBUG) + handler = MinWiiStreamHandler() + handler.setFormatter(logging.Formatter('APP %(message)s')) + console.addHandler(handler) -hiddenEvents = {pygame.MOUSEMOTION:True, - TIMEOUT:True, - #COLOVER:True, - } +def initEnvLogger() : + global envLogger + envLogger = logging.getLogger('minwii.env') + envLogger.setLevel(logging.DEBUG) + handler = MinWiiStreamHandler() + handler.setFormatter(logging.Formatter('ENV %(message)s')) + envLogger.addHandler(handler) + + +def initEventLogger(): + _eventLogger = logging.getLogger('minwii.events') + _eventLogger.setLevel(logging.INFO) + _eventHandler = MinWiiStreamHandler() + _eventFormatter = logging.Formatter(EventLoggerAdapter.FORMAT) + _eventHandler.setFormatter(_eventFormatter) + _eventLogger.addHandler(_eventHandler) + _eventLogger.addFilter(EventFilter()) + global eventLogger + eventLogger = EventLoggerAdapter(_eventLogger, {}) + +def initLoggers() : + initAppConsole() + initEnvLogger() + initEventLogger() + +initLoggers()