sightplace-rep/log.py

123 lines
4.3 KiB
Python

import sys
import os.path
import datetime
import html
from log_event_codes import loge_codes
import base
class Log():
def __init__(self, specify=1):
loge_codes.addEventCode("LOG0001X", "The event code is not exist!")
self.specify = specify
self.debug = False
self.debug_level = 1
self.event_on = True
self.debug_modes = {}
self.debug_modes[0] = ["info", "console", "message", "success"]
self.debug_modes[1] = ["alert", "warning", "event"]
self.debug_modes[2] = ["failed", "error"]
self.debug_modes[3] = ["all"]
print("--- LOG STARTED ----")
def debugOn(self):
self.debug = True
def debugOff(self):
self.debug = False
def eventOn(self):
self.event_on = True
def eventOff(self):
self.event_on = False
def debugLevel(self, number):
if number < 1:
self.debug_level = 1
elif number > 3:
self.debug_level = 3
else:
self.debug_level = number
def whereis(self):
# for current func name, specify 0 or no argument.
# for name of caller of current func, specify 1.
# for name of caller of caller of current func, specify 2. etc.
# self.specify
currentFuncName = lambda n=0: sys._getframe(n + 1).f_code.co_name
try:
currentClassName = sys._getframe(self.specify).f_locals["self"].__class__.__name__
except KeyError:
currentClassName = None
if currentClassName is None:
currentClassName = "No Class"
return str(currentClassName) + " -> " + currentFuncName(self.specify)
def byCode(self, event_code, msg = "", wheres = ""):
code_data = loge_codes.getEventCode(event_code)
if code_data is not None:
if event_code[:3].upper() == "LOG":
self.msg(code_data["type"], event_code, msg, wheres, True)
else:
self.msg(code_data["type"], event_code, msg, wheres)
else:
self.byCode("LOG0001X", event_code + " is not exist!")
def msg(self, type, event_code, msg="", wheres="", serious_error=False):
dnow = datetime.datetime.now()
if serious_error is True:
type = "SERIOUS"
code_data = loge_codes.getEventCode(event_code)
event_state = ""
if code_data is not None:
if len(code_data["event_state"]) > 0:
event_state = code_data["event_state"]
msg = event_state+" "+code_data["details"] + " " + msg
if len(wheres) == 0:
wheres = self.whereis()
""" LOG FILE WRITING """
if type not in self.debug_modes[0] and type.upper() != "EVENT":
filename = str(dnow.strftime("%d"))+"-"+str(dnow.strftime("%m"))+"-"+str(dnow.year)+".log"
directory = '.'+os.path.sep+"logs"+os.path.sep
file_path = os.path.join(directory, filename)
if not os.path.isdir(directory):
os.mkdir(directory)
content = "["+type.upper()+"]\t{"+event_code+"}\t"+str(msg)+"\t("+wheres+")\t"+"<"+str(dnow)+">\n"
if os.path.exists(file_path):
with open(file_path, 'a') as file:
file.write(content)
file.close()
else:
f = open(file_path, "w")
f.write(content)
f.close()
""" END LOG FILE WRITING """
""" LOG DISPLAY """
if serious_error is False:
if type.upper() == "EVENT" and self.event_on is False:
return
if type not in self.debug_modes[0] and self.debug_level != 3:
if self.debug is True and type not in self.debug_modes[self.debug_level]:
return
dict = {}
dict["type"] = type.upper()
dict["event_code"] = str(event_code)
dict["msg"] = msg.replace('\\', '\\\\')
dict["event_state"] = str(event_state)
dict["date"] = str(dnow)
base.sights.sendMessage("logs", ["sight-admin"], dict)
print("["+type.upper()+"]",event_code, str(msg), wheres, "["+str(dnow)+"]", sep='\t')
""" END LOG DISPLAY """
def console(self, msg, wheres = ""):
self.msg("console", "", msg, wheres)
def message(self, msg, wheres = ""):
self.msg("message", "", msg, wheres)