gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[GNUnet-SVN] [taler-taler-util] 18/51: 4453.


From: gnunet
Subject: [GNUnet-SVN] [taler-taler-util] 18/51: 4453.
Date: Mon, 23 Sep 2019 22:02:09 +0200

This is an automated email from the git hooks/post-receive script.

ng0 pushed a commit to branch master
in repository taler-util.

commit 9185a0c008ec6760f2425363e9a17a0fc268373f
Author: Marcello Stanisci <address@hidden>
AuthorDate: Wed Feb 6 14:12:39 2019 +0100

    4453.
    
    Completing the implementation of GNUNET_LOG
    and GNUNET_FORCE_LOG interpretation.
---
 python/log/gnunet_log.py | 142 ++++++++++++++++++++++-------------------------
 python/log/test.py       |  39 ++++++++++---
 2 files changed, 98 insertions(+), 83 deletions(-)

diff --git a/python/log/gnunet_log.py b/python/log/gnunet_log.py
index dcce72f..9dc5e09 100755
--- a/python/log/gnunet_log.py
+++ b/python/log/gnunet_log.py
@@ -5,56 +5,78 @@
 import os
 import logging
 
-class GnunetLogLevel:
-
-    def __init__(self, nativeLoggingLevel, stringification):
-        self.native_logging_level = nativeLoggingLevel
-        self.stringification = stringification
-    def getNativeLevel(self):
-        return self.native_logging_level
-    def __str__(self):
-        return self.stringification
-
 class LogDefinition:
-    
     def __init__(self, component, forced, loglevel):
+
         self.component = component
         self.forced = forced
+        # string here, comes from env after all.
         self.loglevel = loglevel
 
-class GnunetLogger:
+class GnunetLoglevel:
+    def __init__(self, string, level, function):
+        self.string = string
+        self.level = level
+        self.function = function
+
+    def __str__(self):
+        return self.string
     
-    ERROR = GnunetLogLevel(logging.ERROR, "ERROR")
-    WARNING = GnunetLogLevel(logging.WARNING, "WARNING")
-    INFO = GnunetLogLevel(logging.INFO, "INFO")
-    DEBUG = GnunetLogLevel(logging.DEBUG, "DEBUG")
-
-    level_map = {"ERROR": ERROR,
-                 "WARNING": WARNING,
-                 "INFO": INFO,
-                 "DEBUG": DEBUG}
-
-    def __init__(self, component, loglevel=None):
-        print("Log setup, component: %s, level: %s" % (component, 
str(loglevel)))
+    def getLevel(self):
+        return self.level
+
+    def getFunction(self):
+        return self.function
+
+class GnunetLogger:
+
+    def __init__(self, component):
+        self.logger = logging.getLogger(component)
+        self.ERROR = GnunetLoglevel("ERROR", logging.ERROR, self.logger.error)
+        self.WARNING = GnunetLoglevel("WARNING", logging.WARNING, 
self.logger.warning)
+        self.INFO = GnunetLoglevel("INFO", logging.INFO, self.logger.info)
+        self.DEBUG = GnunetLoglevel("DEBUG", logging.DEBUG, self.logger.debug)
+
         self.component = component
-        self.loglevel = loglevel
-        # just because it _needs_ to be set in the beginning.
+        self.loglevel = None
+
+        # Setting the *logging* loglevel in order to have the
+        # chance of changing the *logger* (object) loglevel along the
+        # execution.  So this particular loglevel has no relevance
+        # (might have been any other loglevel).
         logging.basicConfig(level=logging.INFO)
-        self.logger = logging.getLogger(component)
+
         self.no_forced_definitions = True
+        self.definitions = list()
 
         if os.environ.get("GNUNET_LOG"):
-            parse_definitions(os.environ.get("GNUNET_LOG", False))
+            self.__parse_definitions(os.environ.get("GNUNET_LOG"), False)
 
         if os.environ.get("GNUNET_FORCE_LOG"):
             self.no_forced_definitions = False
-            parse_definitions(os.environ.get("GNUNET_LOG", True))
+            self.__parse_definitions(os.environ.get("GNUNET_FORCE_LOG"), True)
 
-    def __internal_log(self, message, message_loglevel, ruling_loglevel):
+    def string_to_loglevel(self, level):
+        
+        level_map = {
+            "ERROR": self.ERROR,
+            "WARNING": self.WARNING,
+            "INFO": self.INFO,
+            "DEBUG": self.DEBUG}
+
+        # Defaults to INFO.
+        return level_map.get(level, self.INFO)
+
+    def setup(self, loglevel):
+        self.loglevel = loglevel
+
+    def log(self, message, message_loglevel):
 
         # Ordinary case (level setup + nothing forced).
-        if ruling_loglevel and self.no_forced_definitions:
-            self.logger.setLevel(level=ruling_loglevel.getNativeLevel())
+        if self.loglevel and self.no_forced_definitions:
+            self.logger.setLevel(level=self.loglevel.getLevel())
+            message_loglevel.getFunction()(message)
+            return
 
         # We crawl through GNUNET_FORCE_LOG definitions,
         # or GNUNET_LOG (in case of non-forced definition
@@ -64,9 +86,11 @@ class GnunetLogger:
                 # Temporarily checking only the component name.
                 # To be extended with all the others definition-parts.
                 if self.component == defi.component:
-                    self.logger.setLevel(level=defi.loglevel)
+                    self.logger.setLevel(
+                        
level=self.string_to_loglevel(defi.loglevel).getLevel())
+                    message_loglevel.getFunction()(message)
+                    return
 
-        #
         # If the flow got here, then one of the following
         # may hold.
         #
@@ -75,30 +99,20 @@ class GnunetLogger:
         #     Possibly, there also exists a default loglevel.
         
         if self.loglevel:
-            self.logger.setLevel(level=self.loglevel)
+            self.logger.setLevel(
+                level=self.loglevel.getLevel())
 
-
-        #
         # (2) GNUNET_FORCE_LOG was NOT given and neither was
         #     a default loglevel, and also a unforced definition
         #     wasn't found.  Must assign a made-up loglevel.
-        #
         
-        self.logger.setLevel(level=logging.INFO)
-
-        if GnunetLogger.ERROR == message_loglevel:
-            fn = self.logger.error
-        if GnunetLogger.WARNING == message_loglevel:
-            fn = self.logger.warning
-        if GnunetLogger.INFO == message_loglevel:
-            fn = self.logger.info
-        if GnunetLogger.DEBUG == message_loglevel:
-            fn = self.logger.debug
+        else:
+            self.logger.setLevel(level=logging.INFO)
 
-        fn(message)
+        message_loglevel.getFunction()(message)
 
-    def parse_definitions(data, forced):
-        gfl_split = gfl.split("/")
+    def __parse_definitions(self, line, forced):
+        gfl_split = line.split("/")
         for component in gfl_split:
             gfl_split_split = component.split(";")
 
@@ -108,28 +122,6 @@ class GnunetLogger:
 
             definition = LogDefinition(gfl_split_split[0],
                                        forced,
-                                       loglevel=glf_split_split[4])
-            self.definitions.push(definition)
-
-    def log(self, message, loglevel):
-        if not os.environ.get("GNUNET_FORCE_LOG"):
-            self.__internal_log(message, loglevel, self.loglevel)
-            return
-        gfl = os.environ.get("GNUNET_FORCE_LOG")
-        gfl_split = gfl.split("/")
-        for component in gfl_split:
-            gfl_split_split = component.split(";")
-            if 5 != len(gfl_split_split):
-                print("warning: GNUNET_FORCE_LOG is malformed")
-                self.__internal_log(message, loglevel, self.loglevel)
-                return
-            # if component chunk is not empty and different from
-            # this component name, then abort, otherwise further
-            # proceed.
-            if gfl_split_split[0] and self.component != gfl_split_split[0]:
-                return
+                                       loglevel=gfl_split_split[4])
 
-            # for simplicity, and for now, we supersede the check
-            # of the 'file', 'function' and 'from line [to line]' bits.
-            # Just check the loglevel.
-            self.__internal_log(message, loglevel, 
GnunetLogger.level_map.get(gfl_split_split[4]))
+            self.definitions.append(definition)
diff --git a/python/log/test.py b/python/log/test.py
index 684a839..2cfaff0 100755
--- a/python/log/test.py
+++ b/python/log/test.py
@@ -2,24 +2,47 @@
 
 from gnunet_log import GnunetLogger as GL
 import os
-
-gl = GL("gnunetish-python-logging", GL.INFO)
+import sys
 
 # 1st test _without_ GNUNET_FORCE_LOG being defined.
 print("Testing with GNUNET_FORCE_LOG *unset*")
 if os.environ.get("GNUNET_FORCE_LOG"):
     del os.environ["GNUNET_FORCE_LOG"]
 
-gl.log("I am a INFO message", GL.INFO)
-gl.log("I am a WARNING message", GL.WARNING)
-gl.log("I am a DEBUG message, and I should *not* get displayed", GL.WARNING)
+gl = GL("gnunetish-python-logging")
+print("Setting loglevel to INFO")
+gl.setup(gl.INFO)
+
+gl.log("I am a INFO message", gl.INFO)
+gl.log("I am a WARNING message", gl.WARNING)
+gl.log("I am a DEBUG message, and I should *not* get displayed", gl.DEBUG)
 
 print("Testing with GNUNET_FORCE_LOG *set* to WARNING")
 os.environ["GNUNET_FORCE_LOG"] = "gnunetish-python-logging;;;;WARNING"
 
-gl.log("I am a INFO message, and I should *not* get displayed", GL.INFO)
-gl.log("I am a ERROR message, and I should get displayed", GL.ERROR)
+gl = GL("gnunetish-python-logging")
+gl.setup(gl.INFO)
+
+gl.log("I am a INFO message, and I should *not* get displayed", gl.INFO)
+gl.log("I am a ERROR message, and I should get displayed", gl.ERROR)
 
 print("Testing with GNUNET_FORCE_LOG *set* to DEBUG")
 os.environ["GNUNET_FORCE_LOG"] = "gnunetish-python-logging;;;;DEBUG"
-gl.log("I am a DEBUG message, and I should get displayed", GL.DEBUG)
+
+gl = GL("gnunetish-python-logging")
+gl.setup(gl.INFO)
+
+gl.log("I am a DEBUG message, and I should get displayed", gl.DEBUG)
+gl.log("I am a WARNING message, and I should get displayed", gl.WARNING)
+
+print("Testing with NO forced definitions neither one setup")
+del os.environ["GNUNET_FORCE_LOG"]
+gl = GL("gnunetish-python-logging")
+gl.log("I am a DEBUG message and I should *not* get displayed", gl.DEBUG)
+gl.log("I am a INFO message and I should get displayed (for default reasons)", 
gl.INFO)
+
+print("Testing with GNUNET_LOG=gnunetish-python-logging;;;;ERROR")
+os.environ["GNUNET_LOG"] = "gnunetish-python-logging;;;;ERROR"
+gl = GL("gnunetish-python-logging")
+gl.log("I am a WARNING message and I should *not* get displayed", gl.WARNING)
+gl.log("I am a ERROR message and I should get displayed", gl.ERROR)

-- 
To stop receiving notification emails like this one, please contact
address@hidden.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]