lib: More result log beautification
This commit adds more tweaks to shell command output in order to make it nicer. The biggest patch is in Result.__summarize(), which makes it more versatile, and allows removal of some code in SSHClient. App sees some independent, minor result format beautification. Signed-off-by: Jan Lindemann <jan@janware.com>
This commit is contained in:
parent
869bef2c06
commit
1e0dee5908
3 changed files with 60 additions and 37 deletions
|
|
@ -3,13 +3,12 @@ from __future__ import annotations
|
|||
import abc
|
||||
import os
|
||||
import pwd
|
||||
import sys
|
||||
|
||||
from enum import Flag, auto
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
from ..ExecContext import ExecContext
|
||||
from ..log import DEBUG, ERR, INFO, NOTICE, WARNING, log
|
||||
from ..log import DEBUG, ERR, INFO, NOTICE, get_caller_pos, log, log_m
|
||||
from ..Uri import Uri
|
||||
|
||||
if TYPE_CHECKING:
|
||||
|
|
@ -54,24 +53,18 @@ class SSHClient(ExecContext):
|
|||
log_prefix: str,
|
||||
) -> Result:
|
||||
|
||||
def __log(prio: int, *args):
|
||||
log(prio, log_prefix, *args)
|
||||
def __log(prio: int, *args, **kwargs):
|
||||
caller = kwargs.get('caller')
|
||||
if caller is None:
|
||||
kwargs['caller'] = get_caller_pos(1)
|
||||
log(prio, log_prefix, *args, **kwargs)
|
||||
|
||||
def __log_block(prio: int, title: str, block: bytes | str | None):
|
||||
def __log_block(prio: int, title: str, block: str | None):
|
||||
if self.__caps & self.Caps.LogOutput:
|
||||
return
|
||||
if not block:
|
||||
if block is None:
|
||||
return
|
||||
if isinstance(block, bytes):
|
||||
encoding = sys.stdout.encoding or 'utf-8'
|
||||
block = block.decode(encoding).strip()
|
||||
# Needed to pacify pyright: block can't be anything else at this point
|
||||
assert isinstance(block, str)
|
||||
delim = f'---- {title} ----'
|
||||
__log(prio, f',{delim}')
|
||||
for line in block.splitlines():
|
||||
__log(prio, '|', line)
|
||||
__log(prio, f'`{delim}')
|
||||
log_m(prio, f'---- {title} ----\n{block}', caller = get_caller_pos(1))
|
||||
|
||||
if wd is not None and not self.__caps & self.Caps.Wd:
|
||||
cmd = ['cd', wd, '&&', *cmd]
|
||||
|
|
@ -97,8 +90,6 @@ class SSHClient(ExecContext):
|
|||
if verbose:
|
||||
__log_block(NOTICE, 'stdout', ret.stdout_str_or_none)
|
||||
__log_block(NOTICE, 'stderr', ret.stderr_str_or_none)
|
||||
if ret.status != 0:
|
||||
__log(WARNING, f'Exit code {ret.status}')
|
||||
|
||||
return ret
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue