100th commit! (Add timestamps to logs, and fix long-standing bug with log to err)

This commit is contained in:
Firepup Sixfifty 2023-11-07 21:58:11 -06:00
parent 6626781dff
commit f876ea3ab1
Signed by: Firepup650
GPG key ID: 7C92E2ABBBFAB9BA
3 changed files with 12 additions and 6 deletions

View file

@ -1,9 +1,7 @@
#!/usr/bin/python3 #!/usr/bin/python3
from os import system from os import system
from time import sleep from time import sleep
from sys import argv as args
from threading import Thread from threading import Thread
from datetime import datetime as dt
from logs import log from logs import log

View file

@ -1,10 +1,18 @@
#!/usr/bin/python3 #!/usr/bin/python3
from bot import bot from bot import bot
from sys import argv as args from sys import argv as args
from traceback import format_exc
from logs import log
server = args[1] if args else "UNSTABLE BOT MODE" server = args[1] if args else "UNSTABLE BOT MODE"
if __name__ == "__main__": if __name__ == "__main__":
instance = bot(server) instance = bot(server)
instance.mainloop() try:
instance.mainloop()
except Exception:
Err = format_exc()
for line in Err.split("\n"):
log(line, server, "CRASH")
exit(-1)

View file

@ -6,14 +6,14 @@ from sys import stdout, stderr
def log( def log(
message: str, origin: str = "Unknown", level: str = "LOG", time: dt = "now" message: str, origin: str = "Unknown", level: str = "LOG", time: dt = "now"
) -> None: ) -> None:
if level == "EXIT": if level in ["EXIT", "CRASH"]:
stream = stderr stream = stderr
else: else:
stream = stdout stream = stdout
if time == "now": if time == "now":
time = dt.now() time = dt.now()
if not "\n" in message: if not "\n" in message:
print(f"[{level}][{origin}][{time}] {message}") print(f"[{level}][{origin}][{time}] {message}", file=stream)
else: else:
for line in message.split("\n"): for line in message.split("\n"):
print(f"[{level}][{origin}][{time}] {line}") print(f"[{level}][{origin}][{time}] {line}", file=stream)