Even more structured logging

This commit is contained in:
hedy 2024-04-06 07:41:17 +00:00
parent 59aa586532
commit 243ecd4638
1 changed files with 28 additions and 13 deletions

41
main.py
View File

@ -16,15 +16,17 @@ import config as cfg
## LOGGING ##
f_log = open(cfg.log_file, "w+")
f_log = None
def log(msg: str, also_print: bool = False):
f_log.write(f"{msg}\n")
def log(type_: str, msg: str, also_print: bool = False):
"""Log a message, optionally echoed to stdout, with a type prefix."""
f_log.write(f"[{type_}] {msg}\n")
if also_print:
print(msg)
def manual_log(msg: str, *args, **kwargs):
log(f"[MANUAL] {msg}", *args, **kwargs)
def manual_log(type_: str, msg: str, *args, **kwargs):
"""Manually log a message with given type."""
log("manual", f"({type_}) {msg}", *args, **kwargs)
## UTILS ##
@ -53,21 +55,22 @@ class Server(Ir.Server):
self.params = params
await self.handshake()
except:
manual_log("connection with {} failed, disconnecting".format(self.name), True)
manual_log("ERROR", "connection with {} failed, disconnecting".format(self.name), also_print=True)
self.disconnected = True
def auto_log(self, msg, *args, **kwargs):
log(f"[{self.name}] {msg}", *args, **kwargs)
def auto_log(self, msg: str, *args, **kwargs):
"""Log an automated message with server name as info."""
log(self.name, msg, *args, **kwargs)
async def line_read(self, line: It.line):
self.auto_log(f"< {line.format()}")
if line.command == "001":
manual_log(f"connected to {self.name} :D", True)
manual_log("status", f"connected to {self.name} :D", True)
self.chans = cfg.SERVERS[self.name]["chans"]
self.chans_actual = []
for c in self.chans:
await self.send(It.build("JOIN", [c]))
manual_log(f"joined {self.name} {c} through config", True)
manual_log("status", f"joined {self.name} {c} through config", True)
self.chans_actual.append(c)
elif line.command == "PRIVMSG" and line.params[0] == self.nickname:
@ -142,7 +145,7 @@ class Server(Ir.Server):
if line.command == "INVITE":
await self.send(It.build("JOIN", [line.params[1]]))
manual_log(f"joined {self.name} {line.params[1]} through invite", True)
manual_log("status", f"joined {self.name} {line.params[1]} through invite", True)
# TODO: add to relay chans if needed
self.chans.append(line.params[1])
self.chans_actual.append(line.params[1])
@ -151,22 +154,32 @@ class Server(Ir.Server):
self.auto_log(f"> {line.format()}")
async def bc(self, name, chan, nick, msg, hide):
manual_log(f"<RELAY> {self.name}{chan}@{nick}{'(hidechan)' if hide else ''}: {msg}", True)
"""Handle a relay operation."""
manual_log("relay", f"{self.name}{chan}@{nick}{'(hidechan)' if hide else ''}: {msg}", True)
if self.disconnected or "chans" not in list(dir(self)):
return
if name == self.name and len(cfg.SERVERS[name]["chans"]) == 1:
return
for c in self.chans_actual:
s = f"\x0f\x0f\x0f\x0f<{nick[:1]}\u200b{nick[1:]}/{name}"
if not hide:
s += f"{chan}"
s += f"> {msg}"
await self.send(It.build("PRIVMSG", [c, s]))
async def ac(self, name, args):
manual_log(f"<ADMIN_CMD> {self.name}: {args}", True)
"""Handle an admin command"""
manual_log("admin-command", f"{self.name}: {args}", True)
if self.disconnected or "chans" not in list(dir(self)):
return
nargs = []
isComb = False
for arg in args:
@ -178,6 +191,7 @@ class Server(Ir.Server):
nargs[-1] += " " + arg
else:
nargs.append(arg)
await self.send(It.build(nargs[0], nargs[1:])) # TODO: loop over chans
@ -209,4 +223,5 @@ async def main():
if __name__ == "__main__":
f_log = open(cfg.log_file, "w+")
asyncio.run(main())