46 lines
1.0 KiB
Python
46 lines
1.0 KiB
Python
import time
|
|
import sys
|
|
|
|
|
|
# 同一个进程中所有调用这个文件的 .py 文件都使用这个变量
|
|
_log_fp=None
|
|
|
|
STR_RED="\033[0;31m"
|
|
STR_BLUE="\033[0;34m"
|
|
STR_YELLOW="\033[0;33m"
|
|
STR_END="\033[0m"
|
|
|
|
def _time():
|
|
return '['+time.strftime("%Y-%m-%d %H:%M:%S")+']'
|
|
|
|
def myprint_dec(func):
|
|
def wrapper(*args, **kwargs):
|
|
# 在这里添加额外的功能
|
|
print(*args, **kwargs)
|
|
if(_log_fp is not None):
|
|
kwargs["file"]=_log_fp
|
|
result = func(*args, **kwargs)
|
|
_log_fp.flush()
|
|
else:
|
|
result=None
|
|
return result
|
|
return wrapper
|
|
|
|
myprint=myprint_dec(print)
|
|
|
|
def mywrite(data:str):
|
|
if(_log_fp is not None):
|
|
txt=data.replace('\r','\n')
|
|
txt=txt.replace('\n\n','\n')
|
|
txt=txt.replace('\n\n','\n')
|
|
_log_fp.write(txt)
|
|
_log_fp.flush()
|
|
sys.stdout.write(f"{STR_YELLOW}{data}{STR_END}")
|
|
sys.stdout.flush()
|
|
|
|
def log_init(file_name:str):
|
|
global _log_fp
|
|
if _log_fp is None:
|
|
_log_fp=open(file_name,mode="w+",encoding="utf-8")
|
|
|