-
Notifications
You must be signed in to change notification settings - Fork 0
/
Logger.py
70 lines (49 loc) · 2 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
#!/usr/bin/env python3
"""
Logging and printing utility.
Copyright (C) 2019 Peter Maar
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
"""
from time import time
logfile_location = 'log.txt'
logfile = open(logfile_location, 'a', encoding='utf-8') # Open for appending.
# UTF-8 helps with some special characters that happen sometimes (like the "No Category:" line of the output of ?help)
# To use, just add this import:
# "from Logger import log"
logfile_closed = False
def log(*args, sep=' ', end='\n', flush=False):
"""
Print and log the values.
Optional keyword arguments:
sep: string inserted between values, default a space.
end: string appended after the last value, default a newline.
flush: whether to forcibly flush the log stream.
"""
time_string = 'Time ' + str(time())[:15] + ":\t"
output_string = time_string
for value in args:
output_string += str(value) + sep
output_string = output_string[:-len(sep)]
# Add to the newlines spaces and a tab to match the time string length
output_string = output_string.replace('\n', '\n' + ' '*(len(time_string)-1) + '\t')
output_string += end
print(output_string, end='')
if not logfile_closed:
logfile.write(output_string)
else:
print("Error:\tOops! You tried to log with the logfile closed!")
if flush:
logfile.flush()
def close_log():
global logfile_closed
logfile.close()
logfile_closed = True