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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
22 // File : FileTraceCollector.cxx
23 // Author : Paul RASCLE (EDF)
34 #include "FileTraceCollector.hxx"
36 // Class attributes initialisation, for class method FileTraceCollector::run
38 std::string FileTraceCollector::_fileName = "";
40 // ============================================================================
42 * This class is for use without CORBA, inside or outside SALOME.
43 * SALOME uses SALOMETraceCollector, to allow trace collection via CORBA.
44 * Type of trace (and corresponding class) is choosen in LocalTraceBufferPool.
46 * Guarantees a unique object instance of the class (singleton thread safe)
47 * a separate thread for loop to print traces is launched.
49 // ============================================================================
51 BaseTraceCollector* FileTraceCollector::instance(const char *fileName)
53 if (_singleton == 0) // no need of lock when singleton already exists
56 ret = pthread_mutex_lock(&_singletonMutex); // acquire lock to be alone
57 if (_singleton == 0) // another thread may have got
58 { // the lock after the first test
59 DEVTRACE("FileTraceCollector:: instance()");
60 BaseTraceCollector* myInstance = new FileTraceCollector();
62 DEVTRACE(" _fileName: " << _fileName);
64 sem_init(&_sem,0,0); // to wait until run thread is initialized
65 pthread_t traceThread;
67 int re2 = pthread_create(&traceThread, NULL,
68 FileTraceCollector::run, (void *)bid);
70 _singleton = myInstance; // _singleton known only when init done
71 DEVTRACE("FileTraceCollector:: instance()-end");
73 ret = pthread_mutex_unlock(&_singletonMutex); // release lock
78 // ============================================================================
80 * In a separate thread, loop to print traces.
81 * Mutex garantees intialisation on instance method is done and only one run
82 * allowed (double check ...)
83 * Loop until there is no more buffer to print,
84 * and no ask for end from destructor.
85 * Get a buffer. If type = ABORT then exit application with message.
87 // ============================================================================
89 void* FileTraceCollector::run(void *bid)
91 _threadId = new pthread_t;
92 *_threadId = pthread_self();
93 sem_post(&_sem); // unlock instance
95 LocalTraceBufferPool* myTraceBuffer = LocalTraceBufferPool::instance();
96 LocalTrace_TraceInfo myTrace;
98 // --- opens a file with append mode
99 // so, several processes can share the same file
102 const char *theFileName = _fileName.c_str();
103 traceFile.open(theFileName, ios::out | ios::app);
106 cerr << "impossible to open trace file "<< theFileName << endl;
110 // --- Loop until there is no more buffer to print,
111 // and no ask for end from destructor.
113 while ((!_threadToClose) || myTraceBuffer->toCollect() )
117 DEVTRACE("FileTraceCollector _threadToClose");
121 int fullBuf = myTraceBuffer->retrieve(myTrace);
122 if (myTrace.traceType == ABORT_MESS)
125 traceFile << "INTERRUPTION from thread " << myTrace.threadId
126 << " : " << myTrace.trace;
128 traceFile << "INTERRUPTION from thread "
129 << (void*)(&myTrace.threadId)
130 << " : " << myTrace.trace;
135 cerr << "INTERRUPTION from thread " << myTrace.threadId
136 << " : " << myTrace.trace;
138 cerr << "INTERRUPTION from thread " << (void*)(&myTrace.threadId)
139 << " : " << myTrace.trace;
147 traceFile << "th. " << myTrace.threadId
148 << " " << myTrace.trace;
150 traceFile << "th. " << (void*)(&myTrace.threadId)
151 << " " << myTrace.trace;
155 DEVTRACE("traceFile.close()");
157 DEVTRACE("traceFile.close()_end");
161 // ============================================================================
163 * Destructor: wait until printing thread ends (FileTraceCollector::run)
165 // ============================================================================
167 FileTraceCollector:: ~FileTraceCollector()
170 ret = pthread_mutex_lock(&_singletonMutex); // acquire lock to be alone
173 DEVTRACE("FileTraceCollector:: ~FileTraceCollector()");
174 LocalTraceBufferPool* myTraceBuffer = LocalTraceBufferPool::instance();
176 myTraceBuffer->insert(NORMAL_MESS,"end of trace\n"); // to wake up thread
179 int ret = pthread_join(*_threadId, NULL);
180 if (ret) cerr << "error close FileTraceCollector : "<< ret << endl;
181 else DEVTRACE("FileTraceCollector destruction OK");
186 ret = pthread_mutex_unlock(&_singletonMutex); // release lock
190 // ============================================================================
192 * Constructor: no need of LocalTraceBufferPool object initialization here,
193 * thread safe singleton used in LocalTraceBufferPool::instance()
195 // ============================================================================
197 FileTraceCollector::FileTraceCollector()