1 // Copyright (C) 2004 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 // File : LocalTraceCollector.cxx
23 // Author : Paul RASCLE (EDF)
34 #include "LocalTraceCollector.hxx"
36 // ============================================================================
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.
42 * Guarantees a unique object instance of the class (singleton thread safe)
43 * a separate thread for loop to print traces is launched.
45 // ============================================================================
47 BaseTraceCollector* LocalTraceCollector::instance()
49 if (_singleton == 0) // no need of lock when singleton already exists
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();
57 sem_init(&_sem,0,0); // to wait until run thread is initialized
58 pthread_t traceThread;
59 int re2 = pthread_create(&traceThread, NULL,
60 LocalTraceCollector::run, NULL);
62 _singleton = myInstance; // _singleton known only when init done
64 ret = pthread_mutex_unlock(&_singletonMutex); // release lock
69 // ============================================================================
71 * In a separate thread, loop to print traces.
72 * Mutex garantees intialisation on instance method is done and only one run
73 * allowed (double check ...)
74 * Loop until there is no more buffer to print,
75 * and no ask for end from destructor.
76 * Get a buffer. If type = ABORT then exit application with message.
78 // ============================================================================
80 void* LocalTraceCollector::run(void *bid)
82 _threadId = new pthread_t;
83 *_threadId = pthread_self();
84 sem_post(&_sem); // unlock instance
86 LocalTraceBufferPool* myTraceBuffer = LocalTraceBufferPool::instance();
87 LocalTrace_TraceInfo myTrace;
89 // --- Loop until there is no more buffer to print,
90 // and no ask for end from destructor.
92 while ((!_threadToClose) || myTraceBuffer->toCollect() )
96 DEVTRACE("FileTraceCollector _threadToClose");
100 int fullBuf = myTraceBuffer->retrieve(myTrace);
101 if (myTrace.traceType == ABORT_MESS)
105 cerr << "INTERRUPTION from thread " << myTrace.threadId
106 << " : " << myTrace.trace;
108 cerr << "INTERRUPTION from thread " << (void*)(&myTrace.threadId)
109 << " : " << myTrace.trace;
118 cerr << "th. " << myTrace.threadId << " " << myTrace.trace;
120 cerr << "th. " << (void*)(&myTrace.threadId)
121 << " " << myTrace.trace;
130 // ============================================================================
132 * Destructor: wait until printing thread ends (LocalTraceCollector::run)
134 // ============================================================================
136 LocalTraceCollector:: ~LocalTraceCollector()
139 ret = pthread_mutex_lock(&_singletonMutex); // acquire lock to be alone
142 DEVTRACE("LocalTraceCollector:: ~LocalTraceCollector()");
143 LocalTraceBufferPool* myTraceBuffer = LocalTraceBufferPool::instance();
145 myTraceBuffer->insert(NORMAL_MESS,"end of trace\n"); // to wake up thread
148 int ret = pthread_join(*_threadId, NULL);
149 if (ret) cerr << "error close LocalTraceCollector : "<< ret << endl;
150 else DEVTRACE("LocalTraceCollector destruction OK");
156 ret = pthread_mutex_unlock(&_singletonMutex); // release lock
159 // ============================================================================
161 * Constructor: no need of LocalTraceBufferPool object initialization here,
162 * thread safe singleton used in LocalTraceBufferPool::instance()
164 // ============================================================================
166 LocalTraceCollector::LocalTraceCollector()