1 // SALOME Logger : CORBA server managing trace output
3 // Copyright (C) 2003 CEA/DEN, EDF R&D
7 // File : SALOME_Logger.cxx
8 // Author : Vasily Rusyaev
11 #include "SALOME_Trace.hxx"
20 #include <omnithread/pthread_nt.h>
23 //////////////////////////////////////////////////////////////////////
24 // Construction/Destruction
25 //////////////////////////////////////////////////////////////////////
27 SALOME_Trace::SALOME_Trace()
32 SALOME_Trace::~SALOME_Trace()
36 SALOME_Trace& SALOME_Trace::Instance()
38 static SALOME_Trace instance;
42 int SALOME_Trace::Initialize(CORBA::ORB_ptr theOrb) {
43 //get reference on object reference from NS
44 //and initialize m_pInterfaceLogger
45 if (isInitialized && !CORBA::is_nil(m_pInterfaceLogger))
48 const long TIMESleep = 250000000;
49 const int NumberOfTries = 40;
51 timespec ts_req = {0, TIMESleep};
52 timespec ts_rem = {0, 0};
54 CosNaming::NamingContext_var inc;
55 CORBA::Object_var theObj;
56 CORBA::Object_var obj;
58 // searchin for naming service for 0.25*40=10 seconds
59 for (i = 1; i <= NumberOfTries; i++) {
61 if (i != 1) nanosleep(&ts_req,&ts_rem);
63 if (i != 1) Sleep(TIMESleep / 1000000);
66 if(CORBA::is_nil(obj))
67 obj = theOrb->resolve_initial_references("RootPOA");
68 if(CORBA::is_nil(theObj))
69 theObj = theOrb->resolve_initial_references("NameService");
70 if (!CORBA::is_nil(theObj))
71 inc = CosNaming::NamingContext::_narrow(theObj);
72 if (!CORBA::is_nil(inc)) break;
73 } catch( CORBA::SystemException& ) {
78 if (CORBA::is_nil(inc)) {
79 cout<<"SALOME_Trace can not find NameService"<<endl;
83 //cout<<"SALOME_Trace : NameService was found"<<endl;
85 const char * Env = getenv("USE_LOGGER");
86 int EnvL = (Env != NULL && strlen(Env))?1:0;
88 // the try to get Logger server if it is necessary
92 name[0].id=CORBA::string_dup("Logger");
94 for(i = 1; i <= NumberOfTries; i++){
96 if (i != 1) nanosleep(&ts_req, &ts_rem);
98 if (i != 1) Sleep(TIMESleep / 1000000);
101 obj = inc->resolve(name);
102 if (!CORBA::is_nil(obj)) m_pInterfaceLogger = SALOME_Logger::Logger::_narrow(obj);
103 } catch(CosNaming::NamingContext::NotFound) {
106 if (!CORBA::is_nil(m_pInterfaceLogger)) {
107 //cout<<"SALOME_Trace : Logger Server was found"<<endl;
108 m_pInterfaceLogger->ping();
112 if (CORBA::is_nil(m_pInterfaceLogger)) {
113 cout<<"SALOME_Trace can not find Logger"<<endl;
121 void SALOME_Trace::putMessage(ostream& msg)
123 //if (!isInitialized) cout<<"!!! SALOME_Trace is used without initialising !!!"<<endl;
124 //write resulting string into Logger CORBA server
125 //concatenate string from passing parameters for transfering into Logger CORBA server
127 //cerr << "-+- " << msg << " ";
129 // CORBA::String_var LogMsg = CORBA::string_dup( str() );
130 //Allow automatic deletion of ostrstream content
132 CORBA::String_var LogMsg = CORBA::string_dup( adt );
133 rdbuf()->freeze(false);
134 //rdbuf()->sync(); // problem with gcc3.2
137 if (CORBA::is_nil(m_pInterfaceLogger))
140 m_pInterfaceLogger-> putMessage (LogMsg) ;