Salome HOME
CCAR: move DSC trace function from Calcium directory to DSC_Basic directory
[modules/kernel.git] / src / Logger / SALOME_Logger_Server_main.cxx
1 //  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
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.
10 //
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.
15 //
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
19 //
20 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 //  SALOME Logger : CORBA server managing trace output
23 //  File   : SALOME_Logger_Server.cxx
24 //  Author : Vasily Rusyaev
25 //  Module : SALOME
26 //
27 #include <iostream>
28 #include "SALOME_Logger_Server.hxx"
29 #include <SALOMEconfig.h>
30 #include <sys/types.h>
31 #include <stdlib.h>
32
33 #ifndef WIN32
34 # include <unistd.h>
35 #else
36 # include "utilities.h" // for compilation type "timespec"
37 #endif
38
39 int main(int argc, char **argv)
40 {
41   if (argc > 2)
42     {
43       std::cout << "usage: SALOME_Logger_Server [output_file]" << std::endl;
44       exit(1);
45     }
46   try
47     {
48       //Initialize the ORB
49       const long TIMESleep = 250000000;
50       const int NumberOfTries = 40;
51       int i;
52       timespec ts_req = {0, TIMESleep};
53       timespec ts_rem = {0, 0};
54       CosNaming::NamingContext_var inc;
55       SALOME_Logger::Logger_var myLoggerRef;
56       CORBA::Object_var theObj;
57       Logger* myLogger;
58       CORBA::Object_var obj;
59       PortableServer::POA_var poa;
60       PortableServer::POAManager_var pman;   
61
62       CORBA::ORB_var orb = CORBA::ORB_init(argc, argv) ;
63
64       for (i = 1; i <= NumberOfTries; i++)
65           {
66 #ifndef WIN32
67                   if (i != 1) nanosleep(&ts_req, &ts_rem);
68 #else
69                   if (i != 1) Sleep(TIMESleep / 1000000);
70 #endif
71                   try 
72                   {
73                           obj = orb->resolve_initial_references("RootPOA") ;
74                           if(!CORBA::is_nil(obj))
75                                   poa = PortableServer::POA::_narrow(obj) ;
76                           pman = poa->the_POAManager();
77                           // NB. You can activate the POA before or after
78                           // activating objects in that POA.
79                           
80                           // This activates the object in the root POA (by default), and
81                           // returns a reference to it.
82                           //NB. You can't use SALOME_NamingService class because it uses MESSAGE macro
83                           //Otherwise, you will get segmentation fault.   
84                           //Get initial naming context
85                           if(!CORBA::is_nil(orb)) 
86                                   theObj = orb->resolve_initial_references("NameService");
87                           //Narrow to NamingContext
88                           if (!CORBA::is_nil(theObj))
89                                   inc = CosNaming::NamingContext::_narrow(theObj);
90                   } catch(CORBA::COMM_FAILURE&) {
91                           //cout<<"Logger Server: CORBA::COMM_FAILURE: Unable to contact the Naming Service"<<endl;
92                   } catch(...) {
93                           //cout<<"Logger Server: Unknown exception dealed with Naming Service" <<endl;
94                   }
95                   
96                   if (!CORBA::is_nil(inc)) {
97                           //      cout<<"Logger Server: Naming Service was found"<<endl; 
98                           break;
99                   }
100       }
101       if (argc == 1)
102                   myLogger = new Logger();
103       else
104                   myLogger = new Logger(argv[1]);
105
106       myLogger->SetOrb(orb);
107       myLoggerRef = myLogger->_this();
108       CosNaming::Name name;
109       name.length(1);
110       name[0].id = CORBA::string_dup("Logger");
111       inc->bind(name,myLoggerRef);
112       myLogger->_remove_ref();
113       pman->activate();   
114       orb->run() ;
115       orb->destroy() ;
116     }  
117   catch(CORBA::COMM_FAILURE& ex)
118         {
119       std::cerr << "Caught system exception COMM_FAILURE -- unable to contact the "
120            << "object." << std::endl;
121     }
122   catch(CORBA::SystemException&) 
123     {
124       std::cerr << "Caught CORBA::SystemException." << std::endl;
125     }
126   catch(CORBA::Exception&) 
127     {
128       std::cerr << "Caught CORBA::Exception." << std::endl;
129     }
130   catch(omniORB::fatalException& fe) 
131     {
132       std::cerr << "Caught omniORB::fatalException:" << std::endl;
133       std::cerr << "  file: " << fe.file() << std::endl;
134       std::cerr << "  line: " << fe.line() << std::endl;
135       std::cerr << "  mesg: " << fe.errmsg() << std::endl;
136     }
137   catch(...) 
138     {
139       std::cerr << "Caught unknown exception." << std::endl;
140     }
141   return 0;
142 }