Salome HOME
PR: merge from BR_UnitTests (tag mergeto_trunk_18oct05)
[modules/yacs.git] / src / SALOMELocalTrace / FileTraceCollector.cxx
1 //  Copyright (C) 2004  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS 
3 // 
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. 
8 // 
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. 
13 // 
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 
17 // 
18 //  See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
19 //
20 //
21 //
22 //  File   : FileTraceCollector.cxx
23 //  Author : Paul RASCLE (EDF)
24 //  Module : KERNEL
25 //  $Header$
26
27 #include <iostream>
28 #include <sstream>
29 #include <fstream>
30 #include <cstdlib>
31
32 using namespace std;
33
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;
67           int re2 = pthread_create(&traceThread, NULL,
68                                    FileTraceCollector::run, (void *)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   _threadId = new pthread_t;
92   *_threadId = pthread_self();
93   sem_post(&_sem); // unlock instance
94
95   LocalTraceBufferPool* myTraceBuffer = LocalTraceBufferPool::instance();
96   LocalTrace_TraceInfo myTrace;
97
98   // --- opens a file with append mode
99   //     so, several processes can share the same file
100
101   ofstream traceFile;
102   const char *theFileName = _fileName.c_str();
103   traceFile.open(theFileName, ios::out | ios::app);
104   if (!traceFile)
105     {
106       cerr << "impossible to open trace file "<< theFileName << endl;
107       exit (1);
108     }
109
110   // --- Loop until there is no more buffer to print,
111   //     and no ask for end from destructor.
112
113   while ((!_threadToClose) || myTraceBuffer->toCollect() )
114     {
115       if (_threadToClose)
116         {
117           DEVTRACE("FileTraceCollector _threadToClose");
118           //break;
119         }
120
121       int fullBuf = myTraceBuffer->retrieve(myTrace);
122       if (myTrace.traceType == ABORT_MESS)
123         {
124 #ifndef WNT
125           traceFile << "INTERRUPTION from thread " << myTrace.threadId
126                     << " : " <<  myTrace.trace;
127 #else
128           traceFile << "INTERRUPTION from thread "
129                     << (void*)(&myTrace.threadId)
130                     << " : " <<  myTrace.trace;
131 #endif
132           traceFile.close();
133           cout << flush ;
134 #ifndef WNT
135           cerr << "INTERRUPTION from thread " << myTrace.threadId
136                << " : " <<  myTrace.trace;
137 #else
138           cerr << "INTERRUPTION from thread " << (void*)(&myTrace.threadId)
139                << " : " <<  myTrace.trace;
140 #endif
141           cerr << flush ; 
142           exit(1);     
143         }
144       else
145         {
146 #ifndef WNT
147           traceFile << "th. " << myTrace.threadId
148                     << " " << myTrace.trace;
149 #else
150           traceFile << "th. " << (void*)(&myTrace.threadId)
151                     << " " << myTrace.trace;
152 #endif
153         }
154     }
155   DEVTRACE("traceFile.close()");
156   traceFile.close();
157   DEVTRACE("traceFile.close()_end");
158   pthread_exit(NULL);
159 }
160
161 // ============================================================================
162 /*!
163  *  Destructor: wait until printing thread ends (FileTraceCollector::run)
164  */
165 // ============================================================================
166
167 FileTraceCollector:: ~FileTraceCollector()
168 {
169   int ret;
170   ret = pthread_mutex_lock(&_singletonMutex); // acquire lock to be alone
171   if (_singleton)
172     {
173       DEVTRACE("FileTraceCollector:: ~FileTraceCollector()");
174       LocalTraceBufferPool* myTraceBuffer = LocalTraceBufferPool::instance();
175       _threadToClose = 1;
176       myTraceBuffer->insert(NORMAL_MESS,"end of trace\n"); // to wake up thread
177       if (_threadId)
178         {
179           int ret = pthread_join(*_threadId, NULL);
180           if (ret) cerr << "error close FileTraceCollector : "<< ret << endl;
181           else DEVTRACE("FileTraceCollector destruction OK");
182           _threadId = 0;
183           _threadToClose = 0;
184         }
185       _singleton = 0;
186       ret = pthread_mutex_unlock(&_singletonMutex); // release lock
187     }
188 }
189
190 // ============================================================================
191 /*!
192  * Constructor: no need of LocalTraceBufferPool object initialization here,
193  * thread safe singleton used in LocalTraceBufferPool::instance()
194  */
195 // ============================================================================
196
197 FileTraceCollector::FileTraceCollector()
198 {
199   _threadId=0;
200   _threadToClose = 0;
201 }
202
203