1 // SALOME Logger : CORBA server managing trace output
3 // Copyright (C) 2003 CEA/DEN, EDF R&D
7 // File : SALOME_Logger_Server.cxx
8 // Author : Vasily Rusyaev
12 #include "SALOME_Logger_Server.hxx"
13 #include <SALOMEconfig.h>
14 #include <sys/types.h>
18 omni_mutex Logger::myLock;
20 /////////////////////////////////////////////////////////////////////
21 // Construction/Destruction
22 //////////////////////////////////////////////////////////////////////
26 m_putIntoFile = false;
29 Logger::Logger(const char *filename)
31 // m_outputFile.open( filename, ios::out | ios::trunc , filebuf::openprot);
32 m_outputFile.open( filename, std::ios::out | std::ios::trunc);
33 if (m_outputFile.is_open())
36 m_putIntoFile = false;
45 void Logger::putMessage(const char* message)
49 m_outputFile << message << std::flush;
57 //cout<<" Logger::ping() pid "<< getpid()<<endl;
60 int main(int argc, char **argv)
64 std::cout << "usage: SALOME_Logger_Server [output_file]" << std::endl;
70 const long TIMESleep = 250000000;
71 const int NumberOfTries = 40;
73 timespec ts_req = {0, TIMESleep};
74 timespec ts_rem = {0, 0};
75 CosNaming::NamingContext_var inc;
76 SALOME_Logger::Logger_var myLoggerRef;
77 CORBA::Object_var theObj;
79 CORBA::Object_var obj;
80 PortableServer::POA_var poa;
81 PortableServer::POAManager_var pman;
83 CORBA::ORB_var orb = CORBA::ORB_init(argc, argv) ;
85 for (i = 1; i <= NumberOfTries; i++)
87 if (i != 1) nanosleep(&ts_req, &ts_rem);
90 obj = orb->resolve_initial_references("RootPOA") ;
91 if(!CORBA::is_nil(obj))
92 poa = PortableServer::POA::_narrow(obj) ;
93 pman = poa->the_POAManager();
94 // NB. You can activate the POA before or after
95 // activating objects in that POA.
97 // This activates the object in the root POA (by default), and
98 // returns a reference to it.
99 //NB. You can't use SALOME_NamingService class because it uses MESSAGE macro
100 //Otherwise, you will get segmentation fault.
101 //Get initial naming context
102 if(!CORBA::is_nil(orb))
103 theObj = orb->resolve_initial_references("NameService");
104 //Narrow to NamingContext
105 if (!CORBA::is_nil(theObj))
106 inc = CosNaming::NamingContext::_narrow(theObj);
107 } catch(CORBA::COMM_FAILURE&) {
108 //cout<<"Logger Server: CORBA::COMM_FAILURE: Unable to contact the Naming Service"<<endl;
110 //cout<<"Logger Server: Unknown exception dealed with Naming Service" <<endl;
113 if (!CORBA::is_nil(inc)) {
114 // cout<<"Logger Server: Naming Service was found"<<endl;
119 myLogger = new Logger();
121 myLogger = new Logger(argv[1]);
123 myLoggerRef = myLogger->_this();
124 CosNaming::Name name;
126 name[0].id = CORBA::string_dup("Logger");
127 inc->bind(name,myLoggerRef);
128 myLogger->_remove_ref();
133 catch(CORBA::COMM_FAILURE& ex)
135 std::cerr << "Caught system exception COMM_FAILURE -- unable to contact the "
136 << "object." << std::endl;
138 catch(CORBA::SystemException&)
140 std::cerr << "Caught CORBA::SystemException." << std::endl;
142 catch(CORBA::Exception&)
144 std::cerr << "Caught CORBA::Exception." << std::endl;
146 catch(omniORB::fatalException& fe)
148 std::cerr << "Caught omniORB::fatalException:" << std::endl;
149 std::cerr << " file: " << fe.file() << std::endl;
150 std::cerr << " line: " << fe.line() << std::endl;
151 std::cerr << " mesg: " << fe.errmsg() << std::endl;
155 std::cerr << "Caught unknown exception." << std::endl;