Browse Source

Small refactor of the GUI-logger

pull/1628/head
Safihre 5 years ago
parent
commit
a9492eb25f
  1. 37
      SABnzbd.py
  2. 2
      sabnzbd/__init__.py

37
SABnzbd.py

@ -34,6 +34,7 @@ import subprocess
import ssl import ssl
import time import time
import re import re
from typing import List, Dict, Any
try: try:
import Cheetah import Cheetah
@ -119,28 +120,25 @@ class GUIHandler(logging.Handler):
def __init__(self, size): def __init__(self, size):
""" Initializes the handler """ """ Initializes the handler """
logging.Handler.__init__(self) logging.Handler.__init__(self)
self.size = size self._size: int = size
self.store = [] self.store: List[Dict[str, Any]] = []
def emit(self, record): def emit(self, record: logging.LogRecord):
""" Emit a record by adding it to our private queue """ """ Emit a record by adding it to our private queue """
if record.levelname == "WARNING": parsed_msg = record.msg % record.args
sabnzbd.LAST_WARNING = record.msg % record.args if record.levelno == logging.WARNING:
sabnzbd.notifier.send_notification(T("Warning"), parsed_msg, "warning")
else: else:
sabnzbd.LAST_ERROR = record.msg % record.args sabnzbd.notifier.send_notification(T("Error"), parsed_msg, "error")
if len(self.store) >= self.size:
# Loose the oldest record
self.store.pop(0)
try:
# Append traceback, if available # Append traceback, if available
warning = {"type": record.levelname, "text": record.msg % record.args, "time": int(time.time())} warning = {"type": record.levelname, "text": parsed_msg, "time": int(time.time())}
if record.exc_info: if record.exc_info:
warning["text"] = "%s\n%s" % (warning["text"], traceback.format_exc()) warning["text"] = "%s\n%s" % (warning["text"], traceback.format_exc())
# Loose the oldest record
if len(self.store) >= self._size:
self.store.pop(0)
self.store.append(warning) self.store.append(warning)
except UnicodeDecodeError:
# Catch elusive Unicode conversion problems
pass
def clear(self): def clear(self):
self.store = [] self.store = []
@ -1486,15 +1484,6 @@ def main():
# Have to keep this running, otherwise logging will terminate # Have to keep this running, otherwise logging will terminate
timer = 0 timer = 0
while not sabnzbd.SABSTOP: while not sabnzbd.SABSTOP:
if sabnzbd.LAST_WARNING:
msg = sabnzbd.LAST_WARNING
sabnzbd.LAST_WARNING = None
sabnzbd.notifier.send_notification(T("Warning"), msg, "warning")
if sabnzbd.LAST_ERROR:
msg = sabnzbd.LAST_ERROR
sabnzbd.LAST_ERROR = None
sabnzbd.notifier.send_notification(T("Error"), msg, "error")
time.sleep(3) time.sleep(3)
# Check for loglevel changes # Check for loglevel changes

2
sabnzbd/__init__.py

@ -171,8 +171,6 @@ PAUSED_ALL = False
TRIGGER_RESTART = False # To trigger restart for Scheduler, WinService and Mac TRIGGER_RESTART = False # To trigger restart for Scheduler, WinService and Mac
WINTRAY = None # Thread for the Windows SysTray icon WINTRAY = None # Thread for the Windows SysTray icon
WEBUI_READY = False WEBUI_READY = False
LAST_WARNING = None
LAST_ERROR = None
EXTERNAL_IPV6 = False EXTERNAL_IPV6 = False
LAST_HISTORY_UPDATE = 1 LAST_HISTORY_UPDATE = 1

Loading…
Cancel
Save