2024-02-07 23:28:54 +01:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
import subprocess
|
|
|
|
from shutil import which
|
|
|
|
import logging
|
|
|
|
import logging.handlers
|
|
|
|
|
|
|
|
|
2024-02-24 18:41:11 +01:00
|
|
|
def send_to_matrix(message: str) -> None:
|
|
|
|
if which("sendxmpppy") is None:
|
|
|
|
logging.warning("Could not send error via xmpp.")
|
|
|
|
return
|
|
|
|
subprocess.call(["sendxmpppy", message], stdout=subprocess.DEVNULL)
|
|
|
|
|
|
|
|
|
2024-02-08 22:18:59 +01:00
|
|
|
class LogSenderHandler(logging.Handler):
|
2024-02-07 23:28:54 +01:00
|
|
|
def __init__(self):
|
|
|
|
logging.Handler.__init__(self)
|
|
|
|
self.is_logging = False
|
|
|
|
|
|
|
|
def emit(self, record):
|
2024-02-24 18:41:11 +01:00
|
|
|
msg = f"[Apps tools error] {record.msg}"
|
|
|
|
send_to_matrix(msg)
|
2024-02-07 23:28:54 +01:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def add(cls, level=logging.ERROR):
|
|
|
|
if not logging.getLogger().handlers:
|
|
|
|
logging.basicConfig()
|
|
|
|
|
|
|
|
# create handler
|
|
|
|
handler = cls()
|
|
|
|
handler.setLevel(level)
|
|
|
|
# add the handler
|
|
|
|
logging.getLogger().handlers.append(handler)
|
|
|
|
|
|
|
|
|
2024-02-08 22:15:08 +01:00
|
|
|
def enable():
|
2024-02-08 22:18:59 +01:00
|
|
|
"""Enables the LogSenderHandler"""
|
|
|
|
LogSenderHandler.add(logging.ERROR)
|