Salome HOME
Updated copyright comment
[modules/kernel.git] / src / Logger / SALOME_Trace.cxx
1 // Copyright (C) 2007-2024  CEA, EDF, 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.cxx
25 //  Author : Vasily Rusyaev
26 //  Module : SALOME
27 //
28 #include "SALOME_Trace.hxx"
29 #include <memory.h>
30 #include <string>
31 //#include <stdio.h>
32 #include <stdlib.h>
33 #include <iostream>
34
35 #ifdef WIN32
36 #include <omnithread/pthread_nt.h>
37 #endif
38
39 //////////////////////////////////////////////////////////////////////
40 // Construction/Destruction
41 //////////////////////////////////////////////////////////////////////
42
43 SALOME_Trace::SALOME_Trace()
44 {
45   isInitialized = 0;
46 }
47
48 SALOME_Trace::~SALOME_Trace()
49 {
50 }
51
52 SALOME_Trace& SALOME_Trace::Instance()
53 {
54         static SALOME_Trace instance;
55         return instance;
56 }
57
58 int SALOME_Trace::Initialize(CORBA::ORB_ptr theOrb) {
59   //get reference on object reference from NS
60   //and initialize m_pInterfaceLogger 
61   if (isInitialized && !CORBA::is_nil(m_pInterfaceLogger))
62     return 1;
63
64   const long TIMESleep = 250000000;
65   const int NumberOfTries = 40;
66   int i;
67   timespec ts_req = {0, TIMESleep};
68   timespec ts_rem = {0, 0};
69
70   CosNaming::NamingContext_var inc;
71   CORBA::Object_var theObj;
72   CORBA::Object_var obj;
73
74   // searching for naming service for 0.25*40=10 seconds
75   for (i = 1; i <= NumberOfTries; i++) {
76 #ifndef WIN32
77     if (i != 1) nanosleep(&ts_req,&ts_rem);
78 #else
79         if (i != 1) Sleep(TIMESleep / 1000000);
80 #endif
81     try{ 
82       if(CORBA::is_nil(obj))
83         obj = theOrb->resolve_initial_references("RootPOA");
84       if(CORBA::is_nil(theObj))
85         theObj = theOrb->resolve_initial_references("NameService"); 
86       if (!CORBA::is_nil(theObj))
87         inc = CosNaming::NamingContext::_narrow(theObj);
88       if (!CORBA::is_nil(inc)) break;
89     } catch( CORBA::SystemException& ) {
90     } catch (...) {
91     }
92   }
93   
94   if (CORBA::is_nil(inc)) {
95     std::cout<<"SALOME_Trace can not find NameService"<<std::endl;
96     return 0;
97   }
98   
99   //cout<<"SALOME_Trace : NameService was found"<<endl;
100   
101   const char * Env = getenv("USE_LOGGER");
102   int EnvL = (Env != NULL && strlen(Env))?1:0;
103   
104   // the try to get Logger server if it is necessary
105   if(EnvL) {
106     CosNaming::Name name;
107     name.length(1);
108     name[0].id=CORBA::string_dup("Logger");    
109     
110     for(i = 1; i <= NumberOfTries; i++){
111 #ifndef WIN32
112       if (i != 1) nanosleep(&ts_req, &ts_rem);
113 #else
114           if (i != 1) Sleep(TIMESleep / 1000000);
115 #endif
116       try {
117         obj = inc->resolve(name);
118         if (!CORBA::is_nil(obj)) m_pInterfaceLogger = SALOME_Logger::Logger::_narrow(obj);
119       } catch(CosNaming::NamingContext::NotFound&) {
120       } catch(...) {
121       }
122       if (!CORBA::is_nil(m_pInterfaceLogger)) {
123         //cout<<"SALOME_Trace : Logger Server was found"<<endl;
124         m_pInterfaceLogger->ping();
125         break;
126       }
127     }
128     if (CORBA::is_nil(m_pInterfaceLogger)) {
129       std::cout<<"SALOME_Trace can not find Logger"<<std::endl;
130       return 0;
131     }
132   }
133   isInitialized = 1;
134   return 1;
135 }
136
137 void SALOME_Trace::putMessage(std::ostream& /*msg*/)
138 {
139   //if (!isInitialized) std::cout<<"!!! SALOME_Trace is used without initialising !!!"<<std::endl;
140   //write resulting string into Logger CORBA server
141   //concatenate string from passing parameters for transferring into Logger CORBA server
142
143   //std::cerr << "-+- " << msg << " ";
144
145   //   CORBA::String_var LogMsg = CORBA::string_dup( str() );
146   //Allow automatic deletion of ostrstream content 
147   const char* adt = str().c_str();
148   CORBA::String_var LogMsg = CORBA::string_dup( adt );
149   //rdbuf()->freeze(false);
150   //rdbuf()->sync(); // problem with gcc3.2
151   seekp(0);
152
153   if (CORBA::is_nil(m_pInterfaceLogger))
154     std::cout << LogMsg;
155   else
156     m_pInterfaceLogger-> putMessage (LogMsg) ;
157 }
158