From 294fa44695279f86ed52a5a15d431907beb37424 Mon Sep 17 00:00:00 2001 From: xnegativx Date: Thu, 21 Sep 2023 17:15:15 +0200 Subject: [PATCH] keep track of stdout and stderr in log file --- logger.py | 20 ++++++++++++++++++++ modules/sensor/sensor_ant.py | 8 +++++++- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/logger.py b/logger.py index 94be6086..88907560 100644 --- a/logger.py +++ b/logger.py @@ -1,11 +1,28 @@ import logging import os import re +import sys from datetime import datetime, timedelta from logging.handlers import RotatingFileHandler +class StreamToLogger: + logger = None + level = None + + def __init__(self, logger, level): + self.logger = logger + self.level = level + + def write(self, buf): + for line in buf.rstrip().splitlines(): + self.logger.log(self.level, line.rstrip()) + + def flush(self): + pass + + class CustomRotatingFileHandler(RotatingFileHandler): def doRollover(self): if self.stream: @@ -53,3 +70,6 @@ def shouldRollover(self, record): sh = logging.StreamHandler() app_logger.addHandler(sh) + +sys.stdout = StreamToLogger(app_logger, logging.INFO) +sys.stderr = StreamToLogger(app_logger, logging.ERROR) diff --git a/modules/sensor/sensor_ant.py b/modules/sensor/sensor_ant.py index d9c28644..c2f727d1 100644 --- a/modules/sensor/sensor_ant.py +++ b/modules/sensor/sensor_ant.py @@ -21,6 +21,8 @@ # ANT+ _SENSOR_ANT = False + +_sys_stdout = sys.stdout f = open(os.devnull, "w") sys.stdout = f try: @@ -33,7 +35,8 @@ except ImportError: pass f.close() -sys.stdout = sys.__stdout__ +sys.stdout = _sys_stdout + if _SENSOR_ANT: app_logger.info("ANT") @@ -303,3 +306,6 @@ def set_light_mode(self, mode, auto=False): self.device[ self.config.G_ANT["ID_TYPE"]["LGT"] ].send_light_setting_light_off_flash_low(auto) + + +print("CUBU ANT")