Salome HOME
3f5cc115295d6fb991a01f92b3c957e81eab6a37
[modules/kernel.git] / src / Logger / SALOME_Logger_Server.cxx
1 using namespace std;
2 //=============================================================================
3 // File      : SALOME_Logger_Server.cxx
4 // Created   : nov 18 10:28:17 2002
5 // Author    : Vasily Rusyaev
6 // Project   : SALOME/PRO
7 //=============================================================================
8
9 // SALOME_Logger_Server.cxx: implementation of the SALOME_Logger_Server class.
10 //
11 //////////////////////////////////////////////////////////////////////
12
13 #include <iostream>
14 #include "SALOME_Logger_Server.hxx"
15
16 omni_mutex Logger::myLock;
17
18 /////////////////////////////////////////////////////////////////////
19 // Construction/Destruction
20 //////////////////////////////////////////////////////////////////////
21
22 Logger::Logger()
23 {
24   m_putIntoFile = false;
25 }
26
27 Logger::Logger(const char *filename)
28 {
29   //  m_outputFile.open( filename, ios::out | ios::trunc , filebuf::openprot);
30   m_outputFile.open( filename, ios::out | ios::trunc);
31   if (m_outputFile.is_open())
32     m_putIntoFile = true;
33   else
34     m_putIntoFile = false;
35 }
36
37 Logger::~Logger()
38 {
39   if (m_putIntoFile)
40     m_outputFile.close();
41 }
42
43 void Logger::putMessage(const char* message)
44 {
45   myLock.lock();
46   if (m_putIntoFile)
47     m_outputFile << message << flush;
48   else
49     cout << message;
50   myLock.unlock();
51 }
52
53 int main(int argc, char **argv)
54 {
55   if (argc > 2)
56     {
57       cout << "usage: SALOME_Logger_Server [output_file]" << endl;
58       exit(1);
59     }
60   try
61     {
62       //Initialize the ORB
63       CORBA::ORB_var orb = CORBA::ORB_init(argc,argv) ;
64
65       CORBA::Object_var obj = orb->resolve_initial_references("RootPOA") ;
66       PortableServer::POA_var poa = PortableServer::POA::_narrow(obj) ;
67
68       // NB. You can activate the POA before or after
69       // activating objects in that POA.
70       PortableServer::POAManager_var pman = poa->the_POAManager();
71       pman->activate();
72
73       Logger* myLogger;
74       if (argc == 1)
75         myLogger = new Logger();
76       else
77         myLogger = new Logger(argv[1]);
78
79
80       // This activates the object in the root POA (by default), and
81       // returns a reference to it.
82       SALOME_Logger::Logger_var myLoggerRef = myLogger->_this();
83
84        //NB. You can't use SALOME_NamingService class because it uses MESSAGE macro
85       //Otherwise, you will get segmentation fault.   
86
87       //Get initial naming context
88       CORBA::Object_var theObj = orb->resolve_initial_references("NameService");
89       //Narrow to NamingContext
90       CosNaming::NamingContext_var inc = CosNaming::NamingContext::_narrow(theObj);
91
92       CosNaming::Name name;
93       name.length(1);
94       name[0].id = CORBA::string_dup("Logger");
95       
96       inc->bind(name,myLoggerRef);
97
98       myLogger->_remove_ref();
99
100       orb->run() ;
101
102       orb->destroy() ;
103     }
104   catch(CORBA::COMM_FAILURE& ex) 
105     {
106       cerr << "Caught system exception COMM_FAILURE -- unable to contact the "
107         << "object." << endl;
108     }
109   catch(CORBA::SystemException&) 
110     {
111       cerr << "Caught CORBA::SystemException." << endl;
112     }
113   catch(CORBA::Exception&) 
114     {
115       cerr << "Caught CORBA::Exception." << endl;
116     }
117   catch(omniORB::fatalException& fe) 
118     {
119       cerr << "Caught omniORB::fatalException:" << endl;
120       cerr << "  file: " << fe.file() << endl;
121       cerr << "  line: " << fe.line() << endl;
122       cerr << "  mesg: " << fe.errmsg() << endl;
123     }
124   catch(...) 
125     {
126       cerr << "Caught unknown exception." << endl;
127     }
128 }