Salome HOME
PR: retour au tag V1_2_1_debug2 (pb dans la branche de merge V1_2c)
[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       //Get initial naming context
38       CORBA::Object_var theObj = orb->resolve_initial_references("NameService");
39       //Narrow to NamingContext
40       CosNaming::NamingContext_var inc = CosNaming::NamingContext::_narrow(theObj);
41
42       CosNaming::Name name;
43       name.length(1);
44       name[0].id = CORBA::string_dup("Logger");
45       
46       CORBA::Object_var obj;
47       obj = inc->resolve(name);
48       
49       m_pInterfaceLogger = SALOME_Logger::Logger::_narrow(obj) ;
50
51     }
52   catch (const CosNaming::NamingContext::NotFound&)
53     {
54 //       cout << "Caught exception: Naming Service can't found Logger";
55     }
56   catch (CORBA::COMM_FAILURE&)
57     {
58 //       cout << "Caught CORBA::SystemException CommFailure.";
59     }
60   catch (CORBA::SystemException&)
61     {
62 //       cout << "Caught CORBA::SystemException.";
63     }
64   catch (CORBA::Exception&)
65     {
66 //       cout << "Caught CORBA::Exception.";
67     }
68   catch (...)
69     {
70 //       cout << "Caught unknown exception.";
71     }
72   //cerr << "-----SALOME_Trace::SALOME_Trace----"<<endl;
73 }
74
75 SALOME_Trace::~SALOME_Trace()
76 {
77 }
78
79 SALOME_Trace& SALOME_Trace::Instance()
80 {
81         static SALOME_Trace instance;
82         return instance;
83 }
84
85 void SALOME_Trace::putMessage(ostream& msg)
86 {
87   //write resulting string into Logger CORBA server
88   //concatenate string from passing parameters for transfering into Logger CORBA server
89
90   //cerr << "-+- " << msg << " ";
91
92   //   CORBA::String_var LogMsg = CORBA::string_dup( str() );
93   //Allow automatic deletion of ostrstream content 
94   char* adt = str();
95   CORBA::String_var LogMsg = CORBA::string_dup( adt );
96   rdbuf()->freeze(false);
97   //rdbuf()->sync(); // problem with gcc3.2
98   seekp(0);
99
100   if (CORBA::is_nil(m_pInterfaceLogger))
101     cout << LogMsg;
102   else
103     m_pInterfaceLogger-> putMessage (LogMsg) ;
104 }
105