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