diff --git a/main.py b/main.py index d836f7c..e0b0137 100755 --- a/main.py +++ b/main.py @@ -2,6 +2,7 @@ from collections import namedtuple from dataclasses import dataclass +from functools import partial, partialmethod from pathlib import Path import argparse import json @@ -12,10 +13,17 @@ import sys import typing as t +logging.TRACE = 5 +logging.addLevelName(logging.TRACE, "TRACE") +logging.Logger.trace = partialmethod(logging.Logger.log, logging.TRACE) +logging.trace = partial(logging.log, logging.TRACE) + + class LogFormatter(logging.Formatter): format = "%(name)s [%(levelname)s] %(message)s" FORMATS = { + logging.TRACE: f"\x1b[30;20m{format}\x1b[0m", logging.DEBUG: f"\x1b[38;20m{format}\x1b[0m", logging.INFO: f"\x1b[34;20m{format}\x1b[0m", logging.WARNING: f"\x1b[33;20m{format}\x1b[0m", @@ -108,6 +116,8 @@ def parse_log(game: str, log: str, into: sqlite3.Connection): if not chunks: continue + LOG.trace(line) + match chunks: case ["player", id, username, *rest]: players[id] = username @@ -247,14 +257,15 @@ def main(): parser = argparse.ArgumentParser( prog=APP, description="extracts stats from a Showdown replay" ) - parser.add_argument( - "-v", "--verbose", action="store_true", help="add debugging info" - ) + parser.add_argument("-v", "--verbose", action="count", help="add debugging info") parser.add_argument("-c", "--cache", action="store_true", help="cache replays") parser.add_argument("replay", nargs="+", help="replay ID or URL") args = parser.parse_args(sys.argv[1:]) - LOG.setLevel(logging.DEBUG if args.verbose else logging.INFO) + if args.verbose > 1: + LOG.setLevel(logging.TRACE) + elif args.verbose > 0: + LOG.setLevel(logging.DEBUG) try: db = sqlite3.connect("data.db")