Skip to content

Commit

Permalink
python: setup colorlog with same log level as in C++
Browse files Browse the repository at this point in the history
  • Loading branch information
ManuelHu committed Jan 29, 2025
1 parent 9d7f1bc commit 4bbbca2
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ classifiers = [
dynamic = ["version"]
dependencies = [
"legend-pydataobj",
"colorlog",
]

[project.optional-dependencies]
Expand Down
38 changes: 35 additions & 3 deletions python/remage/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import threading
from pathlib import Path

import colorlog

from .cpp_config import REMAGE_CPP_EXE_PATH
from .ipc import ipc_thread_fn

Expand Down Expand Up @@ -83,17 +85,47 @@ def new_signal_handler(sig: int, _):
return ec, unhandled_ipc_messages


def _setup_log() -> None:
"""Setup a colored logger for this package."""

logger = logging.getLogger("remage")

if sys.stderr.isatty():
fmt = "%(log_color)s[%(levelname)s %(name)s%(reset)s %(message)s"

handler = colorlog.StreamHandler()
handler.setFormatter(colorlog.ColoredFormatter(fmt))
logger.addHandler(handler)

logger.setLevel(logging.DEBUG)

return logger


def remage_cli() -> None:
logger = _setup_log()

ec, ipc_info = _run_remage_cpp()
if ec not in [0, 2]:
# remage had an error (::fatal -> ec==134 (SIGABRT); ::error -> ec==1)
# ec==2 is just a warning, continue in the execution flow.
sys.exit(ec)

_log_level = next(
msg[1] for msg in ipc_info if len(msg) == 2 and msg[0] == "loglevel"
# setup logging based on log level from C++.
log_level = next(
(msg[1] for msg in ipc_info if len(msg) == 2 and msg[0] == "loglevel"),
"summary",
)
# TODO: setup logging based on _log_level
levels_rmg_to_py = {
"debug": logging.DEBUG,
"detail": logging.INFO,
"summary": logging.INFO,
"warning": logging.WARNING,
"error": logging.ERROR,
"fatal": logging.CRITICAL,
"nothing": logging.CRITICAL,
}
logger.setLevel(levels_rmg_to_py[log_level])

# TODO: further post-processing
output_files = [msg[1] for msg in ipc_info if len(msg) == 2 and msg[0] == "output"]
Expand Down

0 comments on commit 4bbbca2

Please sign in to comment.