trezor-core/src/trezor/log.py

56 lines
1.1 KiB
Python
Raw Normal View History

2016-09-29 03:29:43 -07:00
from micropython import const
2016-04-29 10:54:54 -07: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
2017-06-12 09:16:06 -07:00
def _log(name, mlevel, msg, *args):
if __debug__ and mlevel >= level:
2016-04-29 10:54:54 -07:00
if color:
2017-08-21 04:31:45 -07:00
fmt = '%d \x1b[35m%s\x1b[0m \x1b[' + _leveldict[mlevel][1] + 'm%s\x1b[0m ' + msg
2016-04-29 10:54:54 -07:00
else:
fmt = '%d %s %s ' + msg
2017-03-06 08:33:42 -08:00
print(fmt % ((utime.ticks_us(), name, _leveldict[mlevel][0]) + args))
2016-04-29 10:54:54 -07:00
2017-06-12 09:16:06 -07:00
def debug(name, msg, *args):
2016-04-29 13:01:04 -07:00
_log(name, DEBUG, msg, *args)
2016-04-29 10:54:54 -07:00
2017-06-12 09:16:06 -07:00
def info(name, msg, *args):
2016-04-29 13:01:04 -07:00
_log(name, INFO, msg, *args)
2016-04-29 10:54:54 -07:00
2017-06-12 09:16:06 -07:00
def warning(name, msg, *args):
2016-04-29 13:01:04 -07:00
_log(name, WARNING, msg, *args)
2016-04-29 10:54:54 -07:00
2017-06-12 09:16:06 -07:00
def error(name, msg, *args):
2016-04-29 13:01:04 -07:00
_log(name, ERROR, msg, *args)
2016-04-29 10:54:54 -07:00
2017-06-12 09:16:06 -07:00
def exception(name, exc):
2017-03-06 08:33:42 -08:00
_log(name, ERROR, 'exception:')
sys.print_exception(exc)
2017-06-12 09:16:06 -07:00
def critical(name, msg, *args):
2016-04-29 13:01:04 -07:00
_log(name, CRITICAL, msg, *args)