2024-09-17 15:26:25 +02:00
|
|
|
import atexit
|
2020-11-04 04:26:39 +01:00
|
|
|
import logging
|
2023-05-29 12:31:17 +02:00
|
|
|
import multiprocessing as mp
|
2020-12-04 13:59:03 +01:00
|
|
|
import os
|
2024-09-24 14:07:47 +02:00
|
|
|
import sys
|
2023-05-29 12:31:17 +02:00
|
|
|
import threading
|
|
|
|
from collections import deque
|
2024-09-17 15:26:25 +02:00
|
|
|
from logging.handlers import QueueHandler, QueueListener
|
2023-02-04 03:15:47 +01:00
|
|
|
from typing import Deque, Optional
|
2023-05-29 12:31:17 +02:00
|
|
|
|
2023-07-06 16:28:50 +02:00
|
|
|
from frigate.util.builtin import clean_camera_user_pass
|
2022-11-02 13:00:54 +01:00
|
|
|
|
2024-09-17 15:26:25 +02:00
|
|
|
LOG_HANDLER = logging.StreamHandler()
|
|
|
|
LOG_HANDLER.setFormatter(
|
|
|
|
logging.Formatter(
|
|
|
|
"[%(asctime)s] %(name)-30s %(levelname)-8s: %(message)s",
|
|
|
|
"%Y-%m-%d %H:%M:%S",
|
|
|
|
)
|
|
|
|
)
|
2020-11-04 13:31:25 +01:00
|
|
|
|
2024-09-17 15:26:25 +02:00
|
|
|
LOG_HANDLER.addFilter(
|
|
|
|
lambda record: not record.getMessage().startswith(
|
|
|
|
"You are using a scalar distance function"
|
|
|
|
)
|
|
|
|
)
|
2023-04-26 13:08:53 +02:00
|
|
|
|
2024-09-27 14:53:23 +02:00
|
|
|
log_listener: Optional[QueueListener] = None
|
2023-04-26 13:08:53 +02:00
|
|
|
|
2021-02-17 14:23:32 +01:00
|
|
|
|
2024-09-27 14:53:23 +02:00
|
|
|
def setup_logging() -> None:
|
|
|
|
global log_listener
|
2023-04-26 13:08:53 +02:00
|
|
|
|
2024-09-27 14:53:23 +02:00
|
|
|
log_queue: mp.Queue = mp.Queue()
|
|
|
|
log_listener = QueueListener(log_queue, LOG_HANDLER, respect_handler_level=True)
|
2023-04-26 13:08:53 +02:00
|
|
|
|
2024-09-27 14:53:23 +02:00
|
|
|
atexit.register(_stop_logging)
|
|
|
|
log_listener.start()
|
2020-11-04 04:26:39 +01:00
|
|
|
|
2024-09-27 14:53:23 +02:00
|
|
|
logging.basicConfig(
|
|
|
|
level=logging.INFO,
|
|
|
|
handlers=[],
|
|
|
|
force=True,
|
|
|
|
)
|
2023-02-04 03:15:47 +01:00
|
|
|
|
2024-09-27 14:53:23 +02:00
|
|
|
logging.getLogger().addHandler(QueueHandler(log_listener.queue))
|
2023-02-04 03:15:47 +01:00
|
|
|
|
|
|
|
|
2024-09-27 14:53:23 +02:00
|
|
|
def _stop_logging() -> None:
|
|
|
|
global log_listener
|
2023-02-04 03:15:47 +01:00
|
|
|
|
2024-09-27 14:53:23 +02:00
|
|
|
if log_listener is not None:
|
|
|
|
log_listener.stop()
|
|
|
|
log_listener = None
|
2020-12-04 13:59:03 +01:00
|
|
|
|
2021-02-17 14:23:32 +01:00
|
|
|
|
2024-09-24 14:07:47 +02:00
|
|
|
# When a multiprocessing.Process exits, python tries to flush stdout and stderr. However, if the
|
|
|
|
# process is created after a thread (for example a logging thread) is created and the process fork
|
|
|
|
# happens while an internal lock is held, the stdout/err flush can cause a deadlock.
|
|
|
|
#
|
|
|
|
# https://github.com/python/cpython/issues/91776
|
|
|
|
def reopen_std_streams() -> None:
|
|
|
|
sys.stdout = os.fdopen(1, "w")
|
|
|
|
sys.stderr = os.fdopen(2, "w")
|
|
|
|
|
|
|
|
|
|
|
|
os.register_at_fork(after_in_child=reopen_std_streams)
|
|
|
|
|
|
|
|
|
2020-12-04 13:59:03 +01:00
|
|
|
# based on https://codereview.stackexchange.com/a/17959
|
|
|
|
class LogPipe(threading.Thread):
|
2022-04-12 22:24:45 +02:00
|
|
|
def __init__(self, log_name: str):
|
|
|
|
"""Setup the object with a logger and start the thread"""
|
2024-10-03 03:35:46 +02:00
|
|
|
super().__init__(daemon=False)
|
2020-12-04 13:59:03 +01:00
|
|
|
self.logger = logging.getLogger(log_name)
|
2022-04-12 22:24:45 +02:00
|
|
|
self.level = logging.ERROR
|
|
|
|
self.deque: Deque[str] = deque(maxlen=100)
|
2020-12-04 13:59:03 +01:00
|
|
|
self.fdRead, self.fdWrite = os.pipe()
|
|
|
|
self.pipeReader = os.fdopen(self.fdRead)
|
|
|
|
self.start()
|
|
|
|
|
2022-11-02 13:00:54 +01:00
|
|
|
def cleanup_log(self, log: str) -> str:
|
|
|
|
"""Cleanup the log line to remove sensitive info and string tokens."""
|
|
|
|
log = clean_camera_user_pass(log).strip("\n")
|
|
|
|
return log
|
|
|
|
|
2022-04-12 22:24:45 +02:00
|
|
|
def fileno(self) -> int:
|
2021-02-17 14:23:32 +01:00
|
|
|
"""Return the write file descriptor of the pipe"""
|
2020-12-04 13:59:03 +01:00
|
|
|
return self.fdWrite
|
|
|
|
|
2022-04-12 22:24:45 +02:00
|
|
|
def run(self) -> None:
|
2021-02-17 14:23:32 +01:00
|
|
|
"""Run the thread, logging everything."""
|
|
|
|
for line in iter(self.pipeReader.readline, ""):
|
2022-11-02 13:00:54 +01:00
|
|
|
self.deque.append(self.cleanup_log(line))
|
2020-12-04 13:59:03 +01:00
|
|
|
|
|
|
|
self.pipeReader.close()
|
2021-02-17 14:23:32 +01:00
|
|
|
|
2022-04-12 22:24:45 +02:00
|
|
|
def dump(self) -> None:
|
2021-01-30 14:50:17 +01:00
|
|
|
while len(self.deque) > 0:
|
|
|
|
self.logger.log(self.level, self.deque.popleft())
|
2020-12-04 13:59:03 +01:00
|
|
|
|
2022-04-12 22:24:45 +02:00
|
|
|
def close(self) -> None:
|
2021-02-17 14:23:32 +01:00
|
|
|
"""Close the write end of the pipe."""
|
2021-01-03 20:41:02 +01:00
|
|
|
os.close(self.fdWrite)
|