1 // Copyright (C) 2007-2014 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 // SALOME Logger : CORBA server managing trace output
24 // File : SALOME_Logger.cxx
25 // Author : Vasily Rusyaev
28 #include "SALOME_Trace.hxx"
36 #include <omnithread/pthread_nt.h>
39 //////////////////////////////////////////////////////////////////////
40 // Construction/Destruction
41 //////////////////////////////////////////////////////////////////////
43 SALOME_Trace::SALOME_Trace()
48 SALOME_Trace::~SALOME_Trace()
52 SALOME_Trace& SALOME_Trace::Instance()
54 static SALOME_Trace instance;
58 int SALOME_Trace::Initialize(CORBA::ORB_ptr theOrb) {
59 //get reference on object reference from NS
60 //and initialize m_pInterfaceLogger
61 if (isInitialized && !CORBA::is_nil(m_pInterfaceLogger))
64 const long TIMESleep = 250000000;
65 const int NumberOfTries = 40;
67 timespec ts_req = {0, TIMESleep};
68 timespec ts_rem = {0, 0};
70 CosNaming::NamingContext_var inc;
71 CORBA::Object_var theObj;
72 CORBA::Object_var obj;
74 // searchin for naming service for 0.25*40=10 seconds
75 for (i = 1; i <= NumberOfTries; i++) {
77 if (i != 1) nanosleep(&ts_req,&ts_rem);
79 if (i != 1) Sleep(TIMESleep / 1000000);
82 if(CORBA::is_nil(obj))
83 obj = theOrb->resolve_initial_references("RootPOA");
84 if(CORBA::is_nil(theObj))
85 theObj = theOrb->resolve_initial_references("NameService");
86 if (!CORBA::is_nil(theObj))
87 inc = CosNaming::NamingContext::_narrow(theObj);
88 if (!CORBA::is_nil(inc)) break;
89 } catch( CORBA::SystemException& ) {
94 if (CORBA::is_nil(inc)) {
95 std::cout<<"SALOME_Trace can not find NameService"<<std::endl;
99 //cout<<"SALOME_Trace : NameService was found"<<endl;
101 const char * Env = getenv("USE_LOGGER");
102 int EnvL = (Env != NULL && strlen(Env))?1:0;
104 // the try to get Logger server if it is necessary
106 CosNaming::Name name;
108 name[0].id=CORBA::string_dup("Logger");
110 for(i = 1; i <= NumberOfTries; i++){
112 if (i != 1) nanosleep(&ts_req, &ts_rem);
114 if (i != 1) Sleep(TIMESleep / 1000000);
117 obj = inc->resolve(name);
118 if (!CORBA::is_nil(obj)) m_pInterfaceLogger = SALOME_Logger::Logger::_narrow(obj);
119 } catch(CosNaming::NamingContext::NotFound) {
122 if (!CORBA::is_nil(m_pInterfaceLogger)) {
123 //cout<<"SALOME_Trace : Logger Server was found"<<endl;
124 m_pInterfaceLogger->ping();
128 if (CORBA::is_nil(m_pInterfaceLogger)) {
129 std::cout<<"SALOME_Trace can not find Logger"<<std::endl;
137 void SALOME_Trace::putMessage(std::ostream& msg)
139 //if (!isInitialized) std::cout<<"!!! SALOME_Trace is used without initialising !!!"<<std::endl;
140 //write resulting string into Logger CORBA server
141 //concatenate string from passing parameters for transfering into Logger CORBA server
143 //std::cerr << "-+- " << msg << " ";
145 // CORBA::String_var LogMsg = CORBA::string_dup( str() );
146 //Allow automatic deletion of ostrstream content
147 const char* adt = str().c_str();
148 CORBA::String_var LogMsg = CORBA::string_dup( adt );
149 //rdbuf()->freeze(false);
150 //rdbuf()->sync(); // problem with gcc3.2
153 if (CORBA::is_nil(m_pInterfaceLogger))
156 m_pInterfaceLogger-> putMessage (LogMsg) ;