From fc576a32f5e08f499f5f42b4dfab8f71dd466366 Mon Sep 17 00:00:00 2001 From: Balazs Birtalan Date: Tue, 31 Oct 2023 07:20:44 +0000 Subject: [PATCH] Log message function in base logMsg(). base.where() give back the current class and function name. base code is cleaned up. --- base.py | 34 ++++++++++++++++++++++++++-------- browser.py | 4 ++-- callhandler.py | 8 ++++---- sightfactory.py | 8 ++++---- 4 files changed, 36 insertions(+), 18 deletions(-) diff --git a/base.py b/base.py index ee172f2..bf9b0d9 100644 --- a/base.py +++ b/base.py @@ -15,12 +15,12 @@ def init(): app = QApplication(sys.argv) mousectrl = Controller() sights = SightFactory() + data = {} data["move"] = False data["active_sight"] = "main" data["platform"] = {} data["platform"]["system"] = platform.system() - data["sight_processes"] = {} data["mouse_distance_x"] = 0 data["mouse_distance_y"] = 0 data["display_server"] = "" @@ -28,11 +28,11 @@ def init(): args = ["echo -n $XDG_SESSION_TYPE"] result = subprocess.check_output(args, shell=True) data["display_server"] = result.decode('ascii') - print("D "+data["display_server"]) data["base_dir"]=os.path.abspath(os.getcwd())+os.path.sep data["template_dir"]=data["base_dir"]+"place"+os.path.sep+"templates"+os.path.sep - #LOAD the template config content + #LOAD the template config.ini file's content loadConfigTemplate() + #Events mouseevent = MouseEvents() start() @@ -42,6 +42,20 @@ def frameSupported(): return False return True + +def where(): + # 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. + currentFuncName = lambda n=0: sys._getframe(n + 1).f_code.co_name + + try: + currentClassName = sys._getframe(1).f_locals["self"].__class__.__name__ + except KeyError: + currentClassName = None + + return str(currentClassName) + " -> " + currentFuncName(1) + def loadConfigTemplate(): config = configparser.ConfigParser() config.sections() @@ -51,14 +65,18 @@ def loadConfigTemplate(): data["default_template_page"] = config["DEFAULT"]["default_template_page"] data["default_template_dir"] = data["template_dir"]+data["default_template"]+os.path.sep data["sight_mode"] = config["DEFAULT"]["sight_mode"] - print("Template config is loaded!") + logMsg("LOADED","CONFIG.INI file", where()) else: - print("Missing 'DEFAULT' data from the templates config.ini folder. It can't start!") + logMsg("ERROR", "Missing 'DEFAULT' data from the templates config.ini folder. It can't start!", where()) def start(): # Creating the main Sight - sights.create("main", data["default_template_page"]); - sights.processes(); - sights.show("main"); + sights.create("main", data["default_template_page"]) + sights.show("main") app.exec() + +def logMsg(status, msg, where = ""): + if len(str(where)) > 0: + where = "("+where+")" + print("["+status+"]", str(msg), where, sep='\t') \ No newline at end of file diff --git a/browser.py b/browser.py index 53d43aa..281e7c8 100644 --- a/browser.py +++ b/browser.py @@ -15,5 +15,5 @@ class Browser(QWebEngineView): self.page().setWebChannel(self.channel) def loadContent(self, page): - print(base.data["default_template_dir"]+page+".html") - self.load(QUrl.fromLocalFile(base.data["default_template_dir"]+page+".html")) \ No newline at end of file + self.load(QUrl.fromLocalFile(base.data["default_template_dir"]+page+".html")) + base.logMsg("LOADED", base.data["default_template_dir"]+page+".html", base.where()) \ No newline at end of file diff --git a/callhandler.py b/callhandler.py index c40ea77..1b332cc 100644 --- a/callhandler.py +++ b/callhandler.py @@ -10,12 +10,12 @@ class CallHandler(QObject): self.sight = sight @pyqtSlot(str) - def console(self, t): - print(t) + def console(self, msg): + base.logMsg("CONSOLE", msg) @pyqtSlot(str, result=str) def test2(self, t): - print(t) + print(t+"s") return t + "_return" @pyqtSlot(result=str) @@ -119,7 +119,7 @@ class CallHandler(QObject): def message(self, label, sight_id_json, data): sight_list = json.loads(sight_id_json) - print("Message - " + label) + base.logMsg("MESSAGE",label, base.where()) if len(sight_list) == 0: for sight in base.sights.getSights().values(): sight.browser.page().runJavaScript("message('" + label + "', '"+data+"')", self.ready) diff --git a/sightfactory.py b/sightfactory.py index 20b8ec6..e2af49e 100644 --- a/sightfactory.py +++ b/sightfactory.py @@ -8,10 +8,10 @@ class SightFactory(): self.data = {} #Global data(s) for the sight(s) def create(self, id, page): - print("----- "+page+"-"+str(id)+"-------") + base.logMsg("CREATING","The page '" + str(page) + "' with '"+str(id)+"' id.",base.where()) if str(id) == "main" and self.checkKey(id): - print("The 'main' window already exist! You can't create more than one 'main' page sight!") + base.logMsg("WARNING", "The '"+str(id)+"' window already exist! You can't create more than one 'main' page sight!", base.where()) return None if base.data["sight_mode"] == "unique": @@ -39,9 +39,9 @@ class SightFactory(): def show(self, id): if self.checkKey(id): self.list[id].show() - print("The '"+str(id)+"' sight is showed!") + base.logMsg("SHOWED","The '"+str(id)+"' sight is showed!", base.where()) return True - print("This '"+str(id)+"' id isn't exist to show!") + base.logMsg("MISSING", "This '"+str(id)+"' id isn't exist to show!", base.where()) return False def getSights(self):