Salome HOME
PR: merge branch CCAR_br1
[modules/yacs.git] / src / Logger / SALOME_Trace.cxx
1 using namespace std;
2 //=============================================================================
3 // File      : SALOME_Logger.cxx
4 // Created   : nov 18 10:28:17 2002
5 // Author    : Vasily Rusyaev
6 // Project   : SALOME/PRO
7 //=============================================================================
8
9 // SALOME_Trace.cxx: implementation of the SALOME_Logger class.
10 //
11 //////////////////////////////////////////////////////////////////////
12
13 #include "SALOME_Trace.hxx"
14 #include <memory.h>
15 #include <string>
16 //#include <stdio.h>
17 #include <stdlib.h>
18 #include <iostream>
19
20 //////////////////////////////////////////////////////////////////////
21 // Construction/Destruction
22 //////////////////////////////////////////////////////////////////////
23
24 SALOME_Trace::SALOME_Trace()
25 {
26   //get reference on object reference from NS
27   //and initialize m_pInterfaceLogger 
28
29   int argc = 1;
30   char* argv[1] = {"application"};
31   try
32     {
33       //NB. You can't use SALOME_NamingService class because it uses MESSAGE macro
34       //Otherwise, you will get segmentation fault.   
35
36       //Initialize the ORB
37       CORBA::ORB_var orb = CORBA::ORB_init(argc,argv) ;
38       //Get initial naming context
39       CORBA::Object_var theObj = orb->resolve_initial_references("NameService");
40       //Narrow to NamingContext
41       CosNaming::NamingContext_var inc = CosNaming::NamingContext::_narrow(theObj);
42
43       CosNaming::Name name;
44       name.length(1);
45       name[0].id = CORBA::string_dup("Logger");
46       
47       CORBA::Object_var obj;
48       obj = inc->resolve(name);
49       
50       m_pInterfaceLogger = SALOME_Logger::Logger::_narrow(obj) ;
51
52     }
53   catch (const CosNaming::NamingContext::NotFound&)
54     {
55 //       cout << "Caught exception: Naming Service can't found Logger";
56     }
57   catch (CORBA::COMM_FAILURE&)
58     {
59 //       cout << "Caught CORBA::SystemException CommFailure.";
60     }
61   catch (CORBA::SystemException&)
62     {
63 //       cout << "Caught CORBA::SystemException.";
64     }
65   catch (CORBA::Exception&)
66     {
67 //       cout << "Caught CORBA::Exception.";
68     }
69   catch (...)
70     {
71 //       cout << "Caught unknown exception.";
72     }
73   //cerr << "-----SALOME_Trace::SALOME_Trace----"<<endl;
74 }
75
76 SALOME_Trace::~SALOME_Trace()
77 {
78 }
79
80 SALOME_Trace& SALOME_Trace::Instance()
81 {
82         static SALOME_Trace instance;
83         return instance;
84 }
85
86 void SALOME_Trace::putMessage(ostream& msg)
87 {
88   //write resulting string into Logger CORBA server
89   //concatenate string from passing parameters for transfering into Logger CORBA server
90
91   //cerr << "-+- " << msg << " ";
92
93   //   CORBA::String_var LogMsg = CORBA::string_dup( str() );
94   //Allow automatic deletion of ostrstream content 
95   char* adt = str();
96   CORBA::String_var LogMsg = CORBA::string_dup( adt );
97   rdbuf()->freeze(false);
98   //rdbuf()->sync(); // problem with gcc3.2
99   seekp(0);
100
101   if (CORBA::is_nil(m_pInterfaceLogger))
102     cout << LogMsg;
103   else
104     m_pInterfaceLogger-> putMessage (LogMsg) ;
105 }
106