Salome HOME
[EDF29150] : prevent deletion of datastructure of perf log tree on orb_destroy called...
[modules/kernel.git] / src / Container / SALOME_Container.py
index ca969a8660902b5aaeac33ebc69f0c92b5e25f96..1fa1d41d07668b749ba7385a027aa42823bc11d0 100644 (file)
@@ -1,6 +1,5 @@
-#! /usr/bin/env python
 #  -*- coding: iso-8859-1 -*-
-# Copyright (C) 2007-2015  CEA/DEN, EDF R&D, OPEN CASCADE
+# Copyright (C) 2007-2024  CEA, EDF, OPEN CASCADE
 #
 # Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
 # CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
 
 import os
 import sys
-import string
 import traceback
-import imp
+import importlib
 from omniORB import CORBA, PortableServer
 import SALOMEDS
 import Engines, Engines__POA
 from SALOME_NamingServicePy import *
 from SALOME_ComponentPy import *
 import SALOME_PyNode
+import logging
 
 from SALOME_utilities import *
 from Utils_Identity import getShortHostName
-from launchConfigureParser import verbose
+from salome_utils import verbose
+from KernelBasis import VerbosityActivated,getSSLMode
 
 #=============================================================================
 
@@ -60,54 +60,72 @@ class SALOME_Container_i:
 
     #-------------------------------------------------------------------------
 
-    def __init__(self ,containerName, containerIORStr):
-        MESSAGE( "SALOME_Container_i::__init__" )
-        self._orb = CORBA.ORB_init(sys.argv, CORBA.ORB_ID)
+    def __init__(self ,containerName, containerIORStr, dftTimeIntervalInMs):
+        # Warning this part of code is called at the very first step of container launching
+        # so logging is not instanciate. So use verbose method to discrimine if a message should be printed or not
+        try:
+          argv = sys.argv
+        except AttributeError :
+          # for embedded python interpreter
+          # shouldn't be needed after python 3.8
+          # see https://bugs.python.org/issue32573
+          argv = ['']
+        logging.debug("Instanciation of {} PID = {}".format(containerName,os.getpid()))
+        self._orb = CORBA.ORB_init(argv, CORBA.ORB_ID)
         self._poa = self._orb.resolve_initial_references("RootPOA")
         self._containerName = containerName
-        if verbose(): print "SALOME_Container.SALOME_Container_i : _containerName ",self._containerName
-        #self._naming_service = SALOME_NamingServicePy_i(self._orb)
+        self._logFileName = None
+        self._timeIntervalInMs = dftTimeIntervalInMs
+        self._logm = None
+        self._log = None
         self._container = self._orb.string_to_object(containerIORStr)
 
+    @property
+    def logm(self):
+        logging.debug("Logm PID = {}".format(os.getpid()))
+        import salome
+        if self._logm is None:
+           salome.salome_init()
+           self._logm = salome.logm
+        return self._logm
+
     #-------------------------------------------------------------------------
 
     def import_component(self, componentName):
-        MESSAGE( "SALOME_Container_i::import_component" )
         ret=""
         try:
-            if verbose(): print "try import ",componentName
-            __import__(componentName)
-            if verbose(): print "import ",componentName," successful"
-        except ImportError,e:
+            logging.debug("try import ",componentName)
+            importlib.import_module(componentName)
+            logging.debug("import ",componentName," successful")
+        except ImportError:
             #can't import python module componentName
             #try to find it in python path
             try:
-              fp, pathname, description = imp.find_module(componentName)
-              if fp:fp.close()
+              _specs = importlib.util.find_spec(componentName)
+              _module = importlib.util.module_from_spec(_specs)
+              _specs.loader.exec_module(_module)
               #module file found in path
               ret="Component "+componentName+": Python implementation found but it can't be loaded\n"
               ret=ret+traceback.format_exc(10)
