Add debugging decorator

This commit is contained in:
Orsiris de Jong 2024-01-12 21:53:45 +01:00
parent b85db5a928
commit 2b5ebb8ba3

View file

@ -8,9 +8,16 @@ __author__ = "Orsiris de Jong"
__site__ = "https://www.netperfect.fr/npbackup" __site__ = "https://www.netperfect.fr/npbackup"
__description__ = "NetPerfect Backup Client" __description__ = "NetPerfect Backup Client"
__copyright__ = "Copyright (C) 2023-2024 NetInvent" __copyright__ = "Copyright (C) 2023-2024 NetInvent"
__build__ = "2024011201"
import os import os
from typing import Callable
from functools import wraps
from logging import getLogger
logger = getLogger()
# If set, debugging will be enabled by setting envrionment variable to __SPECIAL_DEBUG_STRING content # If set, debugging will be enabled by setting envrionment variable to __SPECIAL_DEBUG_STRING content
@ -18,13 +25,33 @@ import os
__SPECIAL_DEBUG_STRING = "" __SPECIAL_DEBUG_STRING = ""
__debug_os_env = os.environ.get("_DEBUG", "False").strip("'\"") __debug_os_env = os.environ.get("_DEBUG", "False").strip("'\"")
try:
# pylint: disable=E0601 (used-before-assignment) if not "_DEBUG" in globals():
_DEBUG
except NameError:
_DEBUG = False _DEBUG = False
if __SPECIAL_DEBUG_STRING: if __SPECIAL_DEBUG_STRING:
if __debug_os_env == __SPECIAL_DEBUG_STRING: if __debug_os_env == __SPECIAL_DEBUG_STRING:
_DEBUG = True _DEBUG = True
elif __debug_os_env.capitalize() == "True": elif __debug_os_env.capitalize() == "True":
_DEBUG = True _DEBUG = True
def catch_exceptions(fn: Callable):
"""
Catch any exception and log it so we don't loose exceptions in thread
"""
@wraps(fn)
def wrapper(self, *args, **kwargs):
try:
# pylint: disable=E1102 (not-callable)
return fn(self, *args, **kwargs)
except Exception as exc:
# pylint: disable=E1101 (no-member)
operation = fn.__name__
logger.error(
f"Function {operation} failed with: {exc}", level="error"
)
logger.error("Trace:", exc_info=True)
return None
return wrapper