1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-12-13 09:58:09 +00:00
trezor-firmware/src/trezor/log.py

50 lines
1.1 KiB
Python
Raw Normal View History

2016-05-10 11:59:22 +00:00
import uio
2016-04-29 17:54:54 +00:00
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(name, mlevel, msg, *args):
if __debug__ and mlevel >= level:
2016-04-29 17:54:54 +00:00
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(name, msg, *args):
2016-04-29 20:01:04 +00:00
_log(name, DEBUG, msg, *args)
2016-04-29 17:54:54 +00:00
def info(name, msg, *args):
2016-04-29 20:01:04 +00:00
_log(name, INFO, msg, *args)
2016-04-29 17:54:54 +00:00
def warning(name, msg, *args):
2016-04-29 20:01:04 +00:00
_log(name, WARNING, msg, *args)
2016-04-29 17:54:54 +00:00
def error(name, msg, *args):
2016-04-29 20:01:04 +00:00
_log(name, ERROR, msg, *args)
2016-04-29 17:54:54 +00:00
def exception(name, exc):
2016-05-10 11:59:22 +00:00
out = uio.StringIO()
sys.print_exception(exc, out)
_log(name, ERROR, out.getvalue())
def critical(name, msg, *args):
2016-04-29 20:01:04 +00:00
_log(name, CRITICAL, msg, *args)