-            except ImportError,ee:
+            except ImportError as ee:
               ret="ImplementationNotFound"
-            except:
-              if verbose():print "error when calling find_module"
+            except Exception:
+              print("error when calling find_module")
               ret="ImplementationNotFound"
-        except:
+        except Exception:
             ret="Component "+componentName+": Python implementation found but it can't be loaded\n"
             ret=ret+traceback.format_exc(10)
-            if verbose():
-              traceback.print_exc()
-              print "import ",componentName," not possible"
+            traceback.print_exc()
+            print("import ",componentName," not possible")
         return ret
-        
+
     #-------------------------------------------------------------------------
 
-    def create_component_instance(self, componentName, instanceName, studyId):
-        MESSAGE( "SALOME_Container_i::create_component_instance" )
+    def create_component_instance(self, componentName, instanceName):
         comp_iors=""
         ret=""
         try:
-            component=__import__(componentName)
+            component=importlib.import_module(componentName)
             factory=getattr(component,componentName)
             comp_i=factory(self._orb,
                            self._poa,
@@ -116,15 +134,13 @@ class SALOME_Container_i:
                            instanceName,
                            componentName)
 
-            MESSAGE( "SALOME_Container_i::create_component_instance : OK")
             comp_o = comp_i._this()
             comp_iors = self._orb.object_to_string(comp_o)
-        except:
+        except Exception:
             ret=traceback.format_exc(10)
             traceback.print_exc()
-            MESSAGE( "SALOME_Container_i::create_component_instance : NOT OK")
         return comp_iors, ret
-        
+
 
     def create_pynode(self,nodeName,code):
         try:
@@ -133,19 +149,47 @@ class SALOME_Container_i:
           comp_o = self._poa.id_to_reference(id_o)
           comp_iors = self._orb.object_to_string(comp_o)
           return 0,comp_iors
-        except:
+        except Exception:
           exc_typ,exc_val,exc_fr=sys.exc_info()
           l=traceback.format_exception(exc_typ,exc_val,exc_fr)
           return 1,"".join(l)
 
     def create_pyscriptnode(self,nodeName,code):
+        logging.debug("create_pyscriptnode of {} PID = {}".format(nodeName,os.getpid()))
         try:
-          node=SALOME_PyNode.PyScriptNode_i(nodeName,code,self._poa,self)
+          logscript = None
+          if getSSLMode():
+            logscript = self._log.addScript(nodeName,code)
+          node=SALOME_PyNode.PyScriptNode_i(nodeName,code,self._poa,self, logscript)
           id_o = self._poa.activate_object(node)
           comp_o = self._poa.id_to_reference(id_o)
           comp_iors = self._orb.object_to_string(comp_o)
           return 0,comp_iors
-        except:
+        except Exception:
           exc_typ,exc_val,exc_fr=sys.exc_info()
           l=traceback.format_exception(exc_typ,exc_val,exc_fr)
+          print("".join(l)) ; sys.stdout.flush() # print error also in logs of remote container
           return 1,"".join(l)
+        
+    def positionVerbosityOfLogger(self):
+        logging.debug("positionVerbosityOfLogger PID = {}".format(os.getpid()))
+        if VerbosityActivated():
+          import salome_utils
+          salome_utils.positionVerbosityOfLoggerRegardingState()
+    
+    def monitoringtimeresms(self):
+        return self._timeIntervalInMs
+    
+    def shutdownPy(self):
+        if getSSLMode():
+           if self._log:
+              #self._log.destroy()# TODO : choose to destroy perf report or not. For the moment we keep the report
+              pass
+           
+    def setLogFileName(self, logFileName):
+        logging.debug("setLogFileName {} PID = {}".format(logFileName,os.getpid()))
+        if getSSLMode():
+          self._log = self.logm.declareContainer( self._containerName, logFileName )
+
+    def SetMonitoringtimeresms(self , value):
+        self._timeIntervalInMs = value