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