-
Notifications
You must be signed in to change notification settings - Fork 0
/
Logger.py
414 lines (277 loc) · 11.5 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
# -*- coding: utf-8 -*-
#
# Author: Roland Pihlakas, 2017 - 2021
#
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at https://mozilla.org/MPL/2.0/.
#
import os
import sys
import io
import time
import datetime
import locale
import traceback
# https://stackoverflow.com/questions/333995/how-to-detect-that-python-code-is-being-executed-through-the-debugger
is_dev_machine = (os.name == 'nt')
debugging = (is_dev_machine and sys.gettrace() is not None) and (1 == 1) # debugging switches
# TODO
# from MyPool import current_process_index, parent_exited, exiting
current_process_index = 0
parent_exited = False
exiting = False
current_request_id = None
capture_level = 0
capture_buffers = []
pid = os.getpid()
pid_str = " : " + str(pid).rjust(7) + " : "
# set up console colors
# https://en.wikipedia.org/wiki/ANSI_escape_code#SGR
ansi_PREFIX = '\033['
ansi_RESET = '\033[0m'
ansi_INTENSE = '\033[1m' # bold or bright colour variation
ansi_SLOWBLINK = '\033[5m' # inconsistent support in terminals
ansi_REVERSE = '\033[7m' # inconsistent support in terminals
# foreground colors:
ansi_BLACK = '\033[30m'
ansi_RED = '\033[31m'
ansi_GREEN = '\033[32m'
ansi_YELLOW = '\033[33m'
ansi_BLUE = '\033[34m'
ansi_MAGENTA = '\033[35m'
ansi_CYAN = '\033[36m'
ansi_WHITE = '\033[37m'
ansi_colors_inited = None
def init_colors():
global ansi_colors_inited
if ansi_colors_inited is not None:
return;
if os.name == "nt":
ansi_colors_inited = False
try:
# https://github.com/tartley/colorama
#
# On Windows, calling init() will filter ANSI escape sequences out of any text sent to stdout or stderr, and replace them with equivalent Win32 calls.
#
# On other platforms, calling init() has no effect (unless you request other optional functionality; see “Init Keyword Args”, below). By design, this permits applications to call init() unconditionally on all platforms, after which ANSI output should just work.
#
# To stop using Colorama before your program exits, simply call deinit(). This will restore stdout and stderr to their original values, so that Colorama is disabled. To resume using Colorama again, call reinit(); it is cheaper than calling init() again (but does the same thing).
import colorama
colorama.init()
ansi_colors_inited = True
except Exception as ex:
pass
else: #/ if os.name == "nt":
ansi_colors_inited = True
#/ if os.name == "nt":
#/ def init_colors():
def get_now_str():
now_str = datetime.datetime.strftime(datetime.datetime.now(), '%m.%d %H:%M:%S')
return now_str
#/ def get_now_str():
# https://stackoverflow.com/questions/14906764/how-to-redirect-stdout-to-both-file-and-console-with-scripting
class Logger(object):
def __init__(self, terminal, logfile, is_error_log):
self.is_error_log = is_error_log
if (terminal
and os.name == "nt"
and not isinstance(terminal, Logger)): # NB! loggers can be chained for log file replication purposes, but colorama.AnsiToWin32 must not be chained else colour info gets lost
try:
import colorama
init_colors()
# NB! use .stream in order to be able to call flush()
self.terminal = colorama.AnsiToWin32(terminal).stream # https://github.com/tartley/colorama
except Exception as ex:
self.terminal = terminal
else:
self.terminal = terminal
self.log = None
try:
if logfile is not None:
self.log = io.open(logfile, "a", 1024 * 1024, encoding="utf8", errors='ignore') # https://stackoverflow.com/questions/12468179/unicodedecodeerror-utf8-codec-cant-decode-byte-0x9c
except Exception as ex:
msg = "Exception during Logger.__init__ io.open: " + str(ex) + "\n" + traceback.format_exc()
msg_with_colours = ansi_RED + ansi_INTENSE + msg + ansi_RESET
if capture_level == 0:
try:
self.terminal.write(msg)
except Exception as ex:
pass
else: #/ if capture_level == 0:
capture_buffers[capture_level - 1] += msg_with_colours
try:
if self.log is not None and not self.log.closed:
self.log.flush()
except Exception as ex: # time_limit.TimeoutException
if (is_dev_machine or not self.is_error_log) and self.terminal:
msg = str(ex) + "\n" + traceback.format_exc()
msg_with_colours = ansi_RED + ansi_INTENSE + msg + ansi_RESET
if capture_level == 0:
try:
self.terminal.write(msg_with_colours)
except Exception as ex:
pass
else: #/ if capture_level == 0:
capture_buffers[capture_level - 1] += msg
#/ if (is_dev_machine or not self.is_error_log) and self.terminal:
#/ def __init__(self, terminal, logfile, is_error_log):
def write(self, message_in):
if parent_exited or exiting: # NB!
return
# if message_in.strip() == "":
# return
if isinstance(message_in, bytes):
message = message_in.decode("utf-8", 'ignore') # NB! message_in might be byte array not a string
else:
message = message_in
now = get_now_str()
if len(message) >= 2 and message[-2:] == "\n\r":
message_end_linebreak = "\n\r"
elif len(message) >= 2 and message[-2:] == "\r\n":
message_end_linebreak = "\r\n"
elif len(message) >= 1 and message[-1:] == "\n":
message_end_linebreak = "\n"
else:
message_end_linebreak = ""
newline_prefix = now + pid_str + str(current_process_index).rjust(6) + " : "
newline = message_end_linebreak if message_end_linebreak else "\n"
message_without_end_linebreak = message[:-len(message_end_linebreak)] if message_end_linebreak != "" else message
message_with_timestamp = newline_prefix + message_without_end_linebreak.replace(newline, newline + newline_prefix) + message_end_linebreak
if message_end_linebreak == "":
# message += "\n"
message_with_timestamp += "\n"
message_with_timestamp = message_with_timestamp.encode("utf-8", 'ignore').decode("utf-8", 'ignore') #.decode('latin1', 'ignore'))
if (is_dev_machine or not self.is_error_log) and self.terminal: # error_log output is hidden from live console
msg_with_colours = ""
use_ansi_colour = True and ((False or self.is_error_log)
and (ansi_PREFIX not in message) # NB! ensure that we do not override any colours present in the message
and message.strip() != "") # optimisation for newlines
if use_ansi_colour:
msg_with_colours += ansi_RED + ansi_INTENSE
msg_with_colours += message
if use_ansi_colour:
msg_with_colours += ansi_RESET
if capture_level == 0:
try:
self.terminal.write(msg_with_colours)
except Exception as ex:
msg = str(ex) + "\n" + traceback.format_exc()
try:
if self.log is not None and not self.log.closed:
self.log.write(msg)
self.log.flush()
except Exception as ex:
pass
else: #/ if capture_level == 0:
capture_buffers[capture_level - 1] += msg_with_colours
#/ if (is_dev_machine or not self.is_error_log) and self.terminal:
if message.strip() != "": # NB! still write this message to console since it might contain progress bars or other formatting
try:
if self.log is not None and not self.log.closed:
self.log.write(message_with_timestamp.strip() + "\n") # NB! strip
self.log.flush()
except Exception as ex:
if (is_dev_machine or not self.is_error_log) and self.terminal:
msg = str(ex) + "\n" + traceback.format_exc()
msg_with_colours = ansi_RED + ansi_INTENSE + msg + ansi_RESET
if capture_level == 0:
try:
self.terminal.write(msg_with_colours)
except Exception as ex:
pass
else: #/ if capture_level == 0:
capture_buffers[capture_level - 1] += msg
#/ if (is_dev_machine or not self.is_error_log) and self.terminal:
#/ if message.strip() != "":
#/ def write(self, message_with_timestamp):
def flush(self):
try:
if self.terminal:
self.terminal.flush()
except Exception as ex:
msg = str(ex) + "\n" + traceback.format_exc()
try:
if self.log is not None and not self.log.closed:
self.log.write(msg)
self.log.flush()
except Exception as ex:
pass
try:
if self.log is not None and not self.log.closed:
self.log.flush()
except Exception as ex:
if (is_dev_machine or not self.is_error_log) and self.terminal:
msg = str(ex) + "\n" + traceback.format_exc()
msg_with_colours = ansi_RED + ansi_INTENSE + msg + ansi_RESET
if capture_level == 0:
try:
self.terminal.write(msg_with_colours)
except Exception as ex:
pass
else: #/ if capture_level == 0:
capture_buffers[capture_level - 1] += msg
#/ if (is_dev_machine or not self.is_error_log) and self.terminal:
pass
#/ def flush(self):
@property
def encoding(self):
return self.terminal.encoding
#return "utf8"
def fileno(self):
return self.terminal.fileno()
#/ class Logger(object):
def rename_log_file_if_needed(filename, max_tries = 10):
if os.path.exists(filename):
try:
filedate = datetime.datetime.fromtimestamp(os.path.getmtime(filename))
filedate_str = datetime.datetime.strftime(filedate, '%Y.%m.%d-%H.%M.%S')
new_filename = filename + "-" + filedate_str + ".txt"
if filename != new_filename:
try_index = 1
while True:
try:
os.rename(filename, new_filename)
return
except Exception as ex:
if try_index >= max_tries:
raise
try_index += 1
print("retrying log file rename: " + filename)
time.sleep(1)
continue
#/ try:
#/ if filename != new_filename:
except Exception as ex:
pass
#/ if os.path.exists(filename):
#/ def rename_log_file_if_needed(filename):
def logger_set_current_request_id(new_current_request_id):
global current_request_id
current_request_id = new_current_request_id
#/ def logger_set_current_request_id(new_current_request_id):
def start_capture():
global capture_level
capture_level += 1
capture_buffers.append("")
#/ def start_capture():
def end_capture(print_result = False):
global capture_level
assert(capture_level >= 1)
capture_level -= 1
result = capture_buffers.pop()
if print_result:
try:
print(result)
except Exception:
encoding = locale.getpreferredencoding(False)
if not encoding:
encoding = locale.getdefaultlocale()[1]
if not encoding:
encoding = "ascii"
print(result.encode("utf-8", 'ignore').decode(encoding, 'ignore'))
#/ if print_result:
return result
#/ def end_capture():