Reticulum/RNS/__init__.py

124 lines
3.1 KiB
Python
Raw Normal View History

2016-06-03 19:02:02 +02:00
import os
2018-04-16 22:04:28 +02:00
import sys
2016-06-03 19:02:02 +02:00
import glob
2018-03-19 16:39:08 +01:00
import time
import random
import threading
2016-06-03 19:02:02 +02:00
2021-08-19 14:10:37 +02:00
from ._version import __version__
from .Reticulum import Reticulum
2018-03-16 10:50:37 +01:00
from .Identity import Identity
2021-09-02 20:35:42 +02:00
from .Link import Link, RequestReceipt
2018-03-16 10:50:37 +01:00
from .Transport import Transport
2018-03-19 16:39:08 +01:00
from .Destination import Destination
from .Packet import Packet
2018-04-17 17:46:48 +02:00
from .Packet import PacketReceipt
from .Resource import Resource, ResourceAdvertisement
2018-03-16 10:50:37 +01:00
2016-06-03 19:02:02 +02:00
modules = glob.glob(os.path.dirname(__file__)+"/*.py")
2018-03-19 16:39:08 +01:00
__all__ = [ os.path.basename(f)[:-3] for f in modules if not f.endswith('__init__.py')]
LOG_CRITICAL = 0
LOG_ERROR = 1
LOG_WARNING = 2
LOG_NOTICE = 3
LOG_INFO = 4
LOG_VERBOSE = 5
LOG_DEBUG = 6
2018-04-23 23:42:16 +02:00
LOG_EXTREME = 7
2018-03-19 16:39:08 +01:00
2020-08-13 12:15:56 +02:00
LOG_STDOUT = 0x91
2018-03-19 16:39:08 +01:00
LOG_FILE = 0x92
2021-10-08 19:23:10 +02:00
LOG_MAXSIZE = 5*1024*1024
2020-08-13 12:15:56 +02:00
loglevel = LOG_NOTICE
2018-03-19 16:39:08 +01:00
logfile = None
logdest = LOG_STDOUT
logtimefmt = "%Y-%m-%d %H:%M:%S"
random.seed(os.urandom(10))
_always_override_destination = False
logging_lock = threading.Lock()
2018-03-19 16:39:08 +01:00
def loglevelname(level):
2020-08-13 12:15:56 +02:00
if (level == LOG_CRITICAL):
return "Critical"
if (level == LOG_ERROR):
return "Error"
if (level == LOG_WARNING):
return "Warning"
if (level == LOG_NOTICE):
return "Notice"
if (level == LOG_INFO):
return "Info"
if (level == LOG_VERBOSE):
return "Verbose"
if (level == LOG_DEBUG):
return "Debug"
if (level == LOG_EXTREME):
return "Extra"
return "Unknown"
2018-03-19 16:39:08 +01:00
2021-08-19 14:10:37 +02:00
def version():
return __version__
def log(msg, level=3, _override_destination = False):
global _always_override_destination
2020-08-13 12:15:56 +02:00
if loglevel >= level:
timestamp = time.time()
logstring = "["+time.strftime(logtimefmt)+"] ["+loglevelname(level)+"] "+msg
logging_lock.acquire()
2018-03-19 16:39:08 +01:00
2021-08-19 14:10:37 +02:00
if (logdest == LOG_STDOUT or _always_override_destination or _override_destination):
2020-08-13 12:15:56 +02:00
print(logstring)
logging_lock.release()
2018-03-19 16:39:08 +01:00
elif (logdest == LOG_FILE and logfile != None):
try:
file = open(logfile, "a")
file.write(logstring+"\n")
file.close()
2021-10-08 19:23:10 +02:00
if os.path.getsize(logfile) > LOG_MAXSIZE:
prevfile = logfile+".1"
if os.path.isfile(prevfile):
os.unlink(prevfile)
os.rename(logfile, prevfile)
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)
2018-03-19 18:11:50 +01:00
def rand():
2020-08-13 12:15:56 +02:00
result = random.random()
return result
2018-03-19 18:11:50 +01:00
def hexrep(data, delimit=True):
2020-08-13 12:15:56 +02:00
delimiter = ":"
if not delimit:
delimiter = ""
hexrep = delimiter.join("{:02x}".format(c) for c in data)
return hexrep
2018-03-20 12:32:41 +01:00
def prettyhexrep(data):
2020-08-13 12:15:56 +02:00
delimiter = ""
hexrep = "<"+delimiter.join("{:02x}".format(c) for c in data)+">"
return hexrep
2018-04-16 22:04:28 +02:00
def panic():
os._exit(255)
def exit():
print("")
sys.exit(0)