Skip to content

Commit

Permalink
added logging feature to launcher
Browse files Browse the repository at this point in the history
  • Loading branch information
Angeraa committed Oct 28, 2023
1 parent da3dfe6 commit 4eba00d
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 6 deletions.
26 changes: 20 additions & 6 deletions launcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import subprocess
import sys
import time
import logging
from logtool import init_loggers

# Some specific commands are needed for Windows vs macOS/Linux
if sys.platform == "win32":
Expand Down Expand Up @@ -32,6 +34,9 @@
source = [python_executable, f"sources/{modules['sources'][int(source_selection) - 1]}/main.py"]
sink = [python_executable, f"sinks/{modules['sinks'][int(sink_selection) - 1]}/main.py"]

loggers = init_loggers()
print("Loggers Initiated")

commands = [omnibus, source, sink]
processes = []
print("Launching... ", end="")
Expand Down Expand Up @@ -68,18 +73,27 @@ class Finished(Exception):
process.send_signal(signal.SIGINT)

# Dump output and error (if exists) from every
# process to the shell
# process to the coresponding log file
output, err = process.communicate()
output, err = output.decode(), err.decode()
print(f"\nOutput from {process.args}:")
print(output)
try:
loggers[process.args[-1].split("/")[1]].info(f"From {process.args}:{output}")
print(f"\nOutput from {process.args} logged")
except IndexError:
print(f"\nOutput from {process.args} logged in other.log")
loggers["other"].info(f"From{process.args}:{output}")

if err and "KeyboardInterrupt" not in err:
print(f"\nError from {process.args}:")
print(err)
try:
loggers[process.args[-1].split("/")[1]].error(f"From {process.args}:{err}")
print(f"\nError from {process.args} logged")
except IndexError:
print(f"\nError from {process.args} logged in other.log")
loggers["other"].error(f"From{process.args}:{err}")
logging.shutdown()
finally:
for process in processes:
if sys.platform == "win32":
os.kill(process.pid, signal.CTRL_BREAK_EVENT)
else:
process.send_signal(signal.SIGINT)
process.send_signal(signal.SIGINT)
48 changes: 48 additions & 0 deletions logtool.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import logging
import os
#dynamically generates logger objects and log files based on the number of sources and sinks and stores all loggers in a dictionary accessed with the filename without .py
#so for example the file "sinks/dashboard/main.py" would be accessed with "dashboard"

def init_loggers():
#initializes variable to be returned
loggers = dict([])

#generates logger objects and log files for each source
for filename in os.listdir("sources"):
os.makedirs(os.path.dirname(f'logs/sources/{filename}.log'), exist_ok=True)
logger = logging.getLogger(filename)
logger.setLevel("INFO")

handler = logging.FileHandler(f"logs/sources/{filename}.log", mode='w')
formatter = logging.Formatter('%(levelname)s:%(name)s:%(message)s')
handler.setFormatter(formatter)

logger.addHandler(handler)
loggers.update({filename: logger})

#generates logger objects and log files for each sink
for filename in os.listdir("sinks"):
os.makedirs(os.path.dirname(f'logs/sinks/{filename}.log'), exist_ok=True)
logger = logging.getLogger(filename)
logger.setLevel("INFO")

handler = logging.FileHandler(f"logs/sinks/{filename}.log", mode='w')
formatter = logging.Formatter('%(levelname)s:%(name)s:%(message)s')
handler.setFormatter(formatter)

logger.addHandler(handler)
loggers.update({filename: logger})

#generates a misc logger and log file in case there is an output or error from neither a sink nor a source
os.makedirs(os.path.dirname(f'logs/other.log'), exist_ok=True)
logger = logging.getLogger("other")
logger.setLevel("INFO")

handler = logging.FileHandler(f"logs/other.log", mode='w')
formatter = logging.Formatter('%(levelname)s:%(name)s:%(message)s')
handler.setFormatter(formatter)

logger.addHandler(handler)
loggers.update({"other": logger})

return loggers

0 comments on commit 4eba00d

Please sign in to comment.