-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.py
157 lines (128 loc) · 4.1 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
from time import gmtime, strftime
import pygame
import os
import io
import traceback
def log(message):
print("\033[34m[" +
strftime("%H:%M:%S", gmtime()) +
" / " + str(pygame.time.get_ticks())
+ "] \033[32m[INFO]\033[0m "
+ "\33[36m(" + trace(True) + "\33[36m)\33[0m "
+ str(message))
with open('latest.log', 'a', encoding="UTF-8") as f:
f.write("["
+ strftime("%H:%M:%S", gmtime())
+ " / "
+ str(pygame.time.get_ticks())
+ "] [INFO] "
+ "(" + trace(False) + ") "
+ str(message)
+ "\n")
def warn(message):
print("\033[34m["
+ strftime("%H:%M:%S", gmtime())
+ " / "
+ str(pygame.time.get_ticks())
+ "] \033[33m[WARN]\033[0m "
+ "\33[36m(" + trace(True) + "\33[36m) \33[0m"
+ str(message))
with open('latest.log', 'a', encoding="UTF-8") as f:
f.write("["
+ strftime("%H:%M:%S", gmtime())
+ " / "
+ str(pygame.time.get_ticks())
+ "] [WARN] "
+ "(" + trace(False) + ") "
+ str(message)
+ "\n")
def error(message):
print("\033[34m["
+ strftime("%H:%M:%S", gmtime())
+ " / "
+ str(pygame.time.get_ticks())
+ "] \033[31m[ERROR] "
+ "\33[36m(" + trace(True) + "\33[36m) \33[31m"
+ str(message)
+ "\033[0m")
with open('latest.log', 'a', encoding="UTF-8") as f:
f.write("["
+ strftime("%H:%M:%S", gmtime())
+ " / "
+ str(pygame.time.get_ticks())
+ "] [ERROR] "
+ "(" + trace(False) + ") "
+ str(message)
+ "\n")
def reset_log():
if "latest.log" not in os.listdir('.'):
with open("latest.log", 'w', encoding="UTF-8") as f:
f.write("")
return
with open("latest.log", "w", encoding="UTF-8") as f2:
f2.truncate()
def trace(colors: bool) -> str:
trace = io.StringIO()
traceback.print_stack(file=trace)
trace_string = trace.getvalue()
trace.close()
trace_string_formatted = ''
space = False
even = True
for c in trace_string:
if c == '\n':
if not even:
trace_string_formatted += ';'
even = True
else:
even = False
trace_string_formatted += ' '
space = True
elif c == ' ':
pass
else:
space = False
if not space:
trace_string_formatted += c
trace_string_formatted2 = ''
filename = ''
state = 0
line = ''
i = 0
while i < len(trace_string_formatted):
c = trace_string_formatted[i]
if state == 0:
if c == '"':
state = 1
elif state == 1:
if c != '"':
filename += c
else:
if colors:
trace_string_formatted2 += '\33[36m'
trace_string_formatted2 += os.path.basename(filename)
filename = ''
state = 2
elif state == 2:
j = i + 7
while j < len(trace_string_formatted) and trace_string_formatted[j] != ',':
line += trace_string_formatted[j]
j += 1
state = 3
i = j
elif state == 3:
j = i + 4
func = ''
while j < len(trace_string_formatted) and trace_string_formatted[j] != ' ':
func += trace_string_formatted[j]
j += 1
state = 4
trace_string_formatted2 += f'/{func}:{line}, '
line = ''
elif state == 4:
if trace_string_formatted[i] == ';': # TODO: you can't have a semicolon in the python
state = 0
i += 1
lt = trace_string_formatted2[:-2].split(", ")
trace_string_formatted3 = str(lt[-3]).replace('.py', '')
return trace_string_formatted3