Salome HOME
e3185daf6120a226e3b6a7515fd74b359d65b650
[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 using namespace std;
12 #include "SALOME_Trace.hxx"
13 #include <memory.h>
14 #include <string>
15 //#include <stdio.h>
16 #include <stdlib.h>
17 #include <iostream>
18
19 //////////////////////////////////////////////////////////////////////
20 // Construction/Destruction
21 //////////////////////////////////////////////////////////////////////
22
23 SALOME_Trace::SALOME_Trace()
24 {
25   //get reference on object reference from NS
26   //and initialize m_pInterfaceLogger 
27
28   int argc = 1;
29   char* argv[1] = {"application"};
30   try
31     {
32       //NB. You can't use SALOME_NamingService class because it uses MESSAGE macro
33       //Otherwise, you will get segmentation fault.   
34
35       //Initialize the ORB
36       CORBA::ORB_var orb = CORBA::ORB_init(argc,argv) ;
37       long TIMESleep = 250000000;
38       int NumberOfTries = 40;
39       int a;
40       timespec ts_req;
41       ts_req.tv_nsec=TIMESleep;
42       ts_req.tv_sec=0;
43       timespec ts_rem;
44       ts_rem.tv_nsec=0;
45       ts_rem.tv_sec=0;
46       CosNaming::NamingContext_var inc;
47       CORBA::Object_var theObj;
48       CORBA::Object_var obj;
49       CosNaming::Name name;
50       name.length(1);
51       name[0].id = CORBA::string_dup("Logger");
52       for (int i = 1; i<=NumberOfTries; i++){
53         if (i!=1) 
54             a=nanosleep(&ts_req,&ts_rem);
55           try{ 
56             if(!CORBA::is_nil(orb)) 
57               theObj = orb->resolve_initial_references("NameService");
58             if (!CORBA::is_nil(theObj))
59               inc = CosNaming::NamingContext::_narrow(theObj);
60           }  
61           catch( CORBA::COMM_FAILURE& )
62             {
63               cout<<"SALOME_TRACE: CORBA::COMM_FAILURE: Unable to contact the Naming Service" <<endl;
64             }
65           catch(...){ cout<< "SALOME_TRACE: Unknown exception dealed with Naming Service" <<endl; }
66           
67           if(!CORBA::is_nil(inc)) {
68             obj = inc->resolve(name);
69             m_pInterfaceLogger = SALOME_Logger::Logger::_narrow(obj);
70             if (!CORBA::is_nil(m_pInterfaceLogger))
71               cout<<"SALOME_TRACE: Logger Server was found"<<endl;
72             break;
73             
74           }
75       }
76            
77     }
78       catch (const CosNaming::NamingContext::NotFound&)
79         {
80           //       cout << "Caught exception: Naming Service can't found Logger";
81         }
82       catch (CORBA::COMM_FAILURE&)
83         {
84           //       cout << "Caught CORBA::SystemException CommFailure.";
85     }
86   catch (CORBA::SystemException&)
87     {
88 //       cout << "Caught CORBA::SystemException.";
89     }
90   catch (CORBA::Exception&)
91     {
92 //       cout << "Caught CORBA::Exception.";
93     }
94   catch (...)
95     {
96 //       cout << "Caught unknown exception.";
97     }
98   //cerr << "-----SALOME_Trace::SALOME_Trace----"<<endl;
99 }
100
101 SALOME_Trace::~SALOME_Trace()
102 {
103 }
104
105 SALOME_Trace& SALOME_Trace::Instance()
106 {
107         static SALOME_Trace instance;
108         return instance;
109 }
110
111 void SALOME_Trace::putMessage(ostream& msg)
112 {
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