Skip to content
This repository was archived by the owner on Jan 16, 2026. It is now read-only.

Commit 1cbf614

Browse files
committed
Refactor StreamRecorder to use asyncio.run for updating recording status; enhance LogFileHandler to serve logs as HTML with syntax highlighting
1 parent 23cd87d commit 1cbf614

1 file changed

Lines changed: 33 additions & 5 deletions

File tree

main.py

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ def remove_stream(self, host: str):
293293
)
294294
del self.active_streams[host]
295295
if not list(self.active_streams.keys()):
296-
self.loop.create_task(self.update_recording_status())
296+
asyncio.run(self.update_recording_status())
297297

298298
def run(self):
299299
self.send_notification()
@@ -343,7 +343,7 @@ def run(self):
343343
if host not in active_hosts:
344344
self.remove_stream(host)
345345

346-
self.loop.create_task(self.update_recording_status())
346+
asyncio.run(self.update_recording_status())
347347

348348
time.sleep(15)
349349
except Exception as e:
@@ -387,7 +387,7 @@ async def update_recording_status(self):
387387
SET link = EXCLUDED.link,
388388
length = EXCLUDED.length,
389389
description = EXCLUDED.description,
390-
starting_time = EXCLUDED.starting_time;
390+
starting_time = EXCLUDED.starting_time,
391391
last_updated = NOW();
392392
"""
393393
await conn.execute(query, host, link, length, description, starting_time)
@@ -418,13 +418,41 @@ def get(self, filename: str):
418418
safe_filename = os.path.basename(filename)
419419
file_path = os.path.join("logs", safe_filename)
420420
if os.path.exists(file_path) and file_path.endswith(".log"):
421-
self.set_header("Content-Type", "text/plain; charset=utf-8")
421+
self.set_header("Content-Type", "text/html; charset=utf-8")
422+
self.write("<html><head><style>")
423+
self.write("""
424+
body { font-family: monospace; background: #121212; color: #eee; padding: 1rem; }
425+
.error { color: red; }
426+
.info { color: cyan; }
427+
.debug { color: gray; }
428+
.warn { color: orange; }
429+
.timestamp { color: #888; }
430+
""")
431+
self.write("</style></head><body><pre>")
432+
422433
with open(file_path, "r", encoding="utf-8") as f:
423-
self.write(f.read())
434+
for line in f:
435+
formatted = line
436+
# Highlight levels
437+
formatted = formatted.replace("[ERROR]", '<span class="error">[ERROR]</span>')
438+
formatted = formatted.replace("[INFO]", '<span class="info">[INFO]</span>')
439+
formatted = formatted.replace("[DEBUG]", '<span class="debug">[DEBUG]</span>')
440+
formatted = formatted.replace("[WARNING]", '<span class="warn">[WARN]</span>')
441+
442+
# Optional: Timestamp coloring (assumes timestamp at line start)
443+
if line.startswith("[") and "]" in line:
444+
timestamp_end = line.find("]")
445+
ts = line[0:timestamp_end+1]
446+
rest = line[timestamp_end+1:]
447+
formatted = f'<span class="timestamp">{ts}</span>{rest}'
448+
449+
self.write(formatted)
450+
self.write("</pre></body></html>")
424451
else:
425452
self.set_status(404)
426453
self.write("File not found.")
427454
app_log.error(f"File not found: {file_path}")
455+
428456
else:
429457
self.set_status(404)
430458
self.write("File not found.")

0 commit comments

Comments
 (0)