Salome HOME
Moved some functionality to VTKViewer_Utilities.h
[modules/kernel.git] / src / Logger / SALOME_Trace.cxx
index e3185daf6120a226e3b6a7515fd74b359d65b650..1a33333eb5fe8745887482c3b3695dff4aa6c9b8 100644 (file)
@@ -8,13 +8,13 @@
 //  Author : Vasily Rusyaev
 //  Module : SALOME
 
-using namespace std;
 #include "SALOME_Trace.hxx"
 #include <memory.h>
 #include <string>
 //#include <stdio.h>
 #include <stdlib.h>
 #include <iostream>
+using namespace std;
 
 //////////////////////////////////////////////////////////////////////
 // Construction/Destruction
@@ -22,80 +22,7 @@ using namespace std;
 
 SALOME_Trace::SALOME_Trace()
 {
-  //get reference on object reference from NS
-  //and initialize m_pInterfaceLogger 
-
-  int argc = 1;
-  char* argv[1] = {"application"};
-  try
-    {
-      //NB. You can't use SALOME_NamingService class because it uses MESSAGE macro
-      //Otherwise, you will get segmentation fault.   
-
-      //Initialize the ORB
-      CORBA::ORB_var orb = CORBA::ORB_init(argc,argv) ;
-      long TIMESleep = 250000000;
-      int NumberOfTries = 40;
-      int a;
-      timespec ts_req;
-      ts_req.tv_nsec=TIMESleep;
-      ts_req.tv_sec=0;
-      timespec ts_rem;
-      ts_rem.tv_nsec=0;
-      ts_rem.tv_sec=0;
-      CosNaming::NamingContext_var inc;
-      CORBA::Object_var theObj;
-      CORBA::Object_var obj;
-      CosNaming::Name name;
-      name.length(1);
-      name[0].id = CORBA::string_dup("Logger");
-      for (int i = 1; i<=NumberOfTries; i++){
-       if (i!=1) 
-           a=nanosleep(&ts_req,&ts_rem);
-         try{ 
-           if(!CORBA::is_nil(orb)) 
-             theObj = orb->resolve_initial_references("NameService");
-           if (!CORBA::is_nil(theObj))
-             inc = CosNaming::NamingContext::_narrow(theObj);
-         }  
-         catch( CORBA::COMM_FAILURE& )
-           {
-             cout<<"SALOME_TRACE: CORBA::COMM_FAILURE: Unable to contact the Naming Service" <<endl;
-           }
-          catch(...){ cout<< "SALOME_TRACE: Unknown exception dealed with Naming Service" <<endl; }
-         
-         if(!CORBA::is_nil(inc)) {
-           obj = inc->resolve(name);
-           m_pInterfaceLogger = SALOME_Logger::Logger::_narrow(obj);
-           if (!CORBA::is_nil(m_pInterfaceLogger))
-             cout<<"SALOME_TRACE: Logger Server was found"<<endl;
-           break;
-           
-         }
-      }
-          
-    }
-      catch (const CosNaming::NamingContext::NotFound&)
-       {
-         //       cout << "Caught exception: Naming Service can't found Logger";
-       }
-      catch (CORBA::COMM_FAILURE&)
-       {
-         //       cout << "Caught CORBA::SystemException CommFailure.";
-    }
-  catch (CORBA::SystemException&)
-    {
-//       cout << "Caught CORBA::SystemException.";
-    }
-  catch (CORBA::Exception&)
-    {
-//       cout << "Caught CORBA::Exception.";
-    }
-  catch (...)
-    {
-//       cout << "Caught unknown exception.";
-    }
-  //cerr << "-----SALOME_Trace::SALOME_Trace----"<<endl;
+  isInitialized = 0;
 }
 
 SALOME_Trace::~SALOME_Trace()
@@ -108,8 +35,80 @@ SALOME_Trace& SALOME_Trace::Instance()
        return instance;
 }
 
+int SALOME_Trace::Initialize(CORBA::ORB_ptr theOrb) {
+  //get reference on object reference from NS
+  //and initialize m_pInterfaceLogger 
+  if (isInitialized && !CORBA::is_nil(m_pInterfaceLogger))
+    return 1;
+
+  const long TIMESleep = 250000000;
+  const int NumberOfTries = 40;
+  int i;
+  timespec ts_req = {0, TIMESleep};
+  timespec ts_rem = {0, 0};
+
+  CosNaming::NamingContext_var inc;
+  CORBA::Object_var theObj;
+  CORBA::Object_var obj;
+
+  // searchin for naming service for 0.25*40=10 seconds
+  for (i = 1; i <= NumberOfTries; i++) {
+    if (i != 1) nanosleep(&ts_req,&ts_rem);
+    try{ 
+      if(CORBA::is_nil(obj))
+       obj = theOrb->resolve_initial_references("RootPOA");
+      if(CORBA::is_nil(theObj))
+       theObj = theOrb->resolve_initial_references("NameService"); 
+      if (!CORBA::is_nil(theObj))
+       inc = CosNaming::NamingContext::_narrow(theObj);
+      if (!CORBA::is_nil(inc)) break;
+    } catch( CORBA::COMM_FAILURE& ) {
+    } catch (...) {
+    }
+  }
+  
+  if (CORBA::is_nil(inc)) {
+    cout<<"SALOME_Trace can not find NameService"<<endl;
+    return 0;
+  }
+  
+  //cout<<"SALOME_Trace : NameService was found"<<endl;
+  
+  const char * Env = getenv("USE_LOGGER");
+  int EnvL = (Env != NULL && strlen(Env))?1:0;
+  
+  // the try to get Logger server if it is necessary
+  if(EnvL) {
+    CosNaming::Name name;
+    name.length(1);
+    name[0].id=CORBA::string_dup("Logger");    
+    
+    for(i = 1; i <= NumberOfTries; i++){
+      if (i != 1) nanosleep(&ts_req, &ts_rem);
+      try {
+       obj = inc->resolve(name);
+       if (!CORBA::is_nil(obj)) m_pInterfaceLogger = SALOME_Logger::Logger::_narrow(obj);
+      } catch(CosNaming::NamingContext::NotFound) {
+      } catch(...) {
+      }
+      if (!CORBA::is_nil(m_pInterfaceLogger)) {
+       //cout<<"SALOME_Trace : Logger Server was found"<<endl;
+       m_pInterfaceLogger->ping();
+       break;
+      }
+    }
+    if (CORBA::is_nil(m_pInterfaceLogger)) {
+      cout<<"SALOME_Trace can not find Logger"<<endl;
+      return 0;
+    }
+  }
+  isInitialized = 1;
+  return 1;
+}
+
 void SALOME_Trace::putMessage(ostream& msg)
 {
+  //if (!isInitialized) cout<<"!!! SALOME_Trace is used without initialising !!!"<<endl;
   //write resulting string into Logger CORBA server
   //concatenate string from passing parameters for transfering into Logger CORBA server