2022-02-19 08:52:49 +00:00
|
|
|
from logging import Logger as L
|
|
|
|
import datetime
|
2021-06-12 07:09:26 +00:00
|
|
|
import platform
|
2022-02-19 08:52:49 +00:00
|
|
|
import json
|
2021-06-12 07:09:26 +00:00
|
|
|
|
2022-02-19 08:52:49 +00:00
|
|
|
class Logger(L):
|
|
|
|
|
|
|
|
file = None
|
|
|
|
|
|
|
|
levels = {
|
2022-02-19 22:58:58 +00:00
|
|
|
0: "",
|
2022-02-19 08:52:49 +00:00
|
|
|
10:"[DEBUG] ",
|
|
|
|
20:"[INFO] ",
|
|
|
|
30:"[WARNING] ",
|
|
|
|
40:"[ERROR] ",
|
|
|
|
50:"[CRITICAL]",
|
|
|
|
}
|
|
|
|
|
2022-02-19 22:58:58 +00:00
|
|
|
targets = []
|
|
|
|
|
2022-02-19 08:52:49 +00:00
|
|
|
def __init__(self, verbose, profile, debug, file = None):
|
|
|
|
super().__init__("tra_logger")
|
2022-02-19 22:58:58 +00:00
|
|
|
|
2022-02-19 08:52:49 +00:00
|
|
|
self.file = file
|
2022-02-19 22:58:58 +00:00
|
|
|
|
|
|
|
if file != None:
|
|
|
|
self.targets.append(self._send_file)
|
|
|
|
|
2022-02-19 08:52:49 +00:00
|
|
|
if profile:
|
2022-02-19 22:58:58 +00:00
|
|
|
self.targets.append(self._send_null)
|
2022-02-19 08:52:49 +00:00
|
|
|
elif verbose:
|
2022-02-19 22:58:58 +00:00
|
|
|
self.targets.append(self._send_scli)
|
2022-02-19 08:52:49 +00:00
|
|
|
elif debug:
|
2022-02-19 22:58:58 +00:00
|
|
|
self.targets.append(self._send_scli)
|
2022-02-19 08:52:49 +00:00
|
|
|
else:
|
2022-02-19 22:58:58 +00:00
|
|
|
self.targets.append(self._send_null)
|
2022-02-19 08:52:49 +00:00
|
|
|
|
|
|
|
def _send_null(self, msg):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def _send_scli(self, msg):
|
|
|
|
print(msg)
|
|
|
|
|
|
|
|
def _send_file(self, msg):
|
|
|
|
f = open(self.file, 'a')
|
|
|
|
f.write(msg + "\n")
|
|
|
|
f.close()
|
|
|
|
|
|
|
|
def get_time_formatted(self):
|
|
|
|
return datetime.datetime.now().strftime("%Y/%m/%d %H:%M:%S %Z")
|
|
|
|
|
|
|
|
def log(self, level, msg):
|
2022-02-19 22:58:58 +00:00
|
|
|
for t in self.targets:
|
|
|
|
t(self.get_time_formatted() + "| " + self.levels[level] + ": " + msg)
|
2022-02-19 08:52:49 +00:00
|
|
|
|
|
|
|
def debug(self, msg):
|
|
|
|
self.log(10, msg)
|
|
|
|
|
|
|
|
def info(self, msg):
|
|
|
|
self.log(20, msg)
|
|
|
|
|
|
|
|
def warning(self, msg):
|
|
|
|
self.log(30, msg)
|
|
|
|
|
|
|
|
def error(self, msg):
|
|
|
|
self.log(40, msg)
|
|
|
|
|
|
|
|
def critical(self, msg):
|
|
|
|
self.log(50, msg)
|
|
|
|
|
|
|
|
def splash(self, version):
|
|
|
|
|
|
|
|
def hrule():
|
2022-02-19 22:58:58 +00:00
|
|
|
self.log(0, "#"+38*"-"+"#")
|
2022-02-19 08:52:49 +00:00
|
|
|
def box(s):
|
|
|
|
temp = "|"
|
|
|
|
temp += s
|
|
|
|
temp += (40-len(s)-2)*" "
|
|
|
|
temp += "|"
|
2022-02-19 22:58:58 +00:00
|
|
|
self.log(0, temp)
|
2022-02-19 08:52:49 +00:00
|
|
|
|
|
|
|
hrule()
|
|
|
|
box(" superscript version: " + version)
|
|
|
|
box(" os: " + platform.system())
|
|
|
|
box(" python: " + platform.python_version())
|
|
|
|
hrule()
|
|
|
|
|
|
|
|
def save_module_to_file(self, module, data, results):
|
|
|
|
f = open(module + ".log", "w")
|
|
|
|
json.dump({"data": data, "results":results}, f, ensure_ascii=False, indent=4)
|
|
|
|
f.close()
|