From 54206d9101b3132fe0320a0d2232e2a537bea491 Mon Sep 17 00:00:00 2001 From: Mark Qvist Date: Mon, 3 May 2021 20:24:44 +0200 Subject: [PATCH] Added thread locking to log output. Various housekeeping. --- Examples/Broadcast.py | 2 +- RNS/Identity.py | 2 +- RNS/Resource.py | 10 +++++----- RNS/__init__.py | 31 ++++++++++++++++++++++++------- 4 files changed, 31 insertions(+), 14 deletions(-) diff --git a/Examples/Broadcast.py b/Examples/Broadcast.py index 7bce55c..5bd5bea 100644 --- a/Examples/Broadcast.py +++ b/Examples/Broadcast.py @@ -73,7 +73,7 @@ if __name__ == "__main__": try: parser = argparse.ArgumentParser(description="Reticulum example that demonstrates sending and receiving unencrypted broadcasts") parser.add_argument("--config", action="store", default=None, help="path to alternative Reticulum config directory", type=str) - parser.add_argument("--channel", action="store", default=None, help="path to alternative Reticulum config directory", type=str) + parser.add_argument("--channel", action="store", default=None, help="broadcast channel name", type=str) args = parser.parse_args() if args.config: diff --git a/RNS/Identity.py b/RNS/Identity.py index 6e9bcd8..3796d9e 100644 --- a/RNS/Identity.py +++ b/RNS/Identity.py @@ -188,7 +188,7 @@ class Identity: except Exception as e: RNS.log("Failed to load identity key", RNS.LOG_ERROR) - RNS.log("The contained exception was: "+str(e)) + RNS.log("The contained exception was: "+str(e), RNS.LOG_ERROR) return False def loadPublicKey(self, key): diff --git a/RNS/Resource.py b/RNS/Resource.py index 00a306f..5b3dba6 100644 --- a/RNS/Resource.py +++ b/RNS/Resource.py @@ -42,15 +42,15 @@ class Resource: HASHMAP_IS_EXHAUSTED = 0xFF # Status constants - NONE = 0x00 - QUEUED = 0x01 - ADVERTISED = 0x02 + NONE = 0x00 + QUEUED = 0x01 + ADVERTISED = 0x02 TRANSFERRING = 0x03 AWAITING_PROOF = 0x04 ASSEMBLING = 0x05 COMPLETE = 0x06 - FAILED = 0x07 - CORRUPT = 0x08 + FAILED = 0x07 + CORRUPT = 0x08 @staticmethod def accept(advertisement_packet, callback=None, progress_callback = None): diff --git a/RNS/__init__.py b/RNS/__init__.py index dcbfc3d..72796fa 100755 --- a/RNS/__init__.py +++ b/RNS/__init__.py @@ -3,6 +3,7 @@ import sys import glob import time import random +import threading from .Reticulum import Reticulum from .Identity import Identity @@ -35,6 +36,10 @@ logtimefmt = "%Y-%m-%d %H:%M:%S" random.seed(os.urandom(10)) +_always_override_destination = False + +logging_lock = threading.Lock() + def loglevelname(level): if (level == LOG_CRITICAL): return "Critical" @@ -55,19 +60,31 @@ def loglevelname(level): return "Unknown" -def log(msg, level=3): - # TODO: not thread safe +def log(msg, level=3, _override_destination = False): + global _always_override_destination + if loglevel >= level: timestamp = time.time() logstring = "["+time.strftime(logtimefmt)+"] ["+loglevelname(level)+"] "+msg + logging_lock.acquire() - if (logdest == LOG_STDOUT): + if (logdest == LOG_STDOUT or _always_override_destination): print(logstring) + logging_lock.release() - if (logdest == LOG_FILE and logfile != None): - file = open(logfile, "a") - file.write(logstring+"\n") - file.close() + elif (logdest == LOG_FILE and logfile != None): + try: + file = open(logfile, "a") + file.write(logstring+"\n") + file.close() + logging_lock.release() + except Exception as e: + logging_lock.release() + _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) + def rand(): result = random.random()