tail last 100 lines of ffmpeg logs and dump when failure detected

This commit is contained in:
Blake Blackshear 2021-01-30 07:50:17 -06:00
parent b55bd1e027
commit ee3e744cc6
2 changed files with 9 additions and 1 deletions

View File

@ -7,6 +7,7 @@ import queue
import multiprocessing as mp import multiprocessing as mp
from logging import handlers from logging import handlers
from setproctitle import setproctitle from setproctitle import setproctitle
from collections import deque
def listener_configurer(): def listener_configurer():
@ -54,6 +55,7 @@ class LogPipe(threading.Thread):
self.daemon = False self.daemon = False
self.logger = logging.getLogger(log_name) self.logger = logging.getLogger(log_name)
self.level = level self.level = level
self.deque = deque(maxlen=100)
self.fdRead, self.fdWrite = os.pipe() self.fdRead, self.fdWrite = os.pipe()
self.pipeReader = os.fdopen(self.fdRead) self.pipeReader = os.fdopen(self.fdRead)
self.start() self.start()
@ -67,9 +69,13 @@ class LogPipe(threading.Thread):
"""Run the thread, logging everything. """Run the thread, logging everything.
""" """
for line in iter(self.pipeReader.readline, ''): for line in iter(self.pipeReader.readline, ''):
self.logger.log(self.level, line.strip('\n')) self.deque.append(line.strip('\n'))
self.pipeReader.close() self.pipeReader.close()
def dump(self):
while len(self.deque) > 0:
self.logger.log(self.level, self.deque.popleft())
def close(self): def close(self):
"""Close the write end of the pipe. """Close the write end of the pipe.

View File

@ -181,6 +181,7 @@ class CameraWatchdog(threading.Thread):
now = datetime.datetime.now().timestamp() now = datetime.datetime.now().timestamp()
if not self.capture_thread.is_alive(): if not self.capture_thread.is_alive():
self.logpipe.dump()
self.start_ffmpeg_detect() self.start_ffmpeg_detect()
elif now - self.capture_thread.current_frame.value > 20: elif now - self.capture_thread.current_frame.value > 20:
self.logger.info(f"No frames received from {self.camera_name} in 20 seconds. Exiting ffmpeg...") self.logger.info(f"No frames received from {self.camera_name} in 20 seconds. Exiting ffmpeg...")
@ -197,6 +198,7 @@ class CameraWatchdog(threading.Thread):
poll = p['process'].poll() poll = p['process'].poll()
if poll == None: if poll == None:
continue continue
p['logpipe'].dump()
p['process'] = start_or_restart_ffmpeg(p['cmd'], self.logger, p['logpipe'], ffmpeg_process=p['process']) p['process'] = start_or_restart_ffmpeg(p['cmd'], self.logger, p['logpipe'], ffmpeg_process=p['process'])
# wait a bit before checking again # wait a bit before checking again