2016-04-29 17:54:54 +00:00
|
|
|
import sys
|
|
|
|
import utime
|
2018-07-03 14:20:26 +00:00
|
|
|
from micropython import const
|
2016-04-29 17:54:54 +00:00
|
|
|
|
2019-07-03 13:07:04 +00:00
|
|
|
if False:
|
|
|
|
from typing import Any
|
|
|
|
|
2016-04-29 17:54:54 +00:00
|
|
|
NOTSET = const(0)
|
|
|
|
DEBUG = const(10)
|
|
|
|
INFO = const(20)
|
|
|
|
WARNING = const(30)
|
|
|
|
ERROR = const(40)
|
|
|
|
CRITICAL = const(50)
|
|
|
|
|
|
|
|
_leveldict = {
|
2018-07-03 14:20:58 +00:00
|
|
|
DEBUG: ("DEBUG", "32"),
|
|
|
|
INFO: ("INFO", "36"),
|
|
|
|
WARNING: ("WARNING", "33"),
|
|
|
|
ERROR: ("ERROR", "31"),
|
|
|
|
CRITICAL: ("CRITICAL", "1;31"),
|
2016-04-29 17:54:54 +00:00
|
|
|
}
|
|
|
|
|
2018-04-16 15:17:45 +00:00
|
|
|
level = DEBUG
|
2016-04-29 17:54:54 +00:00
|
|
|
color = True
|
|
|
|
|
2017-06-12 16:16:06 +00:00
|
|
|
|
2019-07-03 13:07:04 +00:00
|
|
|
def _log(name: str, mlevel: int, msg: str, *args: Any) -> None:
|
2016-04-29 19:46:34 +00:00
|
|
|
if __debug__ and mlevel >= level:
|
2016-04-29 17:54:54 +00:00
|
|
|
if color:
|
2018-07-03 14:20:58 +00:00
|
|
|
fmt = (
|
|
|
|
"%d \x1b[35m%s\x1b[0m \x1b["
|
|
|
|
+ _leveldict[mlevel][1]
|
|
|
|
+ "m%s\x1b[0m "
|
|
|
|
+ msg
|
|
|
|
)
|
2016-04-29 17:54:54 +00:00
|
|
|
else:
|
2018-07-03 14:20:58 +00:00
|
|
|
fmt = "%d %s %s " + msg
|
2017-03-06 16:33:42 +00:00
|
|
|
print(fmt % ((utime.ticks_us(), name, _leveldict[mlevel][0]) + args))
|
2016-04-29 17:54:54 +00:00
|
|
|
|
2017-06-12 16:16:06 +00:00
|
|
|
|
2019-07-03 13:07:04 +00:00
|
|
|
def debug(name: str, msg: str, *args: Any) -> None:
|
2016-04-29 20:01:04 +00:00
|
|
|
_log(name, DEBUG, msg, *args)
|
2016-04-29 17:54:54 +00:00
|
|
|
|
2017-06-12 16:16:06 +00:00
|
|
|
|
2019-07-03 13:07:04 +00:00
|
|
|
def info(name: str, msg: str, *args: Any) -> None:
|
2016-04-29 20:01:04 +00:00
|
|
|
_log(name, INFO, msg, *args)
|
2016-04-29 17:54:54 +00:00
|
|
|
|
2017-06-12 16:16:06 +00:00
|
|
|
|
2019-07-03 13:07:04 +00:00
|
|
|
def warning(name: str, msg: str, *args: Any) -> None:
|
2016-04-29 20:01:04 +00:00
|
|
|
_log(name, WARNING, msg, *args)
|
2016-04-29 17:54:54 +00:00
|
|
|
|
2017-06-12 16:16:06 +00:00
|
|
|
|
2019-07-03 13:07:04 +00:00
|
|
|
def error(name: str, msg: str, *args: Any) -> None:
|
2016-04-29 20:01:04 +00:00
|
|
|
_log(name, ERROR, msg, *args)
|
2016-04-29 17:54:54 +00:00
|
|
|
|
2017-06-12 16:16:06 +00:00
|
|
|
|
2019-07-03 13:07:04 +00:00
|
|
|
def critical(name: str, msg: str, *args: Any) -> None:
|
|
|
|
_log(name, CRITICAL, msg, *args)
|
2016-04-29 23:48:13 +00:00
|
|
|
|
2017-06-12 16:16:06 +00:00
|
|
|
|
2019-07-03 13:07:04 +00:00
|
|
|
def exception(name: str, exc: BaseException) -> None:
|
2019-08-22 13:31:20 +00:00
|
|
|
# we are using `__class__.__name__` to avoid importing ui module
|
|
|
|
if exc.__class__.__name__ == "Result":
|
|
|
|
_log(name, DEBUG, "ui.Result: %s", exc.value)
|
2019-08-22 15:37:31 +00:00
|
|
|
elif exc.__class__.__name__ == "Cancelled":
|
|
|
|
_log(name, DEBUG, "ui.Cancelled")
|
2019-08-22 13:31:20 +00:00
|
|
|
else:
|
|
|
|
_log(name, ERROR, "exception:")
|
|
|
|
sys.print_exception(exc)
|