Back to index

salome-kernel  6.5.0
SALOME_Trace.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00019 //
00020 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 //
00022 
00023 //  SALOME Logger : CORBA server managing trace output
00024 //  File   : SALOME_Logger.cxx
00025 //  Author : Vasily Rusyaev
00026 //  Module : SALOME
00027 //
00028 #include "SALOME_Trace.hxx"
00029 #include <memory.h>
00030 #include <string>
00031 //#include <stdio.h>
00032 #include <stdlib.h>
00033 #include <iostream>
00034 
00035 #ifdef WIN32
00036 #include <omnithread/pthread_nt.h>
00037 #endif
00038 
00040 // Construction/Destruction
00042 
00043 SALOME_Trace::SALOME_Trace()
00044 {
00045   isInitialized = 0;
00046 }
00047 
00048 SALOME_Trace::~SALOME_Trace()
00049 {
00050 }
00051 
00052 SALOME_Trace& SALOME_Trace::Instance()
00053 {
00054         static SALOME_Trace instance;
00055         return instance;
00056 }
00057 
00058 int SALOME_Trace::Initialize(CORBA::ORB_ptr theOrb) {
00059   //get reference on object reference from NS
00060   //and initialize m_pInterfaceLogger 
00061   if (isInitialized && !CORBA::is_nil(m_pInterfaceLogger))
00062     return 1;
00063 
00064   const long TIMESleep = 250000000;
00065   const int NumberOfTries = 40;
00066   int i;
00067   timespec ts_req = {0, TIMESleep};
00068   timespec ts_rem = {0, 0};
00069 
00070   CosNaming::NamingContext_var inc;
00071   CORBA::Object_var theObj;
00072   CORBA::Object_var obj;
00073 
00074   // searchin for naming service for 0.25*40=10 seconds
00075   for (i = 1; i <= NumberOfTries; i++) {
00076 #ifndef WIN32
00077     if (i != 1) nanosleep(&ts_req,&ts_rem);
00078 #else
00079         if (i != 1) Sleep(TIMESleep / 1000000);
00080 #endif
00081     try{ 
00082       if(CORBA::is_nil(obj))
00083         obj = theOrb->resolve_initial_references("RootPOA");
00084       if(CORBA::is_nil(theObj))
00085         theObj = theOrb->resolve_initial_references("NameService"); 
00086       if (!CORBA::is_nil(theObj))
00087         inc = CosNaming::NamingContext::_narrow(theObj);
00088       if (!CORBA::is_nil(inc)) break;
00089     } catch( CORBA::SystemException& ) {
00090     } catch (...) {
00091     }
00092   }
00093   
00094   if (CORBA::is_nil(inc)) {
00095     std::cout<<"SALOME_Trace can not find NameService"<<std::endl;
00096     return 0;
00097   }
00098   
00099   //cout<<"SALOME_Trace : NameService was found"<<endl;
00100   
00101   const char * Env = getenv("USE_LOGGER");
00102   int EnvL = (Env != NULL && strlen(Env))?1:0;
00103   
00104   // the try to get Logger server if it is necessary
00105   if(EnvL) {
00106     CosNaming::Name name;
00107     name.length(1);
00108     name[0].id=CORBA::string_dup("Logger");    
00109     
00110     for(i = 1; i <= NumberOfTries; i++){
00111 #ifndef WIN32
00112       if (i != 1) nanosleep(&ts_req, &ts_rem);
00113 #else
00114           if (i != 1) Sleep(TIMESleep / 1000000);
00115 #endif
00116       try {
00117         obj = inc->resolve(name);
00118         if (!CORBA::is_nil(obj)) m_pInterfaceLogger = SALOME_Logger::Logger::_narrow(obj);
00119       } catch(CosNaming::NamingContext::NotFound) {
00120       } catch(...) {
00121       }
00122       if (!CORBA::is_nil(m_pInterfaceLogger)) {
00123         //cout<<"SALOME_Trace : Logger Server was found"<<endl;
00124         m_pInterfaceLogger->ping();
00125         break;
00126       }
00127     }
00128     if (CORBA::is_nil(m_pInterfaceLogger)) {
00129       std::cout<<"SALOME_Trace can not find Logger"<<std::endl;
00130       return 0;
00131     }
00132   }
00133   isInitialized = 1;
00134   return 1;
00135 }
00136 
00137 void SALOME_Trace::putMessage(std::ostream& msg)
00138 {
00139   //if (!isInitialized) std::cout<<"!!! SALOME_Trace is used without initialising !!!"<<std::endl;
00140   //write resulting string into Logger CORBA server
00141   //concatenate string from passing parameters for transfering into Logger CORBA server
00142 
00143   //std::cerr << "-+- " << msg << " ";
00144 
00145   //   CORBA::String_var LogMsg = CORBA::string_dup( str() );
00146   //Allow automatic deletion of ostrstream content 
00147   const char* adt = str().c_str();
00148   CORBA::String_var LogMsg = CORBA::string_dup( adt );
00149   //rdbuf()->freeze(false);
00150   //rdbuf()->sync(); // problem with gcc3.2
00151   seekp(0);
00152 
00153   if (CORBA::is_nil(m_pInterfaceLogger))
00154     std::cout << LogMsg;
00155   else
00156     m_pInterfaceLogger-> putMessage (LogMsg) ;
00157 }
00158