From ed30fa3e0a7bd79c77b2811eb64100ff58d92eab Mon Sep 17 00:00:00 2001 From: Mark Qvist Date: Sun, 8 Dec 2024 14:27:17 +0100 Subject: [PATCH] Added ability to reflect RNS logs to app-internal log handler callback --- RNS/Reticulum.py | 3 +++ RNS/__init__.py | 13 +++++++++++++ 2 files changed, 16 insertions(+) diff --git a/RNS/Reticulum.py b/RNS/Reticulum.py index 2882e87..0146e7f 100755 --- a/RNS/Reticulum.py +++ b/RNS/Reticulum.py @@ -213,6 +213,9 @@ class Reticulum: if logdest == RNS.LOG_FILE: RNS.logdest = RNS.LOG_FILE RNS.logfile = Reticulum.configdir+"/logfile" + elif callable(logdest): + RNS.logdest = RNS.LOG_CALLBACK + RNS.logcall = logdest Reticulum.configpath = Reticulum.configdir+"/config" Reticulum.storagepath = Reticulum.configdir+"/storage" diff --git a/RNS/__init__.py b/RNS/__init__.py index 24e6b23..177d39b 100755 --- a/RNS/__init__.py +++ b/RNS/__init__.py @@ -59,12 +59,14 @@ LOG_EXTREME = 7 LOG_STDOUT = 0x91 LOG_FILE = 0x92 +LOG_CALLBACK = 0x93 LOG_MAXSIZE = 5*1024*1024 loglevel = LOG_NOTICE logfile = None logdest = LOG_STDOUT +logcall = None logtimefmt = "%Y-%m-%d %H:%M:%S" compact_log_fmt = False @@ -140,6 +142,17 @@ def log(msg, level=3, _override_destination = False): log("Exception occurred while writing log message to log file: "+str(e), LOG_CRITICAL) log("Dumping future log events to console!", LOG_CRITICAL) log(msg, level) + + elif logdest == LOG_CALLBACK: + try: + logcall(logstring) + logging_lock.release() + except Exception as e: + logging_lock.release() + _always_override_destination = True + log("Exception occurred while calling external log handler: "+str(e), LOG_CRITICAL) + log("Dumping future log events to console!", LOG_CRITICAL) + log(msg, level) def rand():