-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.py
42 lines (33 loc) · 999 Bytes
/
logger.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import os
import sys
class Logger(object):
def __init__(self, fpath=None):
self.console = sys.stdout
self.file = None
if fpath is not None:
fdir = os.path.dirname(fpath)
if fdir is not None and not os.path.exists(fdir):
os.makedirs(fdir)
self.file = open(fpath, 'w', encoding='utf-8')
def __del__(self):
self.close()
def __enter__(self):
pass
def __exit__(self, *args):
self.close()
def write(self, msg):
self.console.write(msg)
if self.file is not None:
self.file.write(msg)
def save_log(self, msg):
if self.file is not None:
self.file.write(msg)
def flush(self):
self.console.flush()
if self.file is not None:
self.file.flush()
os.fsync(self.file.fileno())
def close(self):
# self.console.close()
if self.file is not None:
self.file.close()