Salome HOME
2ca6bba5bdadb9a03a95fe9d225fb7d76345fbae
[modules/kernel.git] / src / SALOMELocalTrace / FileTraceCollector.cxx
1 // Copyright (C) 2007-2014  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
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.
10 //
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.
15 //
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
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 //  File   : FileTraceCollector.cxx
24 //  Author : Paul RASCLE (EDF)
25 //  Module : KERNEL
26 //  $Header$
27 //
28 #include <iostream>
29 #include <sstream>
30 #include <fstream>
31 #include <cstdlib>
32
33 //#define _DEVDEBUG_
34 #include "FileTraceCollector.hxx"
35
36 // Class attributes initialisation, for class method FileTraceCollector::run
37
38 std::string FileTraceCollector::_fileName = "";
39
40 // ============================================================================
41 /*!
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.
45  *
46  *  Guarantees a unique object instance of the class (singleton thread safe)
47  *  a separate thread for loop to print traces is launched.
48  */
49 // ============================================================================
50
51 BaseTraceCollector* FileTraceCollector::instance(const char *fileName)
52 {
53   if (_singleton == 0) // no need of lock when singleton already exists
54     {
55       int ret;
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();
61           _fileName = fileName;
62           DEVTRACE(" _fileName: " << _fileName);
63
64           sem_init(&_sem,0,0); // to wait until run thread is initialized
65           pthread_t traceThread;
66           int bid = 0;
67           pthread_create(&traceThread, NULL,
68                                    FileTraceCollector::run, &bid);
69           sem_wait(&_sem);
70           _singleton = myInstance; // _singleton known only when init done
71           DEVTRACE("FileTraceCollector:: instance()-end");
72         }
73       ret = pthread_mutex_unlock(&_singletonMutex); // release lock
74     }
75   return _singleton;
76 }
77
78 // ============================================================================
79 /*!
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.
86  */
87 // ============================================================================
88
89 void* FileTraceCollector::run(void *bid)
90 {
91   //DEVTRACE("init run");
92   _threadId = new pthread_t;
93   *_threadId = pthread_self();
94   sem_post(&_sem); // unlock instance
95
96   LocalTraceBufferPool* myTraceBuffer = LocalTraceBufferPool::instance();
97   LocalTrace_TraceInfo myTrace;
98
99   // --- opens a file with append mode
100   //     so, several processes can share the same file
101
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);
106   if (!traceFile)
107     {
108       std::cerr << "impossible to open trace file "<< theFileName << std::endl;
109       exit (1);
110     }
111
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() )
116     {
117       if (_threadToClose)
118         {
119           DEVTRACE("FileTraceCollector _threadToClose");
120           //break;
121         }
122
123       myTraceBuffer->retrieve(myTrace);
124       if (myTrace.traceType == ABORT_MESS)
125         {
126 #ifndef WIN32
127           traceFile << "INTERRUPTION from thread " << myTrace.threadId
128                     << " : " <<  myTrace.trace;
129 #else
130           traceFile << "INTERRUPTION from thread "
131                     << (void*)(&myTrace.threadId)
132                     << " : " <<  myTrace.trace;
133 #endif
134           traceFile.close();
135           std::cout << std::flush ;
136 #ifndef WIN32
137           std::cerr << "INTERRUPTION from thread " << myTrace.threadId
138                << " : " <<  myTrace.trace;
139 #else
140           std::cerr << "INTERRUPTION from thread " << (void*)(&myTrace.threadId)
141                << " : " <<  myTrace.trace;
142 #endif
143           std::cerr << std::flush ; 
144           exit(1);     
145         }
146       else
147         {
148 #ifndef WIN32
149           traceFile << "th. " << myTrace.threadId
150                     << " " << myTrace.trace;
151 #else
152           traceFile << "th. " << (void*)(&myTrace.threadId)
153                     << " " << myTrace.trace;
154 #endif
155         }
156     }
157   DEVTRACE("traceFile.close()");
158   traceFile.close();
159   DEVTRACE("traceFile.close()_end");
160   pthread_exit(NULL);
161 }
162
163 // ============================================================================
164 /*!
165  *  Destructor: wait until printing thread ends (FileTraceCollector::run)
166  */
167 // ============================================================================
168
169 FileTraceCollector:: ~FileTraceCollector()
170 {
171   int ret;
172   ret = pthread_mutex_lock(&_singletonMutex); // acquire lock to be alone
173   if (_singleton)
174     {
175       DEVTRACE("FileTraceCollector:: ~FileTraceCollector()");
176       LocalTraceBufferPool* myTraceBuffer = LocalTraceBufferPool::instance();
177       _threadToClose = 1;
178       myTraceBuffer->insert(NORMAL_MESS,"end of trace\n"); // to wake up thread
179       if (_threadId)
180         {
181           int ret = pthread_join(*_threadId, NULL);
182           if (ret) std::cerr << "error close FileTraceCollector : "<< ret << std::endl;
183           else DEVTRACE("FileTraceCollector destruction OK");
184           delete _threadId;
185           _threadId = 0;
186           _threadToClose = 0;
187         }
188       _singleton = 0;
189     }
190   ret = pthread_mutex_unlock(&_singletonMutex); // release lock
191 }
192
193 // ============================================================================
194 /*!
195  * Constructor: no need of LocalTraceBufferPool object initialization here,
196  * thread safe singleton used in LocalTraceBufferPool::instance()
197  */
198 // ============================================================================
199
200 FileTraceCollector::FileTraceCollector()
201 {
202   _threadId=0;
203   _threadToClose = 0;
204 }
205
206