From 1034d788286c2315e28491275f68cee87624a713 Mon Sep 17 00:00:00 2001 From: Mark Qvist Date: Mon, 24 Aug 2026 10:52:47 +0200 Subject: [PATCH] Merge adjustments --- RNS/Reticulum.py | 3 +- RNS/__init__.py | 150 +++++++++++++++-------------------------------- 2 files changed, 49 insertions(+), 104 deletions(-) diff --git a/RNS/Reticulum.py b/RNS/Reticulum.py index 0804c567..c45c21b4 100755 --- a/RNS/Reticulum.py +++ b/RNS/Reticulum.py @@ -1291,7 +1291,7 @@ class Reticulum: if path == "packet_rssi": self.rpc_return(conn, self.get_packet_rssi(call["packet_hash"])) if path == "packet_snr": self.rpc_return(conn, self.get_packet_snr(call["packet_hash"])) if path == "packet_q": self.rpc_return(conn, self.get_packet_q(call["packet_hash"])) - if path == "profiling_results": self.rpc_return(conn, self.get_profiling_results()) + if path == "profiling_results": self.rpc_return(conn, self.get_profiling_results()) if path == "blackholed_identities": self.rpc_return(conn, self.get_blackholed_identities()) if path == "is_blackholed": self.rpc_return(conn, self.is_blackholed(call["identity_hash"])) @@ -1327,7 +1327,6 @@ class Reticulum: except Exception as e: RNS.log("An error ocurred while handling RPC call from local client: "+str(e), RNS.LOG_ERROR) - RNS.trace_exception(e) def get_rpc_client(self): return multiprocessing.connection.Client(self.rpc_addr, family=self.rpc_type, authkey=self.rpc_key) diff --git a/RNS/__init__.py b/RNS/__init__.py index e4b513b1..52c42e5b 100755 --- a/RNS/__init__.py +++ b/RNS/__init__.py @@ -38,11 +38,25 @@ import threading import math import bisect +from ._version import __version__ from collections import deque from threading import Lock, Condition from functools import partial, wraps -from ._version import __version__ +from .Reticulum import Reticulum +from .Identity import Identity +from .Link import Link, RequestReceipt +from .Channel import MessageBase +from .Buffer import Buffer, RawChannelReader, RawChannelWriter +from .Transport import Transport +from .Discovery import InterfaceAnnouncer +from .Destination import Destination +from .Packet import Packet +from .Packet import PacketReceipt +from .Resolver import Resolver +from .Resource import Resource, ResourceAdvertisement +from .Cryptography import HKDF +from .Cryptography import Hashes py_modules = glob.glob(os.path.dirname(__file__)+"/*.py") pyc_modules = glob.glob(os.path.dirname(__file__)+"/*.pyc") @@ -68,8 +82,7 @@ LOG_STDOUT = 0x91 LOG_FILE = 0x92 LOG_CALLBACK = 0x93 -LOG_MAXSIZE = 30*1024*1024 -LOG_MAXROT = 9 +LOG_MAXSIZE = 5*1024*1024 loglevel = LOG_NOTICE logfile = None @@ -85,6 +98,8 @@ instance_random.seed(os.urandom(10)) _always_override_destination = False +logging_lock = threading.Lock() + def loglevelname(level): if (level == LOG_CRITICAL): return "[Critical]" if (level == LOG_ERROR): return "[Error] " @@ -112,97 +127,45 @@ def timestamp_str(time_s): def precise_timestamp_str(time_s): return datetime.datetime.now().strftime(logtimefmt_p)[:-3] -_log_thread = None -_log_thread_lock = Lock() -_log_queue = deque() -_log_cond = Condition() -def _ensure_log_thread(): - global _log_thread - if _log_thread is None or not _log_thread.is_alive(): - _log_thread = threading.Thread(target=_log_job, daemon=True) - _log_thread.start() - def sl(level=3): return loglevel >= level def log(msg, level=3, _override_destination = False, pt=False): - global compact_log_fmt - if loglevel == LOG_NONE: return - _ensure_log_thread() - + global _always_override_destination, compact_log_fmt msg = str(msg) if loglevel >= level: - with _log_cond: - if pt: logstring = "["+precise_timestamp_str(time.time())+"] "+loglevelname(level)+" "+msg - else: - if not compact_log_fmt: logstring = ("["+timestamp_str(time.time())+"] " if logtimestamps else "")+loglevelname(level)+" "+msg - else: logstring = ("["+timestamp_str(time.time())+"] " if logtimestamps else "")+msg + if pt: logstring = "["+precise_timestamp_str(time.time())+"] "+loglevelname(level)+" "+msg + else: + if not compact_log_fmt: logstring = ("["+timestamp_str(time.time())+"] " if logtimestamps else "")+loglevelname(level)+" "+msg + else: logstring = ("["+timestamp_str(time.time())+"] " if logtimestamps else "")+msg - _log_queue.append((logstring, level, _override_destination)) - _log_cond.notify() + with logging_lock: + if (logdest == LOG_STDOUT or _always_override_destination or _override_destination): + if not threading.main_thread().is_alive(): return + else: + try: print(logstring) + except: pass -def _log_job(): - global _always_override_destination + elif (logdest == LOG_FILE and logfile != None): + try: + with open(logfile, "a") as file: file.write(logstring+"\n") + if os.path.getsize(logfile) > LOG_MAXSIZE: + prevfile = logfile+".1" + if os.path.isfile(prevfile): os.unlink(prevfile) + os.rename(logfile, prevfile) - if not _log_thread_lock.acquire(blocking=False): return - try: - file = None - if (logdest == LOG_FILE and logfile != None): - try: - file = open(logfile, "a", buffering=1) - except Exception as e: - _always_override_destination = True - log("Exception occurred while opening log file: "+str(e), LOG_CRITICAL) - log("Dumping future log events to console!", LOG_CRITICAL) + except Exception as e: + _always_override_destination = True + 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) - while True: - with _log_cond: - try: - logstring, level, _override_destination = _log_queue.popleft() - except IndexError as e: - _log_cond.wait() - continue - - if (logdest == LOG_STDOUT or _always_override_destination or _override_destination): - if not threading.main_thread().is_alive(): return - else: - try: print(logstring) - except: pass - - elif (logdest == LOG_FILE and logfile != None and file != None): - try: - file.write(logstring+"\n") - if os.path.getsize(logfile) > LOG_MAXSIZE: - file.close() - for i in range(LOG_MAXROT, 0, -1): - oldfile = f"{logfile}.{i}" - if os.path.isfile(oldfile): - if i == LOG_MAXROT: - os.unlink(oldfile) - else: - rotfile = f"{logfile}.{i+1}" - os.rename(oldfile, rotfile) - - rotfile = f"{logfile}.1" - os.rename(logfile, rotfile) - file = open(logfile, "a", buffering=1) - - except Exception as e: - _always_override_destination = True - 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) - except Exception as e: - _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) - - finally: - if file is not None: file.close() - _log_thread_lock.release() + elif logdest == LOG_CALLBACK: + try: logcall(logstring) + except Exception as e: + _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(): result = instance_random.random() @@ -711,20 +674,3 @@ def bytes_to_b256(data): if not type(data) == bytes: raise TypeError("Invalid input data for base256 encode") try: return [byte_to_b256(c) for c in data] except Exception as e: raise TypeError(f"Could not encode to base256: {e}") - - -from .Reticulum import Reticulum -from .Identity import Identity -from .Link import Link, RequestReceipt -from .Channel import MessageBase -from .Buffer import Buffer, RawChannelReader, RawChannelWriter -from .Transport import Transport -from .Discovery import InterfaceAnnouncer -from .Destination import Destination -from .Packet import Packet -from .Packet import PacketReceipt -from .Resolver import Resolver -from .Resource import Resource, ResourceAdvertisement -from .Cryptography import HKDF -from .Cryptography import Hashes -