peinjector/src/log.py

62 lines
1.6 KiB
Python
Raw Normal View History

2024-02-08 19:04:07 +08:00
# ------ cxykevin log moudle ------
# This moudle is not a part of this project.
# This file is CopyNone.
import logging
import config
import utils
import sys
levels = {
"debug": logging.DEBUG,
'info': logging.INFO,
'warning': logging.WARN,
'err': logging.ERROR
}
2024-02-10 21:02:45 +08:00
def newlog() -> None:
2024-02-12 13:25:34 +08:00
with open(config.LOGPATH.format(DISK=utils.find_disk()), "w") as _:
2024-02-08 19:04:07 +08:00
pass
2024-02-10 21:02:45 +08:00
def init() -> None:
2024-02-12 13:25:34 +08:00
logging.basicConfig(filename=config.LOGPATH.format(DISK=utils.find_disk()),
2024-02-08 19:04:07 +08:00
format='[%(asctime)s][%(levelname)s] %(message)s',
level=levels[config.LOGLEVEL])
2024-02-10 21:02:45 +08:00
def info(msg: str) -> None:
2024-02-08 19:04:07 +08:00
if (20 >= levels[config.LOGLEVEL]):
sys.stdout.write(
("[\033[34mINFO\033[0m]" if config.OUTPUT_COLORFUL else "[INFO]") + msg + "\n")
logging.info(msg)
2024-02-10 21:02:45 +08:00
def warn(msg: str) -> None:
2024-02-08 19:04:07 +08:00
if (30 >= levels[config.LOGLEVEL]):
sys.stdout.write(
("[\033[33mWARNING\033[0m]" if config.OUTPUT_COLORFUL else "[WARNING]") + msg + "\n")
logging.warn(msg)
2024-02-10 21:02:45 +08:00
def err(msg: str) -> None:
2024-02-08 19:04:07 +08:00
if (40 >= levels[config.LOGLEVEL]):
sys.stdout.write(
("[\033[31mERROR\033[0m]" if config.OUTPUT_COLORFUL else "[ERROR]") + msg + "\n")
logging.error(msg)
2024-02-10 21:02:45 +08:00
def break_err(msg: str) -> None:
2024-02-08 19:04:07 +08:00
if (40 >= levels[config.LOGLEVEL]):
sys.stdout.write(
("[\033[31mERROR\033[0m]" if config.OUTPUT_COLORFUL else "[ERROR]") + msg + "\n")
logging.error(msg)
2024-02-10 21:02:45 +08:00
def print(*args, end="\n") -> None:
2024-02-08 19:04:07 +08:00
msg = ' '.join(map(str, args))
if (10 >= levels[config.LOGLEVEL]):
sys.stdout.write(msg + end)
logging.debug(msg)