Salome HOME
Minor source update for OM compatibility
[modules/adao.git] / src / daComposant / daCore / ExtendedLogging.py
index 2da4d7e654bc9a0f3aca93aee4135c49eb5d06f5..5918581c5d1a2e0d6b3f9ae1b9f55f4f73363057 100644 (file)
@@ -1,6 +1,6 @@
 # -*- coding: utf-8 -*-
 #
-# Copyright (C) 2008-2022 EDF R&D
+# Copyright (C) 2008-2024 EDF R&D
 #
 # This library is free software; you can redistribute it and/or
 # modify it under the terms of the GNU Lesser General Public
@@ -79,7 +79,7 @@ import functools
 import time
 from daCore import PlatformInfo
 
-LOGFILE = os.path.join(os.path.abspath(os.curdir),"AdaoOutputLogfile.log")
+LOGFILE = os.path.join(os.path.abspath(os.curdir), "AdaoOutputLogfile.log")
 
 # ==============================================================================
 class ExtendedLogging(object):
@@ -87,36 +87,57 @@ class ExtendedLogging(object):
     Logger général pour disposer conjointement de la sortie standard et de la
     sortie sur fichier
     """
+    __slots__ = ("__logfile")
+
     def __init__(self, level=logging.WARNING):
         """
         Initialise un logging à la console pour TOUS les niveaux de messages.
         """
-        logging.basicConfig(
-            format = '%(levelname)-8s %(message)s',
-            level  = level,
-            stream = sys.stdout,
+        if sys.version_info.major <= 3 and sys.version_info.minor < 8:
+            if logging.getLogger().hasHandlers():
+                while logging.getLogger().hasHandlers():
+                    logging.getLogger().removeHandler( logging.getLogger().handlers[-1] )
+                __sys_stdout = logging.StreamHandler(sys.stdout)
+                __sys_stdout.setFormatter(logging.Formatter('%(levelname)-8s %(message)s'))
+                logging.getLogger().addHandler(__sys_stdout)
+            else:
+                logging.basicConfig(
+                    format = '%(levelname)-8s %(message)s',
+                    level  = level,
+                    stream = sys.stdout,
+                )
+        else:  # Actif lorsque Python > 3.7
+            logging.basicConfig(
+                format = '%(levelname)-8s %(message)s',
+                level  = level,
+                stream = sys.stdout,
+                force  = True,
             )
         self.__logfile = None
         #
         # Initialise l'affichage de logging
         # ---------------------------------
-        p = PlatformInfo.PlatformInfo()
+        lpi = PlatformInfo.PlatformInfo()
         #
         logging.info( "--------------------------------------------------" )
-        logging.info( p.getName()+" version "+p.getVersion() )
+        logging.info( lpi.getName() + " version " + lpi.getVersion() )
         logging.info( "--------------------------------------------------" )
         logging.info( "Library availability:" )
         logging.info( "- Python.......: True" )
-        logging.info( "- Numpy........: True" )
-        logging.info( "- Scipy........: "+str(PlatformInfo.has_scipy) )
-        logging.info( "- Matplotlib...: "+str(PlatformInfo.has_matplotlib) )
-        logging.info( "- Gnuplot......: "+str(PlatformInfo.has_scipy) )
-        logging.info( "- Sphinx.......: "+str(PlatformInfo.has_sphinx) )
-        logging.info( "- Nlopt........: "+str(PlatformInfo.has_nlopt) )
+        logging.info( "- Numpy........: " + str(lpi.has_numpy) )
+        logging.info( "- Scipy........: " + str(lpi.has_scipy) )
+        logging.info( "- Matplotlib...: " + str(lpi.has_matplotlib) )
+        logging.info( "- Gnuplot......: " + str(lpi.has_gnuplot) )
+        logging.info( "- Sphinx.......: " + str(lpi.has_sphinx) )
+        logging.info( "- Nlopt........: " + str(lpi.has_nlopt) )
         logging.info( "Library versions:" )
-        logging.info( "- Python.......: "+p.getPythonVersion() )
-        logging.info( "- Numpy........: "+p.getNumpyVersion() )
-        logging.info( "- Scipy........: "+p.getScipyVersion() )
+        logging.info( "- Python.......: " + lpi.getPythonVersion() )
+        logging.info( "- Numpy........: " + lpi.getNumpyVersion() )
+        logging.info( "- Scipy........: " + lpi.getScipyVersion() )
+        logging.info( "- Matplotlib...: " + lpi.getMatplotlibVersion() )
+        logging.info( "- Gnuplot......: " + lpi.getGnuplotVersion() )
+        logging.info( "- Sphinx.......: " + lpi.getSphinxVersion() )
+        logging.info( "- Nlopt........: " + lpi.getNloptVersion() )
         logging.info( "" )
 
     def setLogfile(self, filename=LOGFILE, filemode="w", level=logging.NOTSET):
@@ -124,7 +145,7 @@ class ExtendedLogging(object):
         Permet de disposer des messages dans un fichier EN PLUS de la console.
         """
         if self.__logfile is not None:
-            # Supprime le précédent mode de stockage fichier s'il exsitait
+            # Supprime le précédent mode de stockage fichier s'il existait
             logging.getLogger().removeHandler(self.__logfile)
         self.__logfile = logging.FileHandler(filename, filemode)
         self.__logfile.setLevel(level)
@@ -150,14 +171,14 @@ class ExtendedLogging(object):
 def logtimer(f):
     @functools.wraps(f)
     def wrapper(*args, **kwargs):
-        start  = time.clock() # time.time()
+        start  = time.clock()  # time.time()
         result = f(*args, **kwargs)
-        end    = time.clock() # time.time()
+        end    = time.clock()  # time.time()
         msg    = 'TIMER Durée elapsed de la fonction utilisateur "{}": {:.3f}s'
-        logging.debug(msg.format(f.__name__, end-start))
+        logging.debug(msg.format(f.__name__, end - start))
         return result
     return wrapper
 
 # ==============================================================================
 if __name__ == "__main__":
-    print('\n AUTODIAGNOSTIC\n')
+    print("\n AUTODIAGNOSTIC\n")