42 lines
995 B
Python
42 lines
995 B
Python
|
import time
|
||
|
import sys
|
||
|
|
||
|
|
||
|
# 同一个进程中所有调用这个文件的 .py 文件都使用这个变量
|
||
|
_log_fp=None
|
||
|
|
||
|
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):
|
||
|
_log_fp.write(_time())
|
||
|
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(data)
|
||
|
sys.stdout.flush()
|
||
|
|
||
|
def log_init(file_name:str):
|
||
|
global _log_fp
|
||
|
if _log_fp is None:
|
||
|
_log_fp=open(file_name,mode="a+",encoding="utf-8")
|
||
|
|