Skip to content

Commit

Permalink
Copy log setup from EverybodyCodes to AoC and add a log function to t…
Browse files Browse the repository at this point in the history
…he template
  • Loading branch information
IsaacG committed Feb 5, 2025
1 parent ce9bdf0 commit 9d33266
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
24 changes: 24 additions & 0 deletions runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import dataclasses
import datetime
import importlib
import logging
import multiprocessing
import os
import pathlib
Expand All @@ -28,6 +29,20 @@
NUMBERS = "zero one two three four five six seven eight nine ten".split()


class LogFormatter(logging.Formatter):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.last_call = time.perf_counter_ns()

def formatTime(self, record, datefmt=None):
if datefmt:
return super().formatTime(record, datefmt)
call_time = time.perf_counter_ns()
delta = call_time - self.last_call
self.last_call = call_time
return datetime.datetime.now().strftime(f"%H:%M:%S.%f ({delta // 1_000_000:5}ms)")


@dataclasses.dataclass
class ChallengeRunner:
"""Manage the challenge for a single, specific day."""
Expand Down Expand Up @@ -374,6 +389,7 @@ def show_parsers():
@click.option("--timeout", type=int, default=30, help="Set the timeout.")
@click.option("--input-file", "--input", "--file", type=str, default=None, help="Alternative input file.")
@click.option("--cookie", type=str, required=False, help="Set cookie")
@click.option("--verbose", "-v", count=True)
def main(
date: Optional[str],
day: Optional[int],
Expand All @@ -392,9 +408,17 @@ def main(
timeout: int,
year: Optional[int],
cookie: Optional[str],
verbose: int,
) -> None:
"""Run the code in some fashion."""
os.nice(19)

log_level = [logging.WARN, logging.INFO, logging.DEBUG][min(2, verbose)]
handler = logging.StreamHandler()
handler.setFormatter(LogFormatter(fmt="%(asctime)s [%(funcName)s():L%(lineno)s] %(message)s"))
logging.getLogger().addHandler(handler)
logging.getLogger().setLevel(log_level)

try:
resource.setrlimit(resource.RLIMIT_RSS, (int(10e9), int(100e9)))
except ValueError:
Expand Down
4 changes: 4 additions & 0 deletions shared/tmpl.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import collections
import functools
import itertools
import logging
import math
import re

Expand All @@ -16,6 +17,9 @@
InputType = list[LineType]


def log(*args, **kwargs) -> None:
logging.info(*args, **kwargs)

class Day${day}(aoc.Challenge):
"""${title}."""

Expand Down

0 comments on commit 9d33266

Please sign in to comment.