mirror of
https://github.com/titanscouting/tra-superscript.git
synced 2024-11-09 22:44:44 +00:00
8c28c24d60
moved important files to folder "competition" Former-commit-id: 59becb22abc3305a36e2876351e6c7306e3f551e
91 lines
1.7 KiB
Python
91 lines
1.7 KiB
Python
from logging import Logger as L
|
|
import datetime
|
|
import platform
|
|
import json
|
|
|
|
class Logger(L):
|
|
|
|
file = None
|
|
|
|
levels = {
|
|
0: "",
|
|
10:"[DEBUG] ",
|
|
20:"[INFO] ",
|
|
30:"[WARNING] ",
|
|
40:"[ERROR] ",
|
|
50:"[CRITICAL]",
|
|
}
|
|
|
|
targets = []
|
|
|
|
def __init__(self, verbose, profile, debug, file = None):
|
|
super().__init__("tra_logger")
|
|
|
|
self.file = file
|
|
|
|
if file != None:
|
|
self.targets.append(self._send_file)
|
|
|
|
if profile:
|
|
self.targets.append(self._send_null)
|
|
elif verbose:
|
|
self.targets.append(self._send_scli)
|
|
elif debug:
|
|
self.targets.append(self._send_scli)
|
|
else:
|
|
self.targets.append(self._send_null)
|
|
|
|
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):
|
|
for t in self.targets:
|
|
t(self.get_time_formatted() + "| " + self.levels[level] + ": " + msg)
|
|
|
|
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():
|
|
self.log(0, "#"+38*"-"+"#")
|
|
def box(s):
|
|
temp = "|"
|
|
temp += s
|
|
temp += (40-len(s)-2)*" "
|
|
temp += "|"
|
|
self.log(0, temp)
|
|
|
|
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() |