-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathoutput_logging.py
More file actions
49 lines (36 loc) · 1.21 KB
/
output_logging.py
File metadata and controls
49 lines (36 loc) · 1.21 KB
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
43
44
45
46
47
48
49
import contextlib
import os
import sys
class TeeStdout:
def __init__(self, *streams):
self.streams = streams
def write(self, data):
for stream in self.streams:
stream.write(data)
return len(data)
def flush(self):
for stream in self.streams:
stream.flush()
@contextlib.contextmanager
def tee_stdout_to_log(output_dir, model_path, suffix):
if not output_dir:
yield None
return
model_name = os.path.basename(model_path).replace(".pt", "")
log_path = os.path.join(output_dir, f"{model_name}_{suffix}.log")
with open(log_path, "w") as log_file:
tee = TeeStdout(sys.stdout, log_file)
with contextlib.redirect_stdout(tee):
yield log_path
def start_stdout_tee(output_dir, model_path, suffix):
if not output_dir:
return lambda: None
model_name = os.path.basename(model_path).replace(".pt", "")
log_path = os.path.join(output_dir, f"{model_name}_{suffix}.log")
log_file = open(log_path, "w")
original_stdout = sys.stdout
sys.stdout = TeeStdout(original_stdout, log_file)
def restore():
sys.stdout = original_stdout
log_file.close()
return restore