rework logging

pull/25/head
Pavol Rusnak 8 years ago
parent 34e3b51ba8
commit 71496913ba
No known key found for this signature in database
GPG Key ID: 91F3B339B9A02A3D

@ -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)

@ -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")

@ -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

@ -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]

Loading…
Cancel
Save