Salome HOME
PR: merge from branch BR_UnitTests tag mergeto_trunk_17oct05
[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           _singleton = 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           DEVTRACE("FileTraceCollector:: instance()-end");
71         }
72       ret = pthread_mutex_unlock(&_singletonMutex); // release lock
73     }
74   return _singleton;
75 }
76
77 // ============================================================================
78 /*!
79  *  In a separate thread, loop to print traces.
80  *  Mutex garantees intialisation 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.
85  */
86 // ============================================================================
87
88 void* FileTraceCollector::run(void *bid)
89 {
90   _threadId = new pthread_t;
91   *_threadId = pthread_self();
92   sem_post(&_sem); // unlock instance
93
94   LocalTraceBufferPool* myTraceBuffer = LocalTraceBufferPool::instance();
95   LocalTrace_TraceInfo myTrace;
96
97   // --- opens a file with append mode
98   //     so, several processes can share the same file
99
100   ofstream traceFile;
101   const char *theFileName = _fileName.c_str();
102   traceFile.open(theFileName, ios::out | ios::app);
103   if (!traceFile)
104     {
105       cerr << "impossible to open trace file "<< theFileName << endl;
106       exit (1);
107     }
108
109   // --- Loop until there is no more buffer to print,
110   //     and no ask for end from destructor.
111
112   while ((!_threadToClose) || myTraceBuffer->toCollect() )
113     {
114       if (_threadToClose)
115         {
116           DEVTRACE("FileTraceCollector _threadToClose");
117           //break;
118         }
119
120       int fullBuf = myTraceBuffer->retrieve(myTrace);
121       if (myTrace.traceType == ABORT_MESS)
122         {
123 #ifndef WNT
124           traceFile << "INTERRUPTION from thread " << myTrace.threadId
125                     << " : " <<  myTrace.trace;
126 #else
127           traceFile << "INTERRUPTION from thread "
128                     << (void*)(&myTrace.threadId)
129                     << " : " <<  myTrace.trace;
130 #endif
131           traceFile.close();
132           cout << flush ;
133 #ifndef WNT
134           cerr << "INTERRUPTION from thread " << myTrace.threadId
135                << " : " <<  myTrace.trace;
136 #else
137           cerr << "INTERRUPTION from thread " << (void*)(&myTrace.threadId)
138                << " : " <<  myTrace.trace;
139 #endif
140           cerr << flush ; 
141           exit(1);     
142         }
143       else
144         {
145 #ifndef WNT
146           traceFile << "th. " << myTrace.threadId
147                     << " " << myTrace.trace;
148 #else
149           traceFile << "th. " << (void*)(&myTrace.threadId)
150                     << " " << myTrace.trace;
151 #endif
152         }
153     }
154   DEVTRACE("traceFile.close()");
155   traceFile.close();
156   DEVTRACE("traceFile.close()_end");
157   pthread_exit(NULL);
158 }
159
160 // ============================================================================
161 /*!
162  *  Destructor: wait until printing thread ends (FileTraceCollector::run)
163  */
164 // ============================================================================
165
166 FileTraceCollector:: ~FileTraceCollector()
167 {
168   int ret;
169   ret = pthread_mutex_lock(&_singletonMutex); // acquire lock to be alone
170   if (_singleton)
171     {
172       DEVTRACE("FileTraceCollector:: ~FileTraceCollector()");
173       LocalTraceBufferPool* myTraceBuffer = LocalTraceBufferPool::instance();
174       _threadToClose = 1;
175       myTraceBuffer->insert(NORMAL_MESS,"end of trace\n"); // to wake up thread
176       if (_threadId)
177         {
178           int ret = pthread_join(*_threadId, NULL);
179           if (ret) cerr << "error close FileTraceCollector : "<< ret << endl;
180           else DEVTRACE("FileTraceCollector destruction OK");
181           _threadId = 0;
182           _threadToClose = 0;
183         }
184       _singleton = 0;
185       ret = pthread_mutex_unlock(&_singletonMutex); // release lock
186     }
187 }
188
189 // ============================================================================
190 /*!
191  * Constructor: no need of LocalTraceBufferPool object initialization here,
192  * thread safe singleton used in LocalTraceBufferPool::instance()
193  */
194 // ============================================================================
195
196 FileTraceCollector::FileTraceCollector()
197 {
198   _threadId=0;
199   _threadToClose = 0;
200 }
201
202