From 777eb6e869d242d225cd752301bc91f4341b241d Mon Sep 17 00:00:00 2001 From: M Bussonnier Date: Tue, 29 Oct 2024 14:50:51 +0100 Subject: [PATCH] add log.py --- log.py | 92 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 log.py diff --git a/log.py b/log.py new file mode 100644 index 00000000..49ab3590 --- /dev/null +++ b/log.py @@ -0,0 +1,92 @@ +import sys +import json + +from dataclasses import dataclass +from pathlib import Path + +from typing import Optional, Any + +from rich.console import Console + +console = Console() + + +@dataclass +class Entry: + taskName: Any + stack_info: Any + filename: str + funcName: str + levelname: str + module: str + name: str + pathname: str + process: int + lineno: int + thread: int + processName: str + threadName: str + message: str + time: str + msg: str + + @property + def relpath(self): + cwd = Path(".").expanduser().absolute() + return "./" + str(Path(self.pathname).relative_to(Path(cwd))) + + @property + def fm(self): + pass + + +ignore = """ +_importdb.py:563 +_importdb.py:345 +_autoimp.py:513 + +""" + +ign = ignore.strip().splitlines() + +igns = [] +nig = [] + +for line in open("logpipe", "r"): + from time import sleep + + line = line.strip() # Remove any leading/trailing whitespace + + if not line: + sleep(0.01) + continue + try: + # Attempt to parse the line as JSON + parsed_json = json.loads(line) + + e = Entry(**parsed_json) + tag = f"{e.filename}:{e.lineno}" + if tag in ign: + igns.append(tag) + continue + + nig.append(tag) + + fmess = f"{e.relpath:>34}:{e.lineno:>4} {e.funcName:>25}: {e.message}" + try: + console.print(fmess) + + except: + print(fmess) + except json.JSONDecodeError: + # If parsing fails, print the line as-is + pass + print(line) + +from collections import Counter + +for k, v in Counter(igns).items(): + print(k, v) +print("Not ignored") +for k, v in Counter(nig).most_common(10): + print(k, v)