diff options
author | uael <uael@google.com> | 2023-07-27 22:02:37 +0000 |
---|---|---|
committer | Sparkling Diva <30467496+SilverBzH@users.noreply.github.com> | 2023-07-27 15:06:29 -0700 |
commit | 6c5fd54e043038aa93bd4e293851174567ebac4d (patch) | |
tree | 2df20204d79810c6484543ed90012cd6294f9786 /avatar | |
parent | d9ea83bffff4858dc483bd209ecb10023efc6f9b (diff) | |
download | avatar-6c5fd54e043038aa93bd4e293851174567ebac4d.tar.gz |
logging: improve logging
Diffstat (limited to 'avatar')
-rw-r--r-- | avatar/metrics/trace.py | 28 | ||||
-rw-r--r-- | avatar/pandora_client.py | 2 |
2 files changed, 22 insertions, 8 deletions
diff --git a/avatar/metrics/trace.py b/avatar/metrics/trace.py index 85f5249..6851367 100644 --- a/avatar/metrics/trace.py +++ b/avatar/metrics/trace.py @@ -121,12 +121,15 @@ class Callsite(AsTrace): device.log.info(f"{self}") - def __str__(self) -> str: + def pretty(self) -> str: name_pretty = self.name[1:].replace('/', '.') if self.message is None: return f"%{self.id} {name_pretty}" message_pretty, _ = debug_message(self.message) - return f"%{self.id} {name_pretty}({message_pretty})" + return f"{name_pretty}({message_pretty})" + + def __str__(self) -> str: + return f"{str2color('╭──', self.id)} {self.pretty()}" def output(self, message: Any) -> None: self.events.append(CallOutput(self, message)) @@ -169,7 +172,7 @@ class CallEvent(AsTrace): callsite.device.log.info(f"{self}") def __str__(self) -> str: - return "└── " + self.stringify('->') + return f"{str2color('╰──', self.callsite.id)} {self.stringify('──→')}" def as_trace(self) -> TracePacket: return TracePacket( @@ -189,12 +192,15 @@ class CallEvent(AsTrace): def stringify(self, direction: str) -> str: message_pretty = "" if self.message is None else debug_message(self.message)[0] - return f"[{(self.at - self.callsite.at) / 1000000000:.3f}s] {self.callsite} {direction} ({message_pretty})" + return ( + str2color(f"[{(self.at - self.callsite.at) / 1000000000:.3f}s]", self.callsite.id) + + f" {self.callsite.pretty()} {str2color(direction, self.callsite.id)} ({message_pretty})" + ) class CallOutput(CallEvent): def __str__(self) -> str: - return "├── " + self.stringify('->') + return f"{str2color('├──', self.callsite.id)} {self.stringify('──→')}" def as_trace(self) -> TracePacket: return super().as_trace() @@ -202,7 +208,7 @@ class CallOutput(CallEvent): class CallInput(CallEvent): def __str__(self) -> str: - return "├── " + self.stringify('<-') + return f"{str2color('├──', self.callsite.id)} {self.stringify('←──')}" def as_trace(self) -> TracePacket: return super().as_trace() @@ -210,7 +216,7 @@ class CallInput(CallEvent): class CallEnd(CallEvent): def __str__(self) -> str: - return "└── " + self.stringify('->') + return f"{str2color('╰──', self.callsite.id)} {self.stringify('──→')}" def as_trace(self) -> TracePacket: return TracePacket( @@ -268,3 +274,11 @@ def debug_message(msg: message.Message) -> Tuple[Dict[str, Any], List[DebugAnnot json[f.name] = json_entry dbga.append(DebugAnnotation(name=f.name, **dbga_entry)) return json, dbga + + +def str2color(s: str, id: int) -> str: + CSI = "\x1b[" + CSI_RESET = CSI + "0m" + CSI_BOLD = CSI + "1m" + color = ((id * 10) % (230 - 17)) + 17 + return CSI + ("1;38;5;%dm" % color) + CSI_BOLD + s + CSI_RESET diff --git a/avatar/pandora_client.py b/avatar/pandora_client.py index f0f9e58..60689aa 100644 --- a/avatar/pandora_client.py +++ b/avatar/pandora_client.py @@ -184,7 +184,7 @@ class PandoraClientLoggerAdapter(logging.LoggerAdapter): # type: ignore client = self.extra['client'] assert isinstance(client, PandoraClient) addr = ':'.join([f'{x:02X}' for x in client.address[4:]]) - return (f'[{client.name}:{addr}] {msg}', kwargs) + return (f'[{client.name:<8}:{addr}] {msg}', kwargs) class BumblePandoraClient(PandoraClient): |