Salome HOME
add method NameChanged to update title name
[modules/kernel.git] / src / Logger / SALOME_Trace.cxx
index f07b9e57812279bd769304388dfe91f9d4bd7a69..7a12648a6251b0e8da67c4e89639660a7eda6f43 100644 (file)
@@ -1,15 +1,30 @@
-using namespace std;
-//=============================================================================
-// File      : SALOME_Logger.cxx
-// Created   : nov 18 10:28:17 2002
-// Author    : Vasily Rusyaev
-// Project   : SALOME/PRO
-//=============================================================================
-
-// SALOME_Trace.cxx: implementation of the SALOME_Logger class.
+// Copyright (C) 2007-2016  CEA/DEN, EDF R&D, OPEN CASCADE
+//
+// Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
+// CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
+//
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+// Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
+//
+// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
 //
-//////////////////////////////////////////////////////////////////////
 
+//  SALOME Logger : CORBA server managing trace output
+//  File   : SALOME_Logger.cxx
+//  Author : Vasily Rusyaev
+//  Module : SALOME
+//
 #include "SALOME_Trace.hxx"
 #include <memory.h>
 #include <string>
@@ -17,60 +32,17 @@ using namespace std;
 #include <stdlib.h>
 #include <iostream>
 
+#ifdef WIN32
+#include <omnithread/pthread_nt.h>
+#endif
+
 //////////////////////////////////////////////////////////////////////
 // Construction/Destruction
 //////////////////////////////////////////////////////////////////////
 
 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) ;
-      //Get initial naming context
-      CORBA::Object_var theObj = orb->resolve_initial_references("NameService");
-      //Narrow to NamingContext
-      CosNaming::NamingContext_var inc = CosNaming::NamingContext::_narrow(theObj);
-
-      CosNaming::Name name;
-      name.length(1);
-      name[0].id = CORBA::string_dup("Logger");
-      
-      CORBA::Object_var obj;
-      obj = inc->resolve(name);
-      
-      m_pInterfaceLogger = SALOME_Logger::Logger::_narrow(obj) ;
-
-    }
-  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()
@@ -79,27 +51,107 @@ SALOME_Trace::~SALOME_Trace()
 
 SALOME_Trace& SALOME_Trace::Instance()
 {
-       static SALOME_Trace instance;
-       return instance;
+        static 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++) {
+#ifndef WIN32
+    if (i != 1) nanosleep(&ts_req,&ts_rem);
+#else
+        if (i != 1) Sleep(TIMESleep / 1000000);
+#endif
+    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::SystemException& ) {
+    } catch (...) {
+    }
+  }
+  
+  if (CORBA::is_nil(inc)) {
+    std::cout<<"SALOME_Trace can not find NameService"<<std::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++){
+#ifndef WIN32
+      if (i != 1) nanosleep(&ts_req, &ts_rem);
+#else
+          if (i != 1) Sleep(TIMESleep / 1000000);
+#endif
+      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)) {
+      std::cout<<"SALOME_Trace can not find Logger"<<std::endl;
+      return 0;
+    }
+  }
+  isInitialized = 1;
+  return 1;
 }
 
-void SALOME_Trace::putMessage(ostream& msg)
+void SALOME_Trace::putMessage(std::ostream& msg)
 {
+  //if (!isInitialized) std::cout<<"!!! SALOME_Trace is used without initialising !!!"<<std::endl;
   //write resulting string into Logger CORBA server
   //concatenate string from passing parameters for transfering into Logger CORBA server
 
-  //cerr << "-+- " << msg << " ";
+  //std::cerr << "-+- " << msg << " ";
 
   //   CORBA::String_var LogMsg = CORBA::string_dup( str() );
   //Allow automatic deletion of ostrstream content 
-  char* adt = str();
+  const char* adt = str().c_str();
   CORBA::String_var LogMsg = CORBA::string_dup( adt );
-  rdbuf()->freeze(false);
+  //rdbuf()->freeze(false);
   //rdbuf()->sync(); // problem with gcc3.2
   seekp(0);
 
   if (CORBA::is_nil(m_pInterfaceLogger))
-    cout << LogMsg;
+    std::cout << LogMsg;
   else
     m_pInterfaceLogger-> putMessage (LogMsg) ;
 }