-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLogger.py
executable file
·91 lines (80 loc) · 3.64 KB
/
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/usr/bin/env python3.5
"""
a class for making log files, during stdout print
"""
import os
import colorama
import inspect
import sys
import time
class Logger:
def __init__(self, log_file, path=None, err_file=None, emblem=None):
self.started = time.time()
self.HEIGHT, self.WIDTH = (int(a) for a in os.popen('stty size', 'r').read().split())
if path is None:
path = os.getcwd()+'/'
elif path[-1] != '/':
path += '/'
self.emblem = emblem if emblem is not None else '~'
self.HOME = os.environ['HOME']
self.log_file = open(path+log_file, 'w+')
header = ' Starting %s ' % sys.argv[0]
self.create_header(header)
self.log('given command: %s' % ' '.join(sys.argv))
self.files_logged = {}
def log(self, string, to_print=True, emphasize=False, time_stamp=True, skip_stamp: bool=False):
# create time stamp
log_ts = "%s" % time.strftime("%H:%M") if time_stamp else ""
ts = colorama.Fore.RED + log_ts + colorama.Style.RESET_ALL
# create path stamp
log_pt = os.path.expanduser(os.getcwd()).replace(self.HOME, '~')
pt = colorama.Fore.GREEN + log_pt + colorama.Style.RESET_ALL
# create calling frame stamp
ins = inspect.getouterframes( inspect.currentframe() )
(frame, filename, lineno, function, code_context, index) = ins[1]
log_lc ='%s$%s:%i' % (filename.split('/')[-1], function, lineno)
lc = colorama.Fore.MAGENTA + log_lc + colorama.Style.RESET_ALL
# shrink path stamp
if pt.count('/') > 1:
s = pt.split('/')
log_pt = "%s" % s[-1]
pt = colorama.Fore.GREEN + log_pt + colorama.Style.RESET_ALL
if len('<%s@%s@%s> %s' % (pt, ts,lc, string)) > self.WIDTH-2:
string = '\n--> %s' % string
# print or not and log
if to_print:
if emphasize:
print(colorama.Fore.RED + colorama.Back.GREEN + string + colorama.Style.RESET_ALL)
elif not skip_stamp:
print('<%s@%s@%s> %s' % (pt, ts,lc, string))
self.log_file.write('<%s@%s@%s> %s\n' % (log_pt, log_ts, log_lc, string))
else:
print(string)
self.log_file.write('%s\n' % string)
self.log_file.flush()
sys.stdout.flush()
def log_text_file(self, file_name: str, to_print: bool = True):
if file_name in self.files_logged.keys():
self.log('file logged at %i' % self.files_logged[file_name])
else:
self.files_logged[file_name] = len(list(self.files_logged.keys()))+1
self.log('logging the file %s at num %i' % (file_name, len(list(self.files_logged.keys()))+1))
for l in open(file_name, 'r'):
if to_print:
print(l.rstrip())
self.log_file.write(l)
self.log_file.write('\n\n')
def close(self):
time_delta = time.time() - self.started
h, remain = divmod(time_delta, 3600)
m, s = divmod(remain, 60)
self.log('finished at %s, process took %i:%i:%i' % (time.strftime("%H:%M"), h, m, s))
self.create_header('Finishing %s' % sys.argv[0])
self.log_file.close()
def create_header(self, msg: str='') -> None:
print('='*self.WIDTH)
print('%s%s%s' % ('='*int((self.WIDTH-len(msg))/2), colorama.Fore.BLUE + msg + colorama.Style.RESET_ALL, '='*int((self.WIDTH-len(msg))/2)))
print('='*self.WIDTH)
self.log_file.write('%s%s%s\n' % ('='*int((self.WIDTH-len(msg))/2), msg, '='*int((self.WIDTH- len(msg))/2)))
self.log_file.flush()
sys.stdout.flush()