2023-11-04 20:37:35 -05:00
|
|
|
#!/usr/bin/python3
|
|
|
|
from datetime import datetime as dt
|
|
|
|
from sys import stdout, stderr
|
2023-11-08 21:20:50 -06:00
|
|
|
from typing import Union
|
2023-11-04 20:37:35 -05:00
|
|
|
|
|
|
|
|
|
|
|
def log(
|
2023-11-09 15:37:08 -06:00
|
|
|
message: str,
|
|
|
|
origin: str = "Unknown",
|
|
|
|
level: str = "LOG",
|
|
|
|
time: Union[dt, str] = "now",
|
2023-11-04 20:37:35 -05:00
|
|
|
) -> None:
|
2024-03-06 12:33:04 -06:00
|
|
|
if level in ["EXIT", "CRASH", "FATAL"]:
|
2023-11-04 20:37:35 -05:00
|
|
|
stream = stderr
|
|
|
|
else:
|
|
|
|
stream = stdout
|
2023-11-04 22:55:26 -05:00
|
|
|
if time == "now":
|
2023-11-13 15:42:46 -06:00
|
|
|
dtime = dt.now()
|
2023-11-14 15:43:41 -06:00
|
|
|
elif type(time) == str:
|
2023-11-14 23:26:02 -06:00
|
|
|
raise ValueError('Only "now" is an accepted string argument for time')
|
2023-11-17 23:23:46 -06:00
|
|
|
elif type(time) == dt:
|
|
|
|
dtime = time
|
2023-11-13 15:42:46 -06:00
|
|
|
else:
|
2023-11-17 23:23:46 -06:00
|
|
|
raise ValueError("time must either be a string or a dt object")
|
2023-11-13 15:42:46 -06:00
|
|
|
time = dtime.strftime("%H:%M:%S")
|
2023-11-04 20:37:35 -05:00
|
|
|
if not "\n" in message:
|
2023-11-07 21:58:11 -06:00
|
|
|
print(f"[{level}][{origin}][{time}] {message}", file=stream)
|
2023-11-04 20:37:35 -05:00
|
|
|
else:
|
|
|
|
for line in message.split("\n"):
|
2023-11-07 21:58:11 -06:00
|
|
|
print(f"[{level}][{origin}][{time}] {line}", file=stream)
|