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