Salome HOME
Join modifications from BR_Dev_For_4_0 tag V4_1_1.
[modules/kernel.git] / src / SALOMETraceCollector / SALOMETraceCollector.cxx
1 //  Copyright (C) 2004  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS 
3 // 
4 //  This library is free software; you can redistribute it and/or 
5 //  modify it under the terms of the GNU Lesser General Public 
6 //  License as published by the Free Software Foundation; either 
7 //  version 2.1 of the License. 
8 // 
9 //  This library is distributed in the hope that it will be useful, 
10 //  but WITHOUT ANY WARRANTY; without even the implied warranty of 
11 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
12 //  Lesser General Public License for more details. 
13 // 
14 //  You should have received a copy of the GNU Lesser General Public 
15 //  License along with this library; if not, write to the Free Software 
16 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA 
17 // 
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 //
20 //
21 //
22 //  File   : LocalTraceCollector.cxx
23 //  Author : Paul RASCLE (EDF)
24 //  Module : KERNEL
25 //  $Header$
26
27 #include <SALOMEconfig.h>
28
29 #include <iostream>
30 #include <sstream>
31 #include <fstream>
32 #include <cstdlib>
33 #include <omniORB4/CORBA.h>
34
35 using namespace std;
36
37 #include "SALOMETraceCollector.hxx"
38 #include "TraceCollector_WaitForServerReadiness.hxx"
39 #include <SALOMEconfig.h>
40 #include CORBA_CLIENT_HEADER(Logger)
41
42 // Class attributes initialisation, for class method SALOMETraceCollector::run
43
44 CORBA::ORB_ptr SALOMETraceCollector::_orb = 0;
45
46 // ============================================================================
47 /*!
48  *  This class is for use with CORBA, inside SALOME.
49  *  Type of trace (and corresponding class) is choosen in LocalTraceBufferPool.
50  *
51  *  Guarantees a unique object instance of the class (singleton thread safe)
52  *  a separate thread for loop to print traces is launched.
53  */
54 // ============================================================================
55
56 BaseTraceCollector* SALOMETraceCollector::instance()
57 {
58   if (_singleton == 0) // no need of lock when singleton already exists
59     {
60       int ret;
61       ret = pthread_mutex_lock(&_singletonMutex); // acquire lock to be alone
62       if (_singleton == 0)                     // another thread may have got
63         {                                      // the lock after the first test
64           BaseTraceCollector* myInstance = new SALOMETraceCollector();
65           int argc=0;
66           char *_argv=0;
67           char ** argv = &_argv;
68           _orb = CORBA::ORB_init (argc, argv);
69
70           sem_init(&_sem,0,0); // to wait until run thread is initialized
71           pthread_t traceThread;
72           int bid = 0;
73           int re2 = pthread_create(&traceThread, NULL,
74                                    SALOMETraceCollector::run, (void *)bid);
75           sem_wait(&_sem);
76           _singleton = myInstance; // _singleton known only when init done
77         }
78       ret = pthread_mutex_unlock(&_singletonMutex); // release lock
79     }
80   return _singleton;
81 }
82
83 // ============================================================================
84 /*!
85  *  In a separate thread, loop to print traces.
86  *  Mutex garantees intialisation on instance method is done and only one run
87  *  allowed (double check ...)
88  *  Loop until there is no more buffer to print,
89  *  and no ask for end from destructor.
90  *  Get a buffer. If type = ABORT then exit application with message.
91  */
92 // ============================================================================
93
94 void* SALOMETraceCollector::run(void *bid)
95 {
96   _threadId = new pthread_t;
97   *_threadId = pthread_self();
98   sem_post(&_sem); // unlock instance
99
100   LocalTraceBufferPool* myTraceBuffer = LocalTraceBufferPool::instance();
101   LocalTrace_TraceInfo myTrace;
102
103   SALOME_Logger::Logger_var m_pInterfaceLogger;
104   CORBA::Object_var obj;
105
106   obj = TraceCollector_WaitForServerReadiness(_orb,"Logger");
107   if (!CORBA::is_nil(obj))
108     m_pInterfaceLogger = SALOME_Logger::Logger::_narrow(obj);
109   if (CORBA::is_nil(m_pInterfaceLogger))
110     {
111       cerr << "Logger server not found ! Abort" << endl;
112       cerr << flush ; 
113       exit(1);
114     } 
115   else
116     {
117       CORBA::String_var LogMsg =
118         CORBA::string_dup("\n---Init logger trace---\n");
119       m_pInterfaceLogger->putMessage(LogMsg);
120       DEVTRACE("Logger server found");
121     }
122
123   // --- Loop until there is no more buffer to print,
124   //     and no ask for end from destructor.
125
126   while ((!_threadToClose) || myTraceBuffer->toCollect() )
127     {
128       if (_threadToClose)
129         {
130           DEVTRACE("SALOMETraceCollector _threadToClose");
131           //break;
132         }
133
134       int fullBuf = myTraceBuffer->retrieve(myTrace);
135       if (!CORBA::is_nil(_orb))
136         {
137           if (myTrace.traceType == ABORT_MESS)
138             {
139               stringstream abortMessage("");
140 #ifndef WNT
141               abortMessage << "INTERRUPTION from thread "
142                            << myTrace.threadId << " : " << myTrace.trace;
143 #else
144               abortMessage << "INTERRUPTION from thread "
145                            << (void*)&myTrace.threadId 
146                            << " : " << myTrace.trace;
147 #endif
148               CORBA::String_var LogMsg =
149                 CORBA::string_dup(abortMessage.str().c_str());
150               m_pInterfaceLogger->putMessage(LogMsg);
151               exit(1);
152             }
153           else
154             {
155               stringstream aMessage("");
156 #ifndef WNT
157               aMessage << "th. " << myTrace.threadId
158 #else
159                 aMessage << "th. " << (void*)&myTrace.threadId
160 #endif
161                        << " " << myTrace.trace;
162               CORBA::String_var LogMsg =
163                 CORBA::string_dup(aMessage.str().c_str());
164               m_pInterfaceLogger->putMessage(LogMsg);
165             }
166         }
167     }
168   pthread_exit(NULL);
169   return NULL;
170 }
171
172 // ============================================================================
173 /*!
174  *  Destructor: wait until printing thread ends (SALOMETraceCollector::run)
175  */
176 // ============================================================================
177
178 SALOMETraceCollector:: ~SALOMETraceCollector()
179 {
180   int ret;
181   ret = pthread_mutex_lock(&_singletonMutex); // acquire lock to be alone
182   if (_singleton)
183     {
184       DEVTRACE("SALOMETraceCollector:: ~SALOMETraceCollector()");
185       LocalTraceBufferPool* myTraceBuffer = LocalTraceBufferPool::instance();
186       _threadToClose = 1;
187       myTraceBuffer->insert(NORMAL_MESS,"end of trace\n"); // to wake up thread
188       if (_threadId)
189         {
190           int ret = pthread_join(*_threadId, NULL);
191           if (ret) cerr << "error close SALOMETraceCollector : "<< ret << endl;
192           else DEVTRACE("SALOMETraceCollector destruction OK");
193           delete _threadId;
194           _threadId = 0;
195           _threadToClose = 0;
196         }
197       _singleton = 0;
198     }
199   ret = pthread_mutex_unlock(&_singletonMutex); // release lock
200 }
201
202 // ============================================================================
203 /*!
204  * Constructor: no need of LocalTraceBufferPool object initialization here,
205  * thread safe singleton used in LocalTraceBufferPool::instance()
206  */
207 // ============================================================================
208
209 SALOMETraceCollector::SALOMETraceCollector()
210 {
211   _threadId=0;
212   _threadToClose = 0;
213 }
214
215 // ============================================================================
216 /*!
217  * 
218  */
219 // ============================================================================
220
221 extern "C"
222 {
223  SALOMETRACECOLLECTOR_EXPORT
224   BaseTraceCollector *SingletonInstance(void)
225   {
226     BaseTraceCollector *instance = SALOMETraceCollector::instance();
227     return instance;
228   }
229 }