[elbe-devel] [PATCH 4/7] elbepack: log: move handler registry out of handler class

Thomas Weißschuh thomas.weissschuh at linutronix.de
Mon Aug 19 16:50:16 CEST 2024


Going through classmethods everytime serves no purpose.
Simplify the code.

Signed-off-by: Thomas Weißschuh <thomas.weissschuh at linutronix.de>
---
 elbepack/log.py | 46 +++++++++++++++++-----------------------------
 1 file changed, 17 insertions(+), 29 deletions(-)

diff --git a/elbepack/log.py b/elbepack/log.py
index 88e331f2eb1f..b5a7c222f03e 100644
--- a/elbepack/log.py
+++ b/elbepack/log.py
@@ -31,52 +31,40 @@ class LoggingQueue(collections.deque):
             self.max_level = level
 
 
-class QHandler(logging.Handler):
+_queues = {}
 
-    queues: dict[str, LoggingQueue] = {}
 
+class QHandler(logging.Handler):
     def __init__(self, target, *args, **kwargs):
         super().__init__(*args, **kwargs)
-        if target not in QHandler.queues:
-            QHandler.queues[target] = LoggingQueue()
-        self.Q = QHandler.queues[target]
+        if target not in _queues:
+            _queues[target] = LoggingQueue()
+        self.Q = _queues[target]
 
     def emit(self, record):
         self.Q.append(self.format(record))
         self.Q.note_level(record.levelno)
 
-    @classmethod
-    def pop(cls, target):
-        try:
-            return cls.queues[target].popleft()
-        except (IndexError, KeyError):
-            return ''
-
-    @classmethod
-    def max_level(cls, target):
-        try:
-            return cls.queues[target].max_level
-        except (IndexError, KeyError):
-            return logging.NOTSET
-
-    @classmethod
-    def reset_level(cls, target):
-        try:
-            cls.queues[target].max_level = logging.NOTSET
-        except (IndexError, KeyError):
-            pass
-
 
 def read_loggingQ(proj):
-    return QHandler.pop(proj)
+    try:
+        return _queues[proj].popleft()
+    except (IndexError, KeyError):
+        return ''
 
 
 def read_maxlevel(proj):
-    return QHandler.max_level(proj)
+    try:
+        return _queues[proj].max_level
+    except (IndexError, KeyError):
+        return logging.NOTSET
 
 
 def reset_level(proj):
-    QHandler.reset_level(proj)
+    try:
+        _queues[proj].max_level = logging.NOTSET
+    except (IndexError, KeyError):
+        pass
 
 
 class ThreadFilter(logging.Filter):

-- 
2.46.0



More information about the elbe-devel mailing list