1 // Copyright (C) 2007-2015 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 // File : FileTraceCollector.cxx
24 // 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 pthread_create(&traceThread, NULL,
68 FileTraceCollector::run, &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 //DEVTRACE("init run");
92 _threadId = new pthread_t;
93 *_threadId = pthread_self();
94 sem_post(&_sem); // unlock instance
96 LocalTraceBufferPool* myTraceBuffer = LocalTraceBufferPool::instance();
97 LocalTrace_TraceInfo myTrace;
99 // --- opens a file with append mode
100 // so, several processes can share the same file
102 std::ofstream traceFile;
103 const char *theFileName = _fileName.c_str();
104 DEVTRACE("try to open trace file "<< theFileName);
105 traceFile.open(theFileName, std::ios::out | std::ios::app);
108 std::cerr << "impossible to open trace file "<< theFileName << std::endl;
112 // --- Loop until there is no more buffer to print,
113 // and no ask for end from destructor.
114 DEVTRACE("Begin loop");
115 while ((!_threadToClose) || myTraceBuffer->toCollect() )
119 DEVTRACE("FileTraceCollector _threadToClose");
123 myTraceBuffer->retrieve(myTrace);
124 if (myTrace.traceType == ABORT_MESS)
127 traceFile << "INTERRUPTION from thread " << myTrace.threadId
128 << " : " << myTrace.trace;
130 traceFile << "INTERRUPTION from thread "
131 << (void*)(&myTrace.threadId)
132 << " : " << myTrace.trace;
135 std::cout << std::flush ;
137 std::cerr << "INTERRUPTION from thread " << myTrace.threadId
138 << " : " << myTrace.trace;
140 std::cerr << "INTERRUPTION from thread " << (void*)(&myTrace.threadId)
141 << " : " << myTrace.trace;
143 std::cerr << std::flush ;
149 traceFile << "th. " << myTrace.threadId
150 << " " << myTrace.trace;
152 traceFile << "th. " << (void*)(&myTrace.threadId)
153 << " " << myTrace.trace;
157 DEVTRACE("traceFile.close()");
159 DEVTRACE("traceFile.close()_end");
163 // ============================================================================
165 * Destructor: wait until printing thread ends (FileTraceCollector::run)
167 // ============================================================================
169 FileTraceCollector:: ~FileTraceCollector()
172 ret = pthread_mutex_lock(&_singletonMutex); // acquire lock to be alone
175 DEVTRACE("FileTraceCollector:: ~FileTraceCollector()");
176 LocalTraceBufferPool* myTraceBuffer = LocalTraceBufferPool::instance();
178 myTraceBuffer->insert(NORMAL_MESS,"end of trace\n"); // to wake up thread
181 int ret = pthread_join(*_threadId, NULL);
182 if (ret) std::cerr << "error close FileTraceCollector : "<< ret << std::endl;
183 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()