forked from Mirror/pmbootstrap
core: move args.log to context.log (MR 2252)
Signed-off-by: Caleb Connolly <caleb@postmarketos.org>
This commit is contained in:
parent
838beb270d
commit
02f04ba3a8
6 changed files with 28 additions and 15 deletions
|
@ -5,6 +5,8 @@ import os
|
|||
import sys
|
||||
from typing import TextIO
|
||||
import pmb.config
|
||||
from pmb.core import get_context
|
||||
from pmb.core.context import Context
|
||||
from pmb.core.types import PmbArgs
|
||||
|
||||
logfd: TextIO
|
||||
|
@ -21,19 +23,19 @@ VERBOSE = 5
|
|||
|
||||
class log_handler(logging.StreamHandler):
|
||||
"""Write to stdout and to the already opened log file."""
|
||||
_args: PmbArgs
|
||||
context: Context
|
||||
|
||||
def __init__(self, args: PmbArgs):
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
self._args = args
|
||||
self.context = get_context()
|
||||
|
||||
def emit(self, record):
|
||||
try:
|
||||
msg = self.format(record)
|
||||
|
||||
# INFO or higher: Write to stdout
|
||||
if (not self._args.details_to_stdout and
|
||||
not self._args.quiet and
|
||||
if (not self.context.details_to_stdout and
|
||||
not self.context.quiet and
|
||||
record.levelno >= logging.INFO):
|
||||
stream = self.stream
|
||||
|
||||
|
@ -106,18 +108,20 @@ def add_verbose_log_level():
|
|||
def init(args: PmbArgs):
|
||||
"""Set log format and add the log file descriptor to logfd, add the verbose log level."""
|
||||
global logfd
|
||||
context = pmb.core.get_context()
|
||||
# Set log file descriptor (logfd)
|
||||
if args.details_to_stdout:
|
||||
if context.details_to_stdout:
|
||||
logfd = sys.stdout
|
||||
else:
|
||||
# Require containing directory to exist (so we don't create the work
|
||||
# folder and break the folder migration logic, which needs to set the
|
||||
# version upon creation)
|
||||
dir = os.path.dirname(args.log)
|
||||
dir = os.path.dirname(context.log)
|
||||
if os.path.exists(dir):
|
||||
logfd = open(args.log, "a+")
|
||||
logfd = open(context.log, "a+")
|
||||
else:
|
||||
logfd = open(os.devnull, "a+")
|
||||
# FIXME: what? surely this check shouldn't be needed!
|
||||
if args.action != "init":
|
||||
print(f"WARNING: Can't create log file in '{dir}', path"
|
||||
" does not exist!")
|
||||
|
@ -135,7 +139,7 @@ def init(args: PmbArgs):
|
|||
root_logger.setLevel(VERBOSE)
|
||||
|
||||
# Add a custom log handler
|
||||
handler = log_handler(args)
|
||||
handler = log_handler()
|
||||
handler.setFormatter(formatter)
|
||||
root_logger.addHandler(handler)
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue