From 71496913ba16a7aab818e6b0187210048a257c03 Mon Sep 17 00:00:00 2001 From: Pavol Rusnak Date: Fri, 29 Apr 2016 19:54:54 +0200 Subject: [PATCH] rework logging --- src/lib/log.py | 45 +++++++++++++++++++++++++++ src/lib/logging.py | 77 ---------------------------------------------- src/trezor/loop.py | 4 +-- src/trezor/main.py | 4 --- 4 files changed, 46 insertions(+), 84 deletions(-) create mode 100644 src/lib/log.py delete mode 100644 src/lib/logging.py diff --git a/src/lib/log.py b/src/lib/log.py new file mode 100644 index 0000000000..057317a936 --- /dev/null +++ b/src/lib/log.py @@ -0,0 +1,45 @@ +import sys +import utime + +NOTSET = const(0) +DEBUG = const(10) +INFO = const(20) +WARNING = const(30) +ERROR = const(40) +CRITICAL = const(50) + +_leveldict = { + DEBUG: ('DEBUG', '32'), + INFO: ('INFO', '36'), + WARNING: ('WARNING', '33'), + ERROR: ('ERROR', '31'), + CRITICAL: ('CRITICAL', '1;31'), +} + +level = NOTSET +color = True + +def _log(mlevel, msg, *args): + global level + if mlevel >= level: + name = 'name' + if color: + fmt = '%d \x1b[35m%s\x1b[0m %s \x1b[' + _leveldict[mlevel][1] + 'm' + msg + '\x1b[0m' + else: + fmt = '%d %s %s ' + msg + print(fmt % ((utime.ticks_us(), name, _leveldict[mlevel][0]) + args), file=sys.stderr) + +def debug(msg, *args): + _log(DEBUG, msg, *args) + +def info(msg, *args): + _log(INFO, msg, *args) + +def warning(msg, *args): + _log(WARNING, msg, *args) + +def error(msg, *args): + _log(ERROR, msg, *args) + +def critical(msg, *args): + _log(CRITICAL, msg, *args) diff --git a/src/lib/logging.py b/src/lib/logging.py deleted file mode 100644 index 92f10bba45..0000000000 --- a/src/lib/logging.py +++ /dev/null @@ -1,77 +0,0 @@ -import sys - -# raise Exception("Disabled") - -CRITICAL = const(50) -ERROR = const(40) -WARNING = const(30) -INFO = const(20) -DEBUG = const(10) -NOTSET = const(0) - -_level_dict = { - CRITICAL: "CRIT", - ERROR: "ERROR", - WARNING: "WARN", - INFO: "INFO", - DEBUG: "DEBUG", -} - -_stream = sys.stderr - -class Logger: - - def __init__(self, name): - self.level = NOTSET - self.name = name - - def _level_str(self, level): - if level in _level_dict: - return _level_dict[level] - return "LVL" + str(level) - - def log(self, level, msg, *args): - if level >= (self.level or _level): - print(("%s:%s:" + msg) % ((self._level_str(level), self.name) + args), file=_stream) - - def debug(self, msg, *args): - self.log(DEBUG, msg, *args) - - def info(self, msg, *args): - self.log(INFO, msg, *args) - - def warning(self, msg, *args): - self.log(WARNING, msg, *args) - - def error(self, msg, *args): - self.log(ERROR, msg, *args) - - def critical(self, msg, *args): - self.log(CRITICAL, msg, *args) - - -_level = INFO -_loggers = {} - -def getLogger(name): - if name in _loggers: - return _loggers[name] - l = Logger(name) - _loggers[name] = l - return l - -def info(msg, *args): - getLogger(None).info(msg, *args) - -def debug(msg, *args): - getLogger(None).debug(msg, *args) - -def basicConfig(level=INFO, filename=None, stream=None, format=None): - global _level, _stream - _level = level - if stream: - _stream = stream - if filename is not None: - print("logging.basicConfig: filename arg is not supported") - if format is not None: - print("logging.basicConfig: format arg is not supported") diff --git a/src/trezor/loop.py b/src/trezor/loop.py index bacdac4840..7231c48104 100644 --- a/src/trezor/loop.py +++ b/src/trezor/loop.py @@ -5,9 +5,7 @@ from .utils import type_gen from . import msg from . import ui -if __debug__: - import logging - log = logging.getLogger("trezor.loop") +import log q = [] cnt = 0 diff --git a/src/trezor/main.py b/src/trezor/main.py index 435e129e9b..a1b08af42f 100644 --- a/src/trezor/main.py +++ b/src/trezor/main.py @@ -6,10 +6,6 @@ import gc from trezor import loop from trezor import layout -if __debug__: - import logging - logging.basicConfig(level=logging.INFO) - def perf_info_debug(): while True: queue = [str(x[2]).split("'")[1] for x in loop.q]