Improve logging and docs
This commit is contained in:
@@ -54,10 +54,39 @@ def get_setting_bool(addon_id: str, setting_id: str, *, default: bool = False) -
|
||||
return default
|
||||
|
||||
|
||||
def notify_url(addon_id: str, *, heading: str, url: str, enabled_setting_id: str) -> None:
|
||||
def get_setting_int(addon_id: str, setting_id: str, *, default: int = 0) -> int:
|
||||
if xbmcaddon is None:
|
||||
return default
|
||||
try:
|
||||
addon = xbmcaddon.Addon(addon_id)
|
||||
getter = getattr(addon, "getSettingInt", None)
|
||||
if getter is not None:
|
||||
return int(getter(setting_id))
|
||||
raw = addon.getSetting(setting_id)
|
||||
return int(str(raw).strip())
|
||||
except Exception:
|
||||
return default
|
||||
|
||||
|
||||
def _is_logging_enabled(addon_id: str, *, global_setting_id: str, plugin_setting_id: Optional[str]) -> bool:
|
||||
if not get_setting_bool(addon_id, global_setting_id, default=False):
|
||||
return False
|
||||
if plugin_setting_id:
|
||||
return get_setting_bool(addon_id, plugin_setting_id, default=False)
|
||||
return True
|
||||
|
||||
|
||||
def notify_url(
|
||||
addon_id: str,
|
||||
*,
|
||||
heading: str,
|
||||
url: str,
|
||||
enabled_setting_id: str,
|
||||
plugin_setting_id: Optional[str] = None,
|
||||
) -> None:
|
||||
if xbmcgui is None:
|
||||
return
|
||||
if not get_setting_bool(addon_id, enabled_setting_id, default=False):
|
||||
if not _is_logging_enabled(addon_id, global_setting_id=enabled_setting_id, plugin_setting_id=plugin_setting_id):
|
||||
return
|
||||
try:
|
||||
xbmcgui.Dialog().notification(heading, url, xbmcgui.NOTIFICATION_INFO, 3000)
|
||||
@@ -96,16 +125,92 @@ def _append_text_file(path: str, content: str) -> None:
|
||||
return
|
||||
|
||||
|
||||
def log_url(addon_id: str, *, enabled_setting_id: str, log_filename: str, url: str, kind: str = "VISIT") -> None:
|
||||
if not get_setting_bool(addon_id, enabled_setting_id, default=False):
|
||||
def _rotate_log_file(path: str, *, max_bytes: int, max_files: int) -> None:
|
||||
if max_bytes <= 0 or max_files <= 0:
|
||||
return
|
||||
try:
|
||||
if not os.path.exists(path) or os.path.getsize(path) <= max_bytes:
|
||||
return
|
||||
except Exception:
|
||||
return
|
||||
try:
|
||||
for index in range(max_files - 1, 0, -1):
|
||||
older = f"{path}.{index}"
|
||||
newer = f"{path}.{index + 1}"
|
||||
if os.path.exists(older):
|
||||
if index + 1 > max_files:
|
||||
os.remove(older)
|
||||
else:
|
||||
os.replace(older, newer)
|
||||
os.replace(path, f"{path}.1")
|
||||
except Exception:
|
||||
return
|
||||
|
||||
|
||||
def _prune_dump_files(directory: str, *, prefix: str, max_files: int) -> None:
|
||||
if not directory or max_files <= 0:
|
||||
return
|
||||
try:
|
||||
entries = [
|
||||
os.path.join(directory, name)
|
||||
for name in os.listdir(directory)
|
||||
if name.startswith(prefix) and name.endswith(".html")
|
||||
]
|
||||
if len(entries) <= max_files:
|
||||
return
|
||||
entries.sort(key=lambda path: os.path.getmtime(path))
|
||||
for path in entries[: len(entries) - max_files]:
|
||||
try:
|
||||
os.remove(path)
|
||||
except Exception:
|
||||
pass
|
||||
except Exception:
|
||||
return
|
||||
|
||||
|
||||
def log_url(
|
||||
addon_id: str,
|
||||
*,
|
||||
enabled_setting_id: str,
|
||||
log_filename: str,
|
||||
url: str,
|
||||
kind: str = "VISIT",
|
||||
request_id: Optional[str] = None,
|
||||
plugin_setting_id: Optional[str] = None,
|
||||
max_mb_setting_id: str = "log_max_mb",
|
||||
max_files_setting_id: str = "log_max_files",
|
||||
) -> None:
|
||||
if not _is_logging_enabled(addon_id, global_setting_id=enabled_setting_id, plugin_setting_id=plugin_setting_id):
|
||||
return
|
||||
timestamp = datetime.utcnow().isoformat(timespec="seconds") + "Z"
|
||||
line = f"{timestamp}\t{kind}\t{url}\n"
|
||||
request_part = f"\t{request_id}" if request_id else ""
|
||||
line = f"{timestamp}\t{kind}{request_part}\t{url}\n"
|
||||
log_dir = _profile_logs_dir(addon_id)
|
||||
if log_dir:
|
||||
_append_text_file(os.path.join(log_dir, log_filename), line)
|
||||
return
|
||||
_append_text_file(os.path.join(os.path.dirname(__file__), log_filename), line)
|
||||
path = os.path.join(log_dir, log_filename) if log_dir else os.path.join(os.path.dirname(__file__), log_filename)
|
||||
max_mb = get_setting_int(addon_id, max_mb_setting_id, default=5)
|
||||
max_files = get_setting_int(addon_id, max_files_setting_id, default=3)
|
||||
_rotate_log_file(path, max_bytes=max_mb * 1024 * 1024, max_files=max_files)
|
||||
_append_text_file(path, line)
|
||||
|
||||
|
||||
def log_error(
|
||||
addon_id: str,
|
||||
*,
|
||||
enabled_setting_id: str,
|
||||
log_filename: str,
|
||||
message: str,
|
||||
request_id: Optional[str] = None,
|
||||
plugin_setting_id: Optional[str] = None,
|
||||
) -> None:
|
||||
log_url(
|
||||
addon_id,
|
||||
enabled_setting_id=enabled_setting_id,
|
||||
plugin_setting_id=plugin_setting_id,
|
||||
log_filename=log_filename,
|
||||
url=message,
|
||||
kind="ERROR",
|
||||
request_id=request_id,
|
||||
)
|
||||
|
||||
|
||||
def dump_response_html(
|
||||
@@ -115,14 +220,20 @@ def dump_response_html(
|
||||
url: str,
|
||||
body: str,
|
||||
filename_prefix: str,
|
||||
request_id: Optional[str] = None,
|
||||
plugin_setting_id: Optional[str] = None,
|
||||
max_files_setting_id: str = "dump_max_files",
|
||||
) -> None:
|
||||
if not get_setting_bool(addon_id, enabled_setting_id, default=False):
|
||||
if not _is_logging_enabled(addon_id, global_setting_id=enabled_setting_id, plugin_setting_id=plugin_setting_id):
|
||||
return
|
||||
timestamp = datetime.utcnow().strftime("%Y%m%d_%H%M%S_%f")
|
||||
digest = hashlib.md5(url.encode("utf-8")).hexdigest() # nosec - filename only
|
||||
filename = f"{filename_prefix}_{timestamp}_{digest}.html"
|
||||
log_dir = _profile_logs_dir(addon_id)
|
||||
path = os.path.join(log_dir, filename) if log_dir else os.path.join(os.path.dirname(__file__), filename)
|
||||
content = f"<!-- {url} -->\n{body or ''}"
|
||||
request_line = f" request_id={request_id}" if request_id else ""
|
||||
content = f"<!-- {url}{request_line} -->\n{body or ''}"
|
||||
if log_dir:
|
||||
max_files = get_setting_int(addon_id, max_files_setting_id, default=200)
|
||||
_prune_dump_files(log_dir, prefix=filename_prefix, max_files=max_files)
|
||||
_append_text_file(path, content)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user