Back to index

salome-kernel  6.5.0
SALOME_Logger_Server_main.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00019 //
00020 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 //
00022 
00023 //  SALOME Logger : CORBA server managing trace output
00024 //  File   : SALOME_Logger_Server.cxx
00025 //  Author : Vasily Rusyaev
00026 //  Module : SALOME
00027 //
00028 #include <iostream>
00029 #include "SALOME_Logger_Server.hxx"
00030 #include <SALOMEconfig.h>
00031 #include <sys/types.h>
00032 #include <stdlib.h>
00033 
00034 #ifndef WIN32
00035 # include <unistd.h>
00036 #else
00037 # include "utilities.h" // for compilation type "timespec"
00038 #endif
00039 
00040 int main(int argc, char **argv)
00041 {
00042   if (argc > 2)
00043     {
00044       std::cout << "usage: SALOME_Logger_Server [output_file]" << std::endl;
00045       exit(1);
00046     }
00047   try
00048     {
00049       //Initialize the ORB
00050       const long TIMESleep = 250000000;
00051       const int NumberOfTries = 40;
00052       int i;
00053       timespec ts_req = {0, TIMESleep};
00054       timespec ts_rem = {0, 0};
00055       CosNaming::NamingContext_var inc;
00056       SALOME_Logger::Logger_var myLoggerRef;
00057       CORBA::Object_var theObj;
00058       Logger* myLogger;
00059       CORBA::Object_var obj;
00060       PortableServer::POA_var poa;
00061       PortableServer::POAManager_var pman;   
00062 
00063       CORBA::ORB_var orb = CORBA::ORB_init(argc, argv) ;
00064 
00065       for (i = 1; i <= NumberOfTries; i++)
00066           {
00067 #ifndef WIN32
00068                   if (i != 1) nanosleep(&ts_req, &ts_rem);
00069 #else
00070                   if (i != 1) Sleep(TIMESleep / 1000000);
00071 #endif
00072                   try 
00073                   {
00074                           obj = orb->resolve_initial_references("RootPOA") ;
00075                           if(!CORBA::is_nil(obj))
00076                                   poa = PortableServer::POA::_narrow(obj) ;
00077                           pman = poa->the_POAManager();
00078                           // NB. You can activate the POA before or after
00079                           // activating objects in that POA.
00080                           
00081                           // This activates the object in the root POA (by default), and
00082                           // returns a reference to it.
00083                           //NB. You can't use SALOME_NamingService class because it uses MESSAGE macro
00084                           //Otherwise, you will get segmentation fault.   
00085                           //Get initial naming context
00086                           if(!CORBA::is_nil(orb)) 
00087                                   theObj = orb->resolve_initial_references("NameService");
00088                           //Narrow to NamingContext
00089                           if (!CORBA::is_nil(theObj))
00090                                   inc = CosNaming::NamingContext::_narrow(theObj);
00091                   } catch(CORBA::COMM_FAILURE&) {
00092                           //cout<<"Logger Server: CORBA::COMM_FAILURE: Unable to contact the Naming Service"<<endl;
00093                   } catch(...) {
00094                           //cout<<"Logger Server: Unknown exception dealed with Naming Service" <<endl;
00095                   }
00096                   
00097                   if (!CORBA::is_nil(inc)) {
00098                           //      cout<<"Logger Server: Naming Service was found"<<endl; 
00099                           break;
00100                   }
00101       }
00102       if (argc == 1)
00103                   myLogger = new Logger();
00104       else
00105                   myLogger = new Logger(argv[1]);
00106 
00107       myLogger->SetOrb(orb);
00108       myLoggerRef = myLogger->_this();
00109       CosNaming::Name name;
00110       name.length(1);
00111       name[0].id = CORBA::string_dup("Logger");
00112       inc->bind(name,myLoggerRef);
00113       myLogger->_remove_ref();
00114       pman->activate();   
00115       orb->run() ;
00116       orb->destroy() ;
00117     }  
00118   catch(CORBA::COMM_FAILURE&)
00119         {
00120       std::cerr << "Caught system exception COMM_FAILURE -- unable to contact the "
00121            << "object." << std::endl;
00122     }
00123   catch(CORBA::SystemException&) 
00124     {
00125       std::cerr << "Caught CORBA::SystemException." << std::endl;
00126     }
00127   catch(CORBA::Exception&) 
00128     {
00129       std::cerr << "Caught CORBA::Exception." << std::endl;
00130     }
00131   catch(omniORB::fatalException& fe) 
00132     {
00133       std::cerr << "Caught omniORB::fatalException:" << std::endl;
00134       std::cerr << "  file: " << fe.file() << std::endl;
00135       std::cerr << "  line: " << fe.line() << std::endl;
00136       std::cerr << "  mesg: " << fe.errmsg() << std::endl;
00137     }
00138   catch(...) 
00139     {
00140       std::cerr << "Caught unknown exception." << std::endl;
00141     }
00142   return 0;
00143 }