Added some logging

This commit is contained in:
deckensteuerung 2018-08-22 20:48:26 +02:00
parent 01ffe5b124
commit f15a9c4ebd

25
main.py
View File

@ -8,6 +8,9 @@ from bottle import route, run
import time import time
import sys import sys
import signal import signal
import logging
logging.basicConfig(filename='pixelserver.log',level=config.LogLevel)
running = True running = True
@ -24,12 +27,14 @@ if config.UseGui:
def run(self): def run(self):
global running global running
logging.info("Starting GUI")
sf = config.GuiScaleFactor sf = config.GuiScaleFactor
screen = pygame.display.set_mode((sf*config.ScreenX, sf*config.ScreenY)) screen = pygame.display.set_mode((sf*config.ScreenX, sf*config.ScreenY))
pygame.display.set_caption("Pixelserver - GUI Vis") pygame.display.set_caption("Pixelserver - GUI Vis")
while running: while running:
for event in pygame.event.get(): for event in pygame.event.get():
if event.type == pygame.QUIT: if event.type == pygame.QUIT:
logging.debug("GUI quits app")
running = False running = False
screen.fill((0, 255, 0)) screen.fill((0, 255, 0))
for x in range(config.ScreenX): for x in range(config.ScreenX):
@ -41,6 +46,7 @@ if config.UseGui:
pygame.draw.rect(screen, (r, g, b), pygame.Rect(sf*x, sf*y, sf, sf)) pygame.draw.rect(screen, (r, g, b), pygame.Rect(sf*x, sf*y, sf, sf))
pygame.display.flip() pygame.display.flip()
logging.info("Closing GUI")
class SerialWriter(threading.Thread): class SerialWriter(threading.Thread):
def __init__(self): def __init__(self):
@ -55,22 +61,25 @@ class SerialWriter(threading.Thread):
def run(self): def run(self):
should_connect = True should_connect = True
ser = None ser = None
logging.info("Starting SerialWriter")
while running: while running:
try: try:
if should_connect: if should_connect:
ser = serial.Serial(config.Serial) ser = serial.Serial(config.Serial)
should_connect = False should_connect = False
logging.info("Serial Opened")
with self.lock: with self.lock:
ser.write(b"\01") ser.write(b"\01")
ser.write(self.data) ser.write(self.data)
time.sleep(1/120) time.sleep(1/120)
except: except Exception as e:
if ser != None: if ser != None:
ser.close() ser.close()
ser = None ser = None
logging.warning("Serial was close because: "+str(e))
should_connect = True should_connect = True
time.sleep(0.1) time.sleep(0.1)
logging.info("Closing SerialWriter")
class WatchDog(threading.Thread): class WatchDog(threading.Thread):
def __init__(self, check, action): def __init__(self, check, action):
@ -80,6 +89,7 @@ class WatchDog(threading.Thread):
def run(self): def run(self):
while running: while running:
if self.check(): if self.check():
logging.error("Watchdog: Executed")
self.action() self.action()
time.sleep(1) time.sleep(1)
@ -93,12 +103,13 @@ class LogReader(threading.Thread):
self.log = "" self.log = ""
def run(self): def run(self):
logging.info("LogReader started")
while running: while running:
try: try:
self.log += runner.app.stderr.read(1).decode("utf-8") self.log += runner.app.stderr.read(1).decode("utf-8")
except Exception as e: except Exception as e:
time.sleep(0.1) time.sleep(0.1)
pass logging.info("LogReader closed")
class AppRunner(threading.Thread): class AppRunner(threading.Thread):
@ -127,6 +138,7 @@ class AppRunner(threading.Thread):
def terminateApp(self): def terminateApp(self):
if self.app != None: if self.app != None:
logging.error("Terminate app "+config.Apps[self.currentApp]["name"])
# don't ask # don't ask
self.app.terminate() self.app.terminate()
if self.app != None: if self.app != None:
@ -148,6 +160,7 @@ class AppRunner(threading.Thread):
self.logreader.clear() self.logreader.clear()
def run(self): def run(self):
logging.info("Starting Apprunner")
while running: while running:
with self.lock: with self.lock:
if self.app == None or self.app.poll() != None: if self.app == None or self.app.poll() != None:
@ -165,12 +178,14 @@ class AppRunner(threading.Thread):
if config.UseGui: if config.UseGui:
self.gui.setData(data) self.gui.setData(data)
except: except:
pass logging.debug("Exception in Apprunner.run")
self.watchdog.join() self.watchdog.join()
self.serial.join() self.serial.join()
logreader.join() logreader.join()
if config.UseGui: if config.UseGui:
self.gui.join() self.gui.join()
logging.info("Close Apprunner")
def getLog(self): def getLog(self):
return self.logreader.log return self.logreader.log
@ -212,5 +227,5 @@ def apps_running():
run(host="localhost", port=8000) run(host="localhost", port=8000)
running = Falselog running = False
runner.join() runner.join()