You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
61 lines
1.2 KiB
61 lines
1.2 KiB
8 years ago
|
import sys
|
||
|
import utime
|
||
6 years ago
|
from micropython import const
|
||
8 years ago
|
|
||
|
NOTSET = const(0)
|
||
|
DEBUG = const(10)
|
||
|
INFO = const(20)
|
||
|
WARNING = const(30)
|
||
|
ERROR = const(40)
|
||
|
CRITICAL = const(50)
|
||
|
|
||
|
_leveldict = {
|
||
6 years ago
|
DEBUG: ("DEBUG", "32"),
|
||
|
INFO: ("INFO", "36"),
|
||
|
WARNING: ("WARNING", "33"),
|
||
|
ERROR: ("ERROR", "31"),
|
||
|
CRITICAL: ("CRITICAL", "1;31"),
|
||
8 years ago
|
}
|
||
|
|
||
6 years ago
|
level = DEBUG
|
||
8 years ago
|
color = True
|
||
|
|
||
7 years ago
|
|
||
8 years ago
|
def _log(name, mlevel, msg, *args):
|
||
|
if __debug__ and mlevel >= level:
|
||
8 years ago
|
if color:
|
||
6 years ago
|
fmt = (
|
||
|
"%d \x1b[35m%s\x1b[0m \x1b["
|
||
|
+ _leveldict[mlevel][1]
|
||
|
+ "m%s\x1b[0m "
|
||
|
+ msg
|
||
|
)
|
||
8 years ago
|
else:
|
||
6 years ago
|
fmt = "%d %s %s " + msg
|
||
7 years ago
|
print(fmt % ((utime.ticks_us(), name, _leveldict[mlevel][0]) + args))
|
||
8 years ago
|
|
||
7 years ago
|
|
||
8 years ago
|
def debug(name, msg, *args):
|
||
8 years ago
|
_log(name, DEBUG, msg, *args)
|
||
8 years ago
|
|
||
7 years ago
|
|
||
8 years ago
|
def info(name, msg, *args):
|
||
8 years ago
|
_log(name, INFO, msg, *args)
|
||
8 years ago
|
|
||
7 years ago
|
|
||
8 years ago
|
def warning(name, msg, *args):
|
||
8 years ago
|
_log(name, WARNING, msg, *args)
|
||
8 years ago
|
|
||
7 years ago
|
|
||
8 years ago
|
def error(name, msg, *args):
|
||
8 years ago
|
_log(name, ERROR, msg, *args)
|
||
8 years ago
|
|
||
7 years ago
|
|
||
8 years ago
|
def exception(name, exc):
|
||
6 years ago
|
_log(name, ERROR, "exception:")
|
||
7 years ago
|
sys.print_exception(exc)
|
||
8 years ago
|
|
||
7 years ago
|
|
||
8 years ago
|
def critical(name, msg, *args):
|
||
8 years ago
|
_log(name, CRITICAL, msg, *args)
|