Skip to content

Commit

Permalink
keep track of stdout and stderr in log file
Browse files Browse the repository at this point in the history
  • Loading branch information
Ptosiek committed Sep 21, 2023
1 parent 2e2c77e commit 294fa44
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
20 changes: 20 additions & 0 deletions logger.py
Original file line number Diff line number Diff line change
@@ -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:
Expand Down Expand Up @@ -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)
8 changes: 7 additions & 1 deletion modules/sensor/sensor_ant.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

# ANT+
_SENSOR_ANT = False

_sys_stdout = sys.stdout
f = open(os.devnull, "w")
sys.stdout = f
try:
Expand All @@ -33,7 +35,8 @@
except ImportError:
pass
f.close()
sys.stdout = sys.__stdout__
sys.stdout = _sys_stdout

if _SENSOR_ANT:
app_logger.info("ANT")

Expand Down Expand Up @@ -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")

0 comments on commit 294fa44

Please sign in to comment.