Salome HOME
PR: merge from branch BR_UT_V310a2 tag BR_UT_V310a2_20051115
[modules/kernel.git] / src / SALOMELocalTrace / LocalTraceCollector.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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
19 //
20 //
21 //
22 //  File   : LocalTraceCollector.cxx
23 //  Author : Paul RASCLE (EDF)
24 //  Module : KERNEL
25 //  $Header$
26
27 #include <iostream>
28 #include <sstream>
29 #include <fstream>
30 #include <cstdlib>
31
32 using namespace std;
33
34 #include "LocalTraceCollector.hxx"
35
36 // ============================================================================
37 /*!
38  *  This class is for use without CORBA, inside or outside SALOME.
39  *  SALOME uses SALOMETraceCollector, to allow trace collection via CORBA.
40  *  Type of trace (and corresponding class) is choosen in LocalTraceBufferPool.
41  *
42  *  Guarantees a unique object instance of the class (singleton thread safe)
43  *  a separate thread for loop to print traces is launched.
44  */
45 // ============================================================================
46
47 BaseTraceCollector* LocalTraceCollector::instance()
48 {
49   if (_singleton == 0) // no need of lock when singleton already exists
50     {
51       int ret;
52       ret = pthread_mutex_lock(&_singletonMutex); // acquire lock to be alone
53       if (_singleton == 0)                     // another thread may have got
54         {                                      // the lock after the first test
55           BaseTraceCollector* myInstance = new LocalTraceCollector();
56
57           sem_init(&_sem,0,0); // to wait until run thread is initialized
58           pthread_t traceThread;
59           int bid;
60           int re2 = pthread_create(&traceThread, NULL,
61                                    LocalTraceCollector::run, (void *)bid);
62           sem_wait(&_sem);
63           _singleton = myInstance; // _singleton known only when init done
64         }
65       ret = pthread_mutex_unlock(&_singletonMutex); // release lock
66     }
67   return _singleton;
68 }
69
70 // ============================================================================
71 /*!
72  *  In a separate thread, loop to print traces.
73  *  Mutex garantees intialisation on instance method is done and only one run
74  *  allowed (double check ...)
75  *  Loop until there is no more buffer to print,
76  *  and no ask for end from destructor.
77  *  Get a buffer. If type = ABORT then exit application with message.
78  */
79 // ============================================================================
80
81 void* LocalTraceCollector::run(void *bid)
82 {
83   _threadId = new pthread_t;
84   *_threadId = pthread_self();
85   sem_post(&_sem); // unlock instance
86
87   LocalTraceBufferPool* myTraceBuffer = LocalTraceBufferPool::instance();
88   LocalTrace_TraceInfo myTrace;
89
90   // --- Loop until there is no more buffer to print,
91   //     and no ask for end from destructor.
92
93   while ((!_threadToClose) || myTraceBuffer->toCollect() )
94     {
95       if (_threadToClose)
96         {
97           DEVTRACE("FileTraceCollector _threadToClose");
98           //break;
99         }
100
101       int fullBuf = myTraceBuffer->retrieve(myTrace);
102       if (myTrace.traceType == ABORT_MESS)
103         {
104           cout << flush ;
105 #ifndef WNT
106           cerr << "INTERRUPTION from thread " << myTrace.threadId
107                << " : " <<  myTrace.trace;
108 #else
109           cerr << "INTERRUPTION from thread " << (void*)(&myTrace.threadId)
110                << " : " <<  myTrace.trace;
111 #endif
112           cerr << flush ; 
113           exit(1);     
114         }
115       else
116         {
117           cout << flush ;
118 #ifndef WNT
119           cerr << "th. " << myTrace.threadId << " " << myTrace.trace;
120 #else
121           cerr << "th. " << (void*)(&myTrace.threadId)
122                << " " << myTrace.trace;
123 #endif
124           cerr << flush ; 
125         }
126     }
127   pthread_exit(NULL);
128   return NULL;
129 }
130
131 // ============================================================================
132 /*!
133  *  Destructor: wait until printing thread ends (LocalTraceCollector::run)
134  */
135 // ============================================================================
136
137 LocalTraceCollector:: ~LocalTraceCollector()
138 {
139   int ret;
140   ret = pthread_mutex_lock(&_singletonMutex); // acquire lock to be alone
141   if (_singleton)
142     {
143       DEVTRACE("LocalTraceCollector:: ~LocalTraceCollector()");
144       LocalTraceBufferPool* myTraceBuffer = LocalTraceBufferPool::instance();
145       _threadToClose = 1;
146       myTraceBuffer->insert(NORMAL_MESS,"end of trace\n"); // to wake up thread
147       if (_threadId)
148         {
149           int ret = pthread_join(*_threadId, NULL);
150           if (ret) cerr << "error close LocalTraceCollector : "<< ret << endl;
151           else DEVTRACE("LocalTraceCollector destruction OK");
152           _threadId = 0;
153           _threadToClose = 0;
154         }
155       _singleton = 0;
156     }
157   ret = pthread_mutex_unlock(&_singletonMutex); // release lock
158 }
159
160 // ============================================================================
161 /*!
162  * Constructor: no need of LocalTraceBufferPool object initialization here,
163  * thread safe singleton used in LocalTraceBufferPool::instance()
164  */
165 // ============================================================================
166
167 LocalTraceCollector::LocalTraceCollector()
168 {
169   _threadId=0;
170   _threadToClose = 0;
171 }
172
173