Salome HOME
Fix compilation pb.
[modules/kernel.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.salome-platform.org/ or email : webmaster.salome@opencascade.com
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 //#define _DEVDEBUG_
35 #include "FileTraceCollector.hxx"
36
37 // Class attributes initialisation, for class method FileTraceCollector::run
38
39 std::string FileTraceCollector::_fileName = "";
40
41 // ============================================================================
42 /*!
43  *  This class is for use without CORBA, inside or outside SALOME.
44  *  SALOME uses SALOMETraceCollector, to allow trace collection via CORBA.
45  *  Type of trace (and corresponding class) is choosen in LocalTraceBufferPool.
46  *
47  *  Guarantees a unique object instance of the class (singleton thread safe)
48  *  a separate thread for loop to print traces is launched.
49  */
50 // ============================================================================
51
52 BaseTraceCollector* FileTraceCollector::instance(const char *fileName)
53 {
54   if (_singleton == 0) // no need of lock when singleton already exists
55     {
56       int ret;
57       ret = pthread_mutex_lock(&_singletonMutex); // acquire lock to be alone
58       if (_singleton == 0)                     // another thread may have got
59         {                                      // the lock after the first test
60           DEVTRACE("FileTraceCollector:: instance()");
61           BaseTraceCollector* myInstance = new FileTraceCollector();
62           _fileName = fileName;
63           DEVTRACE(" _fileName: " << _fileName);
64
65           sem_init(&_sem,0,0); // to wait until run thread is initialized
66           pthread_t traceThread;
67           int bid = 0;
68           int re2 = pthread_create(&traceThread, NULL,
69                                    FileTraceCollector::run, (void *)bid);
70           sem_wait(&_sem);
71           _singleton = myInstance; // _singleton known only when init done
72           DEVTRACE("FileTraceCollector:: instance()-end");
73         }
74       ret = pthread_mutex_unlock(&_singletonMutex); // release lock
75     }
76   return _singleton;
77 }
78
79 // ============================================================================
80 /*!
81  *  In a separate thread, loop to print traces.
82  *  Mutex garantees intialisation on instance method is done and only one run
83  *  allowed (double check ...)
84  *  Loop until there is no more buffer to print,
85  *  and no ask for end from destructor.
86  *  Get a buffer. If type = ABORT then exit application with message.
87  */
88 // ============================================================================
89
90 void* FileTraceCollector::run(void *bid)
91 {
92   //DEVTRACE("init run");
93   _threadId = new pthread_t;
94   *_threadId = pthread_self();
95   sem_post(&_sem); // unlock instance
96
97   LocalTraceBufferPool* myTraceBuffer = LocalTraceBufferPool::instance();
98   LocalTrace_TraceInfo myTrace;
99
100   // --- opens a file with append mode
101   //     so, several processes can share the same file
102
103   ofstream traceFile;
104   const char *theFileName = _fileName.c_str();
105   DEVTRACE("try to open trace file "<< theFileName);
106   traceFile.open(theFileName, ios::out | ios::app);
107   if (!traceFile)
108     {
109       cerr << "impossible to open trace file "<< theFileName << endl;
110       exit (1);
111     }
112
113   // --- Loop until there is no more buffer to print,
114   //     and no ask for end from destructor.
115   DEVTRACE("Begin loop");
116   while ((!_threadToClose) || myTraceBuffer->toCollect() )
117     {
118       if (_threadToClose)
119         {
120           DEVTRACE("FileTraceCollector _threadToClose");
121           //break;
122         }
123
124       int fullBuf = myTraceBuffer->retrieve(myTrace);
125       if (myTrace.traceType == ABORT_MESS)
126         {
127 #ifndef WNT
128           traceFile << "INTERRUPTION from thread " << myTrace.threadId
129                     << " : " <<  myTrace.trace;
130 #else
131           traceFile << "INTERRUPTION from thread "
132                     << (void*)(&myTrace.threadId)
133                     << " : " <<  myTrace.trace;
134 #endif
135           traceFile.close();
136           cout << flush ;
137 #ifndef WNT
138           cerr << "INTERRUPTION from thread " << myTrace.threadId
139                << " : " <<  myTrace.trace;
140 #else
141           cerr << "INTERRUPTION from thread " << (void*)(&myTrace.threadId)
142                << " : " <<  myTrace.trace;
143 #endif
144           cerr << flush ; 
145           exit(1);     
146         }
147       else
148         {
149 #ifndef WNT
150           traceFile << "th. " << myTrace.threadId
151                     << " " << myTrace.trace;
152 #else
153           traceFile << "th. " << (void*)(&myTrace.threadId)
154                     << " " << myTrace.trace;
155 #endif
156         }
157     }
158   DEVTRACE("traceFile.close()");
159   traceFile.close();
160   DEVTRACE("traceFile.close()_end");
161   pthread_exit(NULL);
162 }
163
164 // ============================================================================
165 /*!
166  *  Destructor: wait until printing thread ends (FileTraceCollector::run)
167  */
168 // ============================================================================
169
170 FileTraceCollector:: ~FileTraceCollector()
171 {
172   int ret;
173   ret = pthread_mutex_lock(&_singletonMutex); // acquire lock to be alone
174   if (_singleton)
175     {
176       DEVTRACE("FileTraceCollector:: ~FileTraceCollector()");
177       LocalTraceBufferPool* myTraceBuffer = LocalTraceBufferPool::instance();
178       _threadToClose = 1;
179       myTraceBuffer->insert(NORMAL_MESS,"end of trace\n"); // to wake up thread
180       if (_threadId)
181         {
182           int ret = pthread_join(*_threadId, NULL);
183           if (ret) cerr << "error close FileTraceCollector : "<< ret << endl;
184           else DEVTRACE("FileTraceCollector destruction OK");
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