mirror of
https://github.com/isjerryxiao/pacroller.git
synced 2024-11-15 04:42:24 +08:00
better stdout log
This commit is contained in:
parent
e6a39f0cec
commit
76b13e8e16
2 changed files with 17 additions and 8 deletions
|
@ -3,6 +3,7 @@
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
import subprocess
|
import subprocess
|
||||||
import logging
|
import logging
|
||||||
|
import logging.handlers
|
||||||
from re import match
|
from re import match
|
||||||
import json
|
import json
|
||||||
from os import environ, getuid, isatty
|
from os import environ, getuid, isatty
|
||||||
|
@ -130,6 +131,20 @@ def do_system_upgrade(debug=False, interactive=False) -> checkReport:
|
||||||
else:
|
else:
|
||||||
raise MaxRetryReached(f'sync failed {NETWORK_RETRY} times')
|
raise MaxRetryReached(f'sync failed {NETWORK_RETRY} times')
|
||||||
|
|
||||||
|
stdout_handler = logging.NullHandler
|
||||||
|
if SAVE_STDOUT:
|
||||||
|
try:
|
||||||
|
LOG_DIR.mkdir(parents=True, exist_ok=True)
|
||||||
|
_formatter = logging.Formatter(fmt='%(asctime)s - %(message)s')
|
||||||
|
stdout_handler = logging.handlers.RotatingFileHandler(LOG_DIR / "stdout.log", mode='a',
|
||||||
|
maxBytes=10*1024**2, backupCount=2)
|
||||||
|
stdout_handler.setFormatter(_formatter)
|
||||||
|
stdout_handler.setLevel(logging.DEBUG)
|
||||||
|
except Exception:
|
||||||
|
logging.exception(f"unable to save stdout to {LOG_DIR}")
|
||||||
|
stdout_handler = logging.NullHandler
|
||||||
|
logger.addHandler(stdout_handler)
|
||||||
|
|
||||||
for _ in range(NETWORK_RETRY):
|
for _ in range(NETWORK_RETRY):
|
||||||
try:
|
try:
|
||||||
with open(PACMAN_LOG, 'r') as pacman_log:
|
with open(PACMAN_LOG, 'r') as pacman_log:
|
||||||
|
@ -145,14 +160,7 @@ def do_system_upgrade(debug=False, interactive=False) -> checkReport:
|
||||||
else:
|
else:
|
||||||
raise MaxRetryReached(f'upgrade failed {NETWORK_RETRY} times')
|
raise MaxRetryReached(f'upgrade failed {NETWORK_RETRY} times')
|
||||||
|
|
||||||
if SAVE_STDOUT:
|
logger.removeHandler(stdout_handler)
|
||||||
filename = datetime.now().astimezone().isoformat(timespec='seconds').replace(':', '_') + ".log"
|
|
||||||
logger.debug(f"saving stdout to {filename}")
|
|
||||||
try:
|
|
||||||
LOG_DIR.mkdir(parents=True, exist_ok=True)
|
|
||||||
(LOG_DIR / filename).write_text("\n".join(stdout))
|
|
||||||
except Exception:
|
|
||||||
logger.warning(f"unable to save stdout to {filename}\n{traceback.format_exc()}")
|
|
||||||
|
|
||||||
with open(PACMAN_LOG, 'r') as pacman_log:
|
with open(PACMAN_LOG, 'r') as pacman_log:
|
||||||
pacman_log.seek(log_anchor)
|
pacman_log.seek(log_anchor)
|
||||||
|
|
|
@ -44,6 +44,7 @@ def execute_with_io(command: List[str], timeout: int = 3600, interactive: bool =
|
||||||
stderr=subprocess.STDOUT,
|
stderr=subprocess.STDOUT,
|
||||||
encoding='utf-8'
|
encoding='utf-8'
|
||||||
)
|
)
|
||||||
|
logger.debug(f"running {command}")
|
||||||
try:
|
try:
|
||||||
Thread(target=set_timeout, args=(p, timeout), daemon=True).start()
|
Thread(target=set_timeout, args=(p, timeout), daemon=True).start()
|
||||||
line = ''
|
line = ''
|
||||||
|
|
Loading…
Reference in a new issue