1 // Copyright (C) 2007-2020 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 chosen 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
55 pthread_mutex_lock(&_singletonMutex); // acquire lock to be alone
56 if (_singleton == 0) // another thread may have got
57 { // the lock after the first test
58 DEVTRACE("FileTraceCollector:: instance()");
59 BaseTraceCollector* myInstance = new FileTraceCollector();
61 DEVTRACE(" _fileName: " << _fileName);
63 sem_init(&_sem,0,0); // to wait until run thread is initialized
64 pthread_t traceThread;
66 pthread_create(&traceThread, NULL,
67 FileTraceCollector::run, &bid);
69 _singleton = myInstance; // _singleton known only when init done
70 DEVTRACE("FileTraceCollector:: instance()-end");
72 pthread_mutex_unlock(&_singletonMutex); // release lock
77 // ============================================================================
79 * In a separate thread, loop to print traces.
80 * Mutex guarantees initialisation on instance method is done and only one run
81 * allowed (double check ...)
82 * Loop until there is no more buffer to print,
83 * and no ask for end from destructor.
84 * Get a buffer. If type = ABORT then exit application with message.
86 // ============================================================================
88 void* FileTraceCollector::run(void* /*bid*/)
90 //DEVTRACE("init run");
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
101 std::ofstream traceFile;
102 const char *theFileName = _fileName.c_str();
103 DEVTRACE("try to open trace file "<< theFileName);
104 traceFile.open(theFileName, std::ios::out | std::ios::app);
107 std::cerr << "impossible to open trace file "<< theFileName << std::endl;
111 // --- Loop until there is no more buffer to print,
112 // and no ask for end from destructor.
113 DEVTRACE("Begin loop");
114 while ((!_threadToClose) || myTraceBuffer->toCollect() )
118 DEVTRACE("FileTraceCollector _threadToClose");
122 myTraceBuffer->retrieve(myTrace);
123 if (myTrace.traceType == ABORT_MESS)
126 traceFile << "INTERRUPTION from thread " << myTrace.threadId
127 << " : " << myTrace.trace;
129 traceFile << "INTERRUPTION from thread "
130 << (void*)(&myTrace.threadId)
131 << " : " << myTrace.trace;
134 std::cout << std::flush ;
136 std::cerr << "INTERRUPTION from thread " << myTrace.threadId
137 << " : " << myTrace.trace;
139 std::cerr << "INTERRUPTION from thread " << (void*)(&myTrace.threadId)
140 << " : " << myTrace.trace;
142 std::cerr << std::flush ;
148 traceFile << "th. " << myTrace.threadId
149 << " " << myTrace.trace;
151 traceFile << "th. " << (void*)(&myTrace.threadId)
152 << " " << myTrace.trace;
156 DEVTRACE("traceFile.close()");
158 DEVTRACE("traceFile.close()_end");
162 // ============================================================================
164 * Destructor: wait until printing thread ends (FileTraceCollector::run)
166 // ============================================================================
168 FileTraceCollector:: ~FileTraceCollector()
170 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) { std::cerr << "error close FileTraceCollector : "<< ret << std::endl; }
181 else { DEVTRACE("FileTraceCollector destruction OK"); }
188 pthread_mutex_unlock(&_singletonMutex); // release lock
191 // ============================================================================
193 * Constructor: no need of LocalTraceBufferPool object initialization here,
194 * thread safe singleton used in LocalTraceBufferPool::instance()
196 // ============================================================================
198 FileTraceCollector::FileTraceCollector()