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