Added thread locking to log output. Various housekeeping.

This commit is contained in:
Mark Qvist 2021-05-03 20:24:44 +02:00
parent 178c69e361
commit 54206d9101
4 changed files with 31 additions and 14 deletions

View File

@ -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:

View File

@ -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):

View File

@ -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):

View File

@ -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()
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)
if (logdest == LOG_FILE and logfile != None):
file = open(logfile, "a")
file.write(logstring+"\n")
file.close()
def rand():
result = random.random()