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 : FileTraceCollector.cxx
23 // Author : Paul RASCLE (EDF)
35 #include "FileTraceCollector.hxx"
37 // Class attributes initialisation, for class method FileTraceCollector::run
39 std::string FileTraceCollector::_fileName = "";
41 // ============================================================================
43 * This class is for use without CORBA, inside or outside SALOME.
44 * SALOME uses SALOMETraceCollector, to allow trace collection via CORBA.
45 * Type of trace (and corresponding class) is choosen in LocalTraceBufferPool.
47 * Guarantees a unique object instance of the class (singleton thread safe)
48 * a separate thread for loop to print traces is launched.
50 // ============================================================================
52 BaseTraceCollector* FileTraceCollector::instance(const char *fileName)
54 if (_singleton == 0) // no need of lock when singleton already exists
57 ret = pthread_mutex_lock(&_singletonMutex); // acquire lock to be alone
58 if (_singleton == 0) // another thread may have got
59 { // the lock after the first test
60 DEVTRACE("FileTraceCollector:: instance()");
61 BaseTraceCollector* myInstance = new FileTraceCollector();
63 DEVTRACE(" _fileName: " << _fileName);
65 sem_init(&_sem,0,0); // to wait until run thread is initialized
66 pthread_t traceThread;
68 int re2 = pthread_create(&traceThread, NULL,
69 FileTraceCollector::run, (void *)bid);
71 _singleton = myInstance; // _singleton known only when init done
72 DEVTRACE("FileTraceCollector:: instance()-end");
74 ret = pthread_mutex_unlock(&_singletonMutex); // release lock
79 // ============================================================================
81 * In a separate thread, loop to print traces.
82 * Mutex garantees intialisation on instance method is done and only one run
83 * allowed (double check ...)
84 * Loop until there is no more buffer to print,
85 * and no ask for end from destructor.
86 * Get a buffer. If type = ABORT then exit application with message.
88 // ============================================================================
90 void* FileTraceCollector::run(void *bid)
92 //DEVTRACE("init run");
93 _threadId = new pthread_t;
94 *_threadId = pthread_self();
95 sem_post(&_sem); // unlock instance
97 LocalTraceBufferPool* myTraceBuffer = LocalTraceBufferPool::instance();
98 LocalTrace_TraceInfo myTrace;
100 // --- opens a file with append mode
101 // so, several processes can share the same file
104 const char *theFileName = _fileName.c_str();
105 DEVTRACE("try to open trace file "<< theFileName);
106 traceFile.open(theFileName, ios::out | ios::app);
109 cerr << "impossible to open trace file "<< theFileName << endl;
113 // --- Loop until there is no more buffer to print,
114 // and no ask for end from destructor.
115 DEVTRACE("Begin loop");
116 while ((!_threadToClose) || myTraceBuffer->toCollect() )
120 DEVTRACE("FileTraceCollector _threadToClose");
124 int fullBuf = myTraceBuffer->retrieve(myTrace);
125 if (myTrace.traceType == ABORT_MESS)
128 traceFile << "INTERRUPTION from thread " << myTrace.threadId
129 << " : " << myTrace.trace;
131 traceFile << "INTERRUPTION from thread "
132 << (void*)(&myTrace.threadId)
133 << " : " << myTrace.trace;
138 cerr << "INTERRUPTION from thread " << myTrace.threadId
139 << " : " << myTrace.trace;
141 cerr << "INTERRUPTION from thread " << (void*)(&myTrace.threadId)
142 << " : " << myTrace.trace;
150 traceFile << "th. " << myTrace.threadId
151 << " " << myTrace.trace;
153 traceFile << "th. " << (void*)(&myTrace.threadId)
154 << " " << myTrace.trace;
158 DEVTRACE("traceFile.close()");
160 DEVTRACE("traceFile.close()_end");
164 // ============================================================================
166 * Destructor: wait until printing thread ends (FileTraceCollector::run)
168 // ============================================================================
170 FileTraceCollector:: ~FileTraceCollector()
173 ret = pthread_mutex_lock(&_singletonMutex); // acquire lock to be alone
176 DEVTRACE("FileTraceCollector:: ~FileTraceCollector()");
177 LocalTraceBufferPool* myTraceBuffer = LocalTraceBufferPool::instance();
179 myTraceBuffer->insert(NORMAL_MESS,"end of trace\n"); // to wake up thread
182 int ret = pthread_join(*_threadId, NULL);
183 if (ret) cerr << "error close FileTraceCollector : "<< ret << endl;
184 else DEVTRACE("FileTraceCollector destruction OK");
190 ret = pthread_mutex_unlock(&_singletonMutex); // release lock
193 // ============================================================================
195 * Constructor: no need of LocalTraceBufferPool object initialization here,
196 * thread safe singleton used in LocalTraceBufferPool::instance()
198 // ============================================================================
200 FileTraceCollector::FileTraceCollector()