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