diff --git a/server/brewpi.py b/server/brewpi.py index 573a5e3..51e1f98 100755 --- a/server/brewpi.py +++ b/server/brewpi.py @@ -1,6 +1,9 @@ #!/usr/bin/env python3 import asyncio import json +import os +import sys +import time from utils import ChangedFloat from ws.message import MessageDispatcher from ws.server.ws_server_multi_user import WsServerMultiUser @@ -23,7 +26,33 @@ DEFAULT_PLANT_PARAMS = { "Td": 30 } + +class Tee: + """Duplicates writes to multiple streams - used to mirror stdout/stderr + (everything the rest of the codebase already reaches via print()) into + a log file under logs/ as well as the console, without having to touch + every print() call site.""" + + def __init__(self, *streams): + self.streams = streams + + def write(self, data): + for stream in self.streams: + stream.write(data) + + def flush(self): + for stream in self.streams: + stream.flush() + + if __name__ == '__main__': + os.makedirs("logs", exist_ok=True) + log_path = "logs/brewpi.{}.log".format(time.strftime("%Y%m%d%H%M%S", time.localtime())) + log_file = open(log_path, "a", buffering=1) + sys.stdout = Tee(sys.stdout, log_file) + sys.stderr = Tee(sys.stderr, log_file) + print("Logging to {}".format(log_path)) + parser = ap.ArgumentParser() parser.add_argument("-d", "--debug", action="store_true") parser.add_argument("-c", "--config", default="config.json")