-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathlogger.py
265 lines (212 loc) · 8.93 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
from datetime import datetime
from uicard import server
import logging
from trame.widgets import vuetify, markdown
from pathlib import Path
BASE = Path(__file__).parent
# Extract state and controller from the server
state, ctrl = server.state, server.controller
state.show_error_dialog_card = False
state.show_warn_dialog_card = False
state.md_content = ""
state.su2_logs = ""
state.error_msg = ""
state.warn_msg = ""
state.last_modified_su2_log_len = 0
state.last_modified_su2gui_log_len = 0
state.case_name = "new_case"
#################### LOGGING HANDLER ####################
# Configure the root logger to suppress logs from other modules
logging.basicConfig(
filename=f"{BASE}/user/su2gui.log",
level=logging.WARNING, # Suppress logs at levels lower than WARNING
format="%(asctime)s - %(levelname)s - %(message)s"
)
# Get the logger for the current module
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG) # Allow DEBUG logs for this module
# Ensure the logger uses the same formatter as the root logger
formatter = logging.Formatter("%(asctime)s - %(levelname)s - %(message)s")
for handler in logger.handlers:
handler.setFormatter(formatter)
class CustomHandler(logging.Handler):
def emit(self, record):
log_entry = self.format(record)
if record.levelno == logging.ERROR:
handle_error(log_entry)
if record.levelno == logging.WARN:
handle_warn(log_entry)
add_new_logs(log_entry)
# Add the custom handler to the root logger
custom_handler = CustomHandler()
custom_handler.setFormatter(formatter)
logging.getLogger().addHandler(custom_handler)
#################### LOGS -> SU2GUI TAB ####################
def log(type :str, message, **kwargs):
"""
Appends a message to the "su2gui.log" file and LOGS Tab.
Args:
type (str) : Type of message to be appended, like INFO, ERROR, WARN, etc - Any
message (str): The message to append to the log.
detail (str) : Details of message, Not necessary
"""
message = str(message)
message += " \n"
if "detail" in kwargs:
message+=kwargs.get("detail") + " \n"
if type.upper() == "INFO":
logger.info(message)
elif type.upper() == "WARN":
logger.warning(message)
elif type.upper() == "ERROR":
logger.error(message)
find_error_message(message)
elif type.upper() == "DEBUG":
logger.debug(message)
# Add new logs to the markdown content in LOGS -> SU2GUI Tab
def add_new_logs(msg):
state.md_content = (msg + state.md_content[:25000])
# Clear previous logs in the LOGS -> SU2GUI Tab and start fresh
def clear_logs():
with open(BASE / "user" / state.case_name / 'su2gui.log', 'w') as f:
f.write('')
state.last_modified_su2gui_log_len = 0
state.su2_logs = ""
state.show_error_dialog_card = False
# Handle the error message for LOGS -> SU2GUI Tab
def handle_error(error_message):
# Display the error message in the error dialog card
# set the char limit to 20000 to avoid overflow
if state.show_error_dialog_card == False:
state.error_msg = error_message[:20000]
state.show_error_dialog_card = True
else:
state.error_msg=(state.error_msg+error_message)[:20000]
# Also Printing the error message in terminal
print(f"{error_message}")
# Handle the warning message for LOGS -> SU2GUI Tab
def handle_warn(warn_message):
# Display the warning message in the warning dialog card
# set the char limit to 20000 to avoid overflow
if state.show_warn_dialog_card == False:
state.warn_msg = warn_message[:20000]
state.show_warn_dialog_card = True
else:
state.warn_msg=(state.warn_msg+warn_message)[:20000]
# Also printing the warning message in terminal
print(f"{warn_message}")
@ctrl.trigger("download_su2gui_log")
def download_su2gui_log():
with open(BASE / "user" / 'su2gui.log', 'r') as f:
return f.read()
#################### LOGS -> SU2 TAB ####################
# Update the SU2 logs in the LOGS -> SU2 Tab
# called by asyn function start_countdown in solver.py
def update_su2_logs():
file = BASE / "user" / state.case_name / 'su2.out'
try:
with open(file, 'r') as f:
# Move the file pointer to the last read position
f.seek(state.last_modified_su2_log_len)
# Read the new content
new_logs = f.read()
# Update the last modified log length
state.last_modified_su2_log_len += len(new_logs)
# Update the state logs with the new content
state.su2_logs = "```" + (state.su2_logs[3:-3] + new_logs)[-25000:] + "```"
# Check for error messages in the new logs
find_error_message(new_logs)
except FileNotFoundError:
state.su2_logs = "# File not found"
# find the error message in the su2 log file
# and display it in the error dialog card
def find_error_message(msg):
error_found = False
error_lines = []
for line in msg.splitlines():
if len(error_lines)>10:
break
if error_found:
error_lines.append(line.strip())
elif "error" in line.lower().split(' '):
error_lines.append(line.strip())
error_found = True
if error_lines:
error_message = "\n".join(error_lines)
# Display the error message in the error dialog card
# set the char limit to 20000 to avoid overflow
if state.show_error_dialog_card == False:
state.error_msg = error_message[:20000]
state.show_error_dialog_card = True
else:
state.error_msg=(state.error_msg+error_message)[:20000]
return error_found
#################### DIALOG CARDS ####################
# popup window for Error messages
def Error_dialog_card():
with vuetify.VDialog(width=800,position='{X:10,Y:10}',transition="dialog-top-transition",v_model=("show_error_dialog_card",False)):
with vuetify.VCard():
vuetify.VCardTitle("ERROR",
classes="grey lighten-1 py-1 grey--text text--darken-3")
with vuetify.VContainer(fluid=True):
markdown.Markdown(
content = ('error_msg', state.error_msg),
style = "padding: 3rem; color: Red; background-color: white"
)
vuetify.VBtn("Close",click=(hide_error_dialog_card)
)
def hide_error_dialog_card():
state.show_error_dialog_card = False
state.error_msg = ""
# popup window for Warning messages
def Warn_dialog_card():
with vuetify.VDialog(width=800,position='{X:10,Y:10}',transition="dialog-top-transition",v_model=("show_warn_dialog_card",False)):
with vuetify.VCard():
vuetify.VCardTitle("WARNING",
classes="grey lighten-1 py-1 grey--text text--darken-3")
with vuetify.VContainer(fluid=True):
markdown.Markdown(
content = ('warn_msg', state.warn_msg),
style = "padding: 3rem; color: #ffcc00; background-color: white"
)
vuetify.VBtn("Close",click=(hide_warn_dialog_card)
)
def hide_warn_dialog_card():
state.show_warn_dialog_card = False
state.warn_msg = ""
#################### LOGS TAB ####################
def logs_tab():
with vuetify.VTabItem(
value=(3,), style="width: 100%; height: 100%;"
):
# with vuetify.VTabs(v_model=("log_tab", 0), right=True):
with vuetify.VTabs(v_model=("log_tab", 0)):
vuetify.VTab("SU2GUI")
vuetify.VTab("SU2")
with vuetify.VContainer(
fluid=True,
classes="pa-0 fill-height",
style="position: relative;"
):
with vuetify.VTabsItems(
value=("log_tab",), style="width: 100%; height: 100%;"
):
with vuetify.VTabItem(
value=(0,), style="width: 100%; height: 100%;"
):
with vuetify.VBtn("Logs",
click = f"utils.download('su2gui.log', trigger('download_su2gui_log'), 'text/plain')",
style="margin-left: 80%;"):
vuetify.VIcon("mdi-arrow-down-bold-box-outline")
markdown.Markdown(
content = ('md_content', state.md_content),
style = "padding: 0.5rem 3rem; color: black; background-color: white"
)
with vuetify.VTabItem(
value=(1,), style="width: 100%; height: 100%;"
):
markdown.Markdown(
content = ('su2_logs', state.su2_logs),
style = "padding: 3rem; color: black; background-color: white",
hide_details = True
)