| 
									
										
										
										
											2023-11-02 19:17:25 +00:00
										 |  |  | import sys | 
					
						
							|  |  |  | import os.path | 
					
						
							|  |  |  | import datetime | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import html | 
					
						
							| 
									
										
										
										
											2023-11-09 20:22:58 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | from core.messenger.broadcast import Broadcast | 
					
						
							|  |  |  | from core.registration.record import * | 
					
						
							| 
									
										
										
										
											2023-11-02 19:17:25 +00:00
										 |  |  | 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() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-09 13:57:36 +00:00
										 |  |  |         # IT IS FREEZE BY EVENT (runJavascript cant execute lot of request in one time! | 
					
						
							|  |  |  |         if type.upper() != "EVENT": | 
					
						
							| 
									
										
										
										
											2023-11-09 20:22:58 +00:00
										 |  |  |             d = {"type": type.upper(), "event_code": str(event_code)} | 
					
						
							| 
									
										
										
										
											2023-11-09 13:57:36 +00:00
										 |  |  |             msg = msg.replace('\\', '\\\\') | 
					
						
							|  |  |  |             msg = msg.replace('\'', "") | 
					
						
							| 
									
										
										
										
											2023-11-09 20:22:58 +00:00
										 |  |  |             d["msg"] = msg | 
					
						
							|  |  |  |             d["event_state"] = str(event_state) | 
					
						
							|  |  |  |             d["date"] = str(dnow) | 
					
						
							|  |  |  |             r = Record(RecordHeader("message", "logs", ["sight-admin"]), RecordData(d)) | 
					
						
							|  |  |  |             msg_bc = Broadcast() | 
					
						
							|  |  |  |             msg_bc.sendMessage(r) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             #base.sights.sendMessage("logs", ["sight-admin"], dict) | 
					
						
							| 
									
										
										
										
											2023-11-09 13:57:36 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-02 19:17:25 +00:00
										 |  |  |         """ 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 """ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-09 13:57:36 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-02 19:17:25 +00:00
										 |  |  |         """ 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 | 
					
						
							| 
									
										
										
										
											2023-11-09 13:57:36 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-02 19:17:25 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         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) |