]> SALOME platform Git repositories - modules/kernel.git/commitdiff
Salome HOME
Call site in the log message as in C++
authorAnthony Geay <anthony.geay@edf.fr>
Mon, 5 Sep 2022 07:57:31 +0000 (09:57 +0200)
committerAnthony Geay <anthony.geay@edf.fr>
Mon, 5 Sep 2022 07:57:31 +0000 (09:57 +0200)
bin/runSalomeCommon.py

index 8ecb2489d37a1d8f52ceaf3b09865596d78dd90b..7ff22727dc9b530a240d82e45a53a27de97688ff 100755 (executable)
@@ -44,12 +44,37 @@ class ColoredFormatter(logging.Formatter):
     def format(self, record):
         RESET_SEQ = "\033[0m"
         COLOR_SEQ = "\033[1;%dm"
+        import inspect
+        frame = inspect.currentframe()
+        for i in range(8):
+            frame = frame.f_back
         record.levelname = COLOR_SEQ % ColoredFormatter.COLORS[record.levelname] + record.levelname + RESET_SEQ
+        record.msg = "{} ( callsite is {} of file \"{}\" at line {} )".format(record.msg, frame.f_code.co_name,inspect.getsourcefile(frame),inspect.getlineno(frame) )
+        return logging.Formatter.format(self, record)
+
+class BackTraceFormatter(logging.Formatter):
+    def __init__(self, *args, **kwargs):
+        logging.Formatter.__init__(self, *args, **kwargs)
+    def format(self, record):
+        import inspect
+        frame = inspect.currentframe()
+        # go upward of the stack to catch the effective callsite. Not very steady....
+        # should be replaced by an analysis of frame.f_code
+        for i in range(8):
+            frame = frame.f_back
+        record.msg = "{} ( callsite is {} of file \"{}\" at line {} )".format(record.msg, frame.f_code.co_name,inspect.getsourcefile(frame),inspect.getlineno(frame) )
         return logging.Formatter.format(self, record)
 
 def setVerbose(verbose):
+    from packaging import version
+    current_version = version.parse("{}.{}".format(sys.version_info.major,sys.version_info.minor))
+    version_ref = version.parse("3.5.0")
     global logger
-    formatter = logging.Formatter('%(levelname)s : %(asctime)s : %(message)s ',style='%')
+    formatter = None
+    if current_version >= version_ref:
+        formatter = BackTraceFormatter('%(levelname)s : %(asctime)s : %(message)s ',style='%')
+    else:
+        formatter = logging.Formatter('%(levelname)s : %(asctime)s : %(message)s ',style='%')
     formatter.default_time_format = '%H:%M:%S'
     formatter.default_msec_format = "%s.%03d"
     stream_handler = logging.StreamHandler()