X-Git-Url: https://svn.cri.ensmp.fr/git/minwii.git/blobdiff_plain/cf0a97e6fca3155000772d961bba89c215e1b457..baf4f2fe0e04d6b95c428813dd920b597031ec04:/src/app/log.py?ds=inline diff --git a/src/app/log.py b/src/app/log.py index e3a2182..e30daa9 100755 --- a/src/app/log.py +++ b/src/app/log.py @@ -22,6 +22,39 @@ hiddenEvents = {pygame.MOUSEMOTION: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 = "EVT %(ticks)-10s %(eventName)-15s %(message)s" @@ -43,25 +76,38 @@ class EventFilter : def filter(self, record) : return not hiddenEvents.has_key(record.eventType) -logsdir = os.path.join(os.path.expanduser('~'), 'minwii_logs') -if not os.path.exists(logsdir) : - os.mkdir(logsdir) -logfile = os.path.join(logsdir, datetime.datetime.now().strftime('%Y-%m-%d-%H_%M_%S.log')) -logfile = open(logfile, 'w') +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) + +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) + -console = logging.getLogger('minwii.console') -console.setLevel(logging.DEBUG) -handler = logging.StreamHandler(logfile) -handler.setFormatter(logging.Formatter('APP %(message)s')) -console.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, {}) -_eventLogger = logging.getLogger('minwii.events') -_eventLogger.setLevel(logging.INFO) -_eventHandler = logging.StreamHandler(logfile) -_eventFormatter = logging.Formatter(EventLoggerAdapter.FORMAT) -_eventHandler.setFormatter(_eventFormatter) -_eventLogger.addHandler(_eventHandler) -_eventLogger.addFilter(EventFilter()) +def initLoggers() : + initAppConsole() + initEnvLogger() + initEventLogger() -eventLogger = EventLoggerAdapter(_eventLogger, {}) +initLoggers()