Merge pull request #17 from dg9vh/debug-output

Adding Network-Debug-Output as hex-debug
This commit is contained in:
Antonio Matraia 2021-04-19 01:09:12 +02:00 committed by GitHub
commit 0776df2352
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -414,6 +414,15 @@ def ReadConfig(f,p):
if (t_reload_blacklist < 0.1): if (t_reload_blacklist < 0.1):
t_reload_blacklist = 0.1 t_reload_blacklist = 0.1
try:
debug = int(config['Network']['Debug'])
if (debug >= 1):
debug = 1
if (debug < 1):
debug = 0
except:
debug = 0
p.append(id) # 0 p.append(id) # 0
p.append(name) # 1 p.append(name) # 1
p.append(description) # 2 p.append(description) # 2
@ -427,6 +436,7 @@ def ReadConfig(f,p):
p.append(en_ext_cmd) #10 p.append(en_ext_cmd) #10
p.append(display_level) #11 p.append(display_level) #11
p.append(file_level) #12 p.append(file_level) #12
p.append(debug) #13
@ -454,6 +464,7 @@ def RunServer(config):
global IP_BL global IP_BL
global GW_LK global GW_LK
global IP_LK global IP_LK
global debug
host = '0.0.0.0' host = '0.0.0.0'
port = config[5] port = config[5]
s = socket.socket(socket.AF_INET,socket.SOCK_DGRAM) s = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
@ -513,6 +524,10 @@ def RunServer(config):
data_ns, addr = recvPackets.get() # blocked if queue is empty data_ns, addr = recvPackets.get() # blocked if queue is empty
data = sanitize_msg(data_ns) data = sanitize_msg(data_ns)
cmd = data[0:4] cmd = data[0:4]
if debug == 1:
hex_dump(data)
if (cmd == b'YSFP'): if (cmd == b'YSFP'):
pres = False pres = False
for c in clients: for c in clients:
@ -709,6 +724,8 @@ def printlog(log_level, mess):
global log_basename global log_basename
global file_rotate global file_rotate
global file_level global file_level
global debug
if file_rotate == "1": if file_rotate == "1":
log_file = log_basename + '-' + str(datetime.utcnow().strftime('%Y-%m-%d')) + '.log' log_file = log_basename + '-' + str(datetime.utcnow().strftime('%Y-%m-%d')) + '.log'
else: else:
@ -720,8 +737,13 @@ def printlog(log_level, mess):
filelog = open(log_file,'x') filelog = open(log_file,'x')
except: except:
pass pass
if isinstance(log_level, int):
if file_level <= log_level: if file_level <= log_level:
str_log = check_string('M: ' + str(datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S.%f'))[:-3] + ' ' + mess) str_log = check_string('M: ' + str(datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S.%f'))[:-3] + ' ' + mess)
else:
if log_level == "d" and debug == 1:
str_log = check_string('D: ' + str(datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S.%f'))[:-3] + ' ' + mess)
try: try:
filelog.write(str_log + '\n') filelog.write(str_log + '\n')
filelog.flush() filelog.flush()
@ -729,6 +751,25 @@ def printlog(log_level, mess):
pass pass
def hex_dump(data):
try:
n = 0
b = data[0:16]
while b:
s1 = " ".join([f"{i:02x}" for i in b]) # hex string
s1 = s1[0:23] + " " + s1[23:] # insert extra space between groups of 8 hex values
s2 = "".join([chr(i) if 32 <= i <= 127 else "." for i in b]) # ascii string; chained comparison
message = f"{n * 16:08x} {s1:<48} |{s2}|"
printlog("d", message)
n += 1
b = data[n*16:(n+1)*16]
except Exception as e:
print(__file__, ": ", type(e).__name__, " - ", e, sep="", file=sys.stderr)
######## main ######## ######## main ########
version = '20210417' version = '20210417'
@ -754,6 +795,8 @@ log_basename = config[3] + '/' + config[4]
file_rotate = config[8] file_rotate = config[8]
display_level = config[11] display_level = config[11]
file_level = config[12] file_level = config[12]
debug = config[13]
### log ### log
if file_rotate == "1": if file_rotate == "1":
log_file = log_basename + '-' + str(datetime.utcnow().strftime('%Y-%m-%d')) + '.log' log_file = log_basename + '-' + str(datetime.utcnow().strftime('%Y-%m-%d')) + '.log'
@ -775,6 +818,7 @@ IP_BL = []
GW_LK = [] GW_LK = []
IP_LK = [] IP_LK = []
RunServer(config) RunServer(config)