Salome HOME
PR: mergefrom_BSEC_br1_14Mar04
[modules/yacs.git] / src / Logger / SALOME_Trace.cxx
1 //  SALOME Logger : CORBA server managing trace output
2 //
3 //  Copyright (C) 2003  CEA/DEN, EDF R&D
4 //
5 //
6 //
7 //  File   : SALOME_Logger.cxx
8 //  Author : Vasily Rusyaev
9 //  Module : SALOME
10
11 #include "SALOME_Trace.hxx"
12 #include <memory.h>
13 #include <string>
14 #include <stdlib.h>
15 #include <iostream>
16
17 #include "SALOME_Trace.hxx"
18 using namespace std;
19
20 //////////////////////////////////////////////////////////////////////
21 // Construction/Destruction
22 //////////////////////////////////////////////////////////////////////
23
24 SALOME_Trace::SALOME_Trace()
25 {
26   isInitialized = 0;
27 }
28
29 SALOME_Trace::~SALOME_Trace()
30 {
31 }
32
33 SALOME_Trace& SALOME_Trace::Instance()
34 {
35         static SALOME_Trace instance;
36         return instance;
37 }
38
39 int SALOME_Trace::Initialize(CORBA::ORB_ptr theOrb) {
40   //get reference on object reference from NS
41   //and initialize m_pInterfaceLogger 
42   if (isInitialized && !CORBA::is_nil(m_pInterfaceLogger))
43     return 1;
44
45   const long TIMESleep = 250000000;
46   const int NumberOfTries = 40;
47   int i;
48   timespec ts_req = {0, TIMESleep};
49   timespec ts_rem = {0, 0};
50
51   CosNaming::NamingContext_var inc;
52   CORBA::Object_var theObj;
53   CORBA::Object_var obj;
54
55   // searchin for naming service for 0.25*40=10 seconds
56   for (i = 1; i <= NumberOfTries; i++) {
57     if (i != 1) nanosleep(&ts_req,&ts_rem);
58     try{ 
59       if(CORBA::is_nil(obj))
60         obj = theOrb->resolve_initial_references("RootPOA");
61       if(CORBA::is_nil(theObj))
62         theObj = theOrb->resolve_initial_references("NameService"); 
63       if (!CORBA::is_nil(theObj))
64         inc = CosNaming::NamingContext::_narrow(theObj);
65       if (!CORBA::is_nil(inc)) break;
66     } catch( CORBA::COMM_FAILURE& ) {
67     } catch (...) {
68     }
69   }
70   
71   if (CORBA::is_nil(inc)) {
72     cout<<"SALOME_Trace can not find NameService"<<endl;
73     return 0;
74   }
75   
76   //cout<<"SALOME_Trace : NameService was found"<<endl;
77   
78   const char * Env = getenv("USE_LOGGER");
79   int EnvL = (Env != NULL && strlen(Env))?1:0;
80   
81   // the try to get Logger server if it is necessary
82   if(EnvL) {
83     CosNaming::Name name;
84     name.length(1);
85     name[0].id=CORBA::string_dup("Logger");    
86     
87     for(i = 1; i <= NumberOfTries; i++){
88       if (i != 1) nanosleep(&ts_req, &ts_rem);
89       try {
90         obj = inc->resolve(name);
91         if (!CORBA::is_nil(obj)) m_pInterfaceLogger = SALOME_Logger::Logger::_narrow(obj);
92       } catch(CosNaming::NamingContext::NotFound) {
93       } catch(...) {
94       }
95       if (!CORBA::is_nil(m_pInterfaceLogger)) {
96         //cout<<"SALOME_Trace : Logger Server was found"<<endl;
97         m_pInterfaceLogger->ping();
98         break;
99       }
100     }
101     if (CORBA::is_nil(m_pInterfaceLogger)) {
102       cout<<"SALOME_Trace can not find Logger"<<endl;
103       return 0;
104     }
105   }
106   isInitialized = 1;
107   return 1;
108 }
109
110 void SALOME_Trace::putMessage(ostream& msg)
111 {
112   //if (!isInitialized) cout<<"!!! SALOME_Trace is used without initialising !!!"<<endl;
113   //write resulting string into Logger CORBA server
114   //concatenate string from passing parameters for transfering into Logger CORBA server
115
116   //cerr << "-+- " << msg << " ";
117
118   //   CORBA::String_var LogMsg = CORBA::string_dup( str() );
119   //Allow automatic deletion of ostrstream content 
120   char* adt = str();
121   CORBA::String_var LogMsg = CORBA::string_dup( adt );
122   rdbuf()->freeze(false);
123   //rdbuf()->sync(); // problem with gcc3.2
124   seekp(0);
125
126   if (CORBA::is_nil(m_pInterfaceLogger))
127     cout << LogMsg;
128   else
129     m_pInterfaceLogger-> putMessage (LogMsg) ;
130 }
131