]> SALOME platform Git repositories - modules/smesh.git/commitdiff
Salome HOME
blocFissure : debug logging implementation
authorPaul RASCLE <paul.rascle@edf.fr>
Sun, 1 May 2016 21:34:06 +0000 (23:34 +0200)
committerPaul RASCLE <paul.rascle@edf.fr>
Sun, 1 May 2016 21:34:06 +0000 (23:34 +0200)
src/Tools/blocFissure/gmu/initLog.py

index bc427de1d3c0aedc04556887cc825e1c8c63deb2..57b887e60f42e599f8ed82f21b701840f28d7fe1 100644 (file)
@@ -1,6 +1,7 @@
 # -*- coding: utf-8 -*-
 
 import logging
+import os
 
 debug = 10
 info = 20
@@ -9,76 +10,72 @@ error = 40
 critical = 50
 
 loglevel = warning
+logging.basicConfig(format='%(funcName)s[%(lineno)d] %(message)s',
+                    level=logging.WARNING)
+ch = None
+fh = None
 
-def setDebug(logfile=None):
-  global loglevel
-  logging.shutdown()
+def setLogger(logfile, level, formatter):
+  global ch, fh
+  rootLogger = logging.getLogger('')
+  if fh is not None:
+    rootLogger.removeHandler(fh)
+    fh = None
+  if ch is not None:
+    rootLogger.removeHandler(ch)
+    ch = None
   if logfile:
-    logging.basicConfig(filename=logfile,
-                        format='%(relativeCreated)d %(funcName)s[%(lineno)d] %(message)s',
-                        level=logging.DEBUG)
+    if os.path.exists(logfile):
+      os.remove(logfile)
+    fh = logging.FileHandler(logfile)
+    rootLogger.addHandler(fh)
+    fh.setFormatter(formatter)
   else:
-    logging.basicConfig(format='%(relativeCreated)d %(funcName)s[%(lineno)d] %(message)s',
-                        level=logging.DEBUG)    
+    ch = logging.StreamHandler()
+    rootLogger.addHandler(ch)
+    ch.setFormatter(formatter)
+  rootLogger.setLevel(level)
+  
+  
+def setDebug(logfile=None):
+  global loglevel
   loglevel = debug
+  level = logging.DEBUG
+  formatter = logging.Formatter('%(relativeCreated)d %(funcName)s[%(lineno)d] %(message)s')
+  setLogger(logfile, level, formatter)
   logging.info('start Debug %s', loglevel)
 
 def setVerbose(logfile=None):
   global loglevel
-  logging.shutdown()
-  if logfile:
-    logging.basicConfig(filename=logfile,
-                        format='%(relativeCreated)d %(funcName)s[%(lineno)d] %(message)s',
-                        level=logging.INFO)
-  else:
-    logging.basicConfig(format='%(relativeCreated)d %(funcName)s[%(lineno)d] %(message)s',
-                        level=logging.INFO)    
   loglevel = info
+  level = logging.INFO
+  formatter = logging.Formatter('%(relativeCreated)d %(funcName)s[%(lineno)d] %(message)s')
+  setLogger(logfile, level, formatter)
   logging.info('start Verbose %s', loglevel)
 
 def setRelease(logfile=None):
   global loglevel
-  logging.shutdown()
-  if logfile:
-    logging.basicConfig(filename=logfile,
-                        format='%(funcName)s[%(lineno)d] %(message)s',
-                        level=logging.WARNING)
-  else:
-    logging.basicConfig(format='%(funcName)s[%(lineno)d] %(message)s',
-                        level=logging.WARNING)
   loglevel = warning
+  level = logging.WARNING
+  formatter = logging.Formatter('%(funcName)s[%(lineno)d] %(message)s')
+  setLogger(logfile, level, formatter)
   logging.warning('start Release %s', loglevel)
   
 def setUnitTests(logfile=None):
   global loglevel
-  logging.shutdown()
-  if logfile:
-    logging.basicConfig(filename=logfile,
-                        format='%(funcName)s[%(lineno)d] %(message)s',
-                        level=logging.CRITICAL)
-  else:
-    logging.basicConfig(format='%(funcName)s[%(lineno)d] %(message)s',
-                        level=logging.CRITICAL)
   loglevel = critical
+  level = logging.CRITICAL
+  formatter = logging.Formatter('%(funcName)s[%(lineno)d] %(message)s')
+  setLogger(logfile, level, formatter)
   logging.critical('start UnitTests %s', loglevel)
   
 def setPerfTests(logfile=None):
   global loglevel
-  logging.shutdown()
-  if logfile:
-    logging.basicConfig(filename=logfile,
-                        format='%(relativeCreated)d %(funcName)s[%(lineno)d] %(message)s',
-                        level=logging.CRITICAL)
-  else:
-    logging.basicConfig(format='%(relativeCreated)d %(funcName)s[%(lineno)d] %(message)s',
-                        level=logging.CRITICAL)    
   loglevel = critical
+  level = logging.CRITICAL
+  formatter = logging.Formatter('%(funcName)s[%(lineno)d] %(message)s')
+  setLogger(logfile, level, formatter)
   logging.info('start PerfTests %s', loglevel)
   
 def getLogLevel():
   return loglevel
-  
-  #logging.basicConfig(filename='myapp.log',
-  #                    format='%(asctime)s %(message)s',
-  #                    datefmt='%m/%d/%Y %I:%M:%S %p',
-  #                    level=logging.DEBUG)