Salome HOME
70f7650382c31f9e1d25fe8d89fd3bf8c9cb939d
[modules/yacs.git] / src / SALOMELogger / SALOME_LoggerClient.cxx
1 //  SALOME_LoggerClient : CORBA distributed log
2 //
3 //  Copyright (C) 2003  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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
21 //
22 //
23 //
24 //  File   : SALOME_Loggerclient.hxx
25 //  Author : Vasily Rusyaev, Paul RASCLE, EDF
26 //  Module : KERNEL
27 //  $Header$
28
29 #include <string>
30 #include <iostream>
31
32 using namespace std;
33
34 #include "SALOME_Log.hxx"
35
36 #include <SALOMEconfig.h>
37 #include CORBA_CLIENT_HEADER(Logger)
38
39 // class SALOME_LoggerClient : public SALOME_Log
40 // {
41 // protected:
42 //   SALOME_Logger::Logger_var m_pInterfaceLogger; // object reference on Logger server
43 // };
44
45 SALOME_Logger::Logger_ptr m_pInterfaceLogger; // object reference on Logger server
46
47 SALOME_Log::SALOME_Log()
48 {
49   cout << "SALOME_LoggerClient: constructor" << endl;
50   //get reference on object reference from NS
51   //and initialize m_pInterfaceLogger 
52
53   int argc = 1;
54   char* argv[1] = {"application"};
55   try
56     {
57       //NB. You can't use SALOME_NamingService class because it uses MESSAGE macro
58       //Otherwise, you will get segmentation fault.   
59
60       //Initialize the ORB
61       CORBA::ORB_var orb = CORBA::ORB_init(argc,argv);
62       long TIMESleep = 250000000;
63       int NumberOfTries = 40;
64       int a;
65       timespec ts_req;
66       ts_req.tv_nsec=TIMESleep;
67       ts_req.tv_sec=0;
68       timespec ts_rem;
69       ts_rem.tv_nsec=0;
70       ts_rem.tv_sec=0;
71       CosNaming::NamingContext_var inc;
72       CORBA::Object_var theObj;
73       CORBA::Object_var obj;
74       CosNaming::Name name;
75       name.length(1);
76       name[0].id = CORBA::string_dup("Logger");
77       for (int i = 1; i<=NumberOfTries; i++)
78         {
79           if (i!=1) 
80             a=nanosleep(&ts_req,&ts_rem);
81           try
82             { 
83               if(!CORBA::is_nil(orb)) 
84                 theObj = orb->resolve_initial_references("NameService");
85               if (!CORBA::is_nil(theObj))
86                 inc = CosNaming::NamingContext::_narrow(theObj);
87             }  
88           catch( CORBA::COMM_FAILURE& )
89             {
90               cout<<"SALOME_LoggerClient: CORBA::COMM_FAILURE: Unable to contact the Naming Service"<<endl;
91             }
92           catch(...)
93             {
94               cout<<"SALOME_LoggerClient: Unknown exception dealing with Naming Service"<<endl;
95             }
96           
97           if(!CORBA::is_nil(inc))
98             {
99               obj = inc->resolve(name);
100               m_pInterfaceLogger = SALOME_Logger::Logger::_narrow(obj);
101               if (!CORBA::is_nil(m_pInterfaceLogger))
102                 cout<<"SALOME_LoggerClient: Logger Server was found"<<endl;
103               break;
104             }
105         }          
106     }
107   catch (const CosNaming::NamingContext::NotFound&)
108     {
109       //       cout << "Caught exception: Naming Service can't found Logger";
110     }
111   catch (CORBA::COMM_FAILURE&)
112     {
113       //       cout << "Caught CORBA::SystemException CommFailure.";
114     }
115   catch (CORBA::SystemException&)
116     {
117       //       cout << "Caught CORBA::SystemException.";
118     }
119   catch (CORBA::Exception&)
120     {
121       //       cout << "Caught CORBA::Exception.";
122     }
123   catch (...)
124     {
125       //       cout << "Caught unknown exception.";
126     }
127   //cerr << "-----SALOME_Trace::SALOME_Trace----"<<endl;
128 }
129
130 SALOME_Log::~SALOME_Log()
131 {
132 }
133
134 SALOME_Log& SALOME_Log::Instance()
135 {
136   static SALOME_Log instance;
137   return instance;
138 }
139
140 void SALOME_Log::putMessage(std::ostream& msg)
141 {
142   //write resulting string into Logger CORBA server
143
144   char* adt = str();
145   CORBA::String_var LogMsg = CORBA::string_dup(adt);
146   rdbuf()->freeze(false);
147   seekp(0);
148
149   if (CORBA::is_nil(m_pInterfaceLogger))
150     cout << LogMsg << std::flush;
151   else
152     m_pInterfaceLogger-> putMessage (LogMsg) ;
153  }
154