Salome HOME
ENV: Windows porting.
[modules/kernel.git] / src / SALOMELocalTrace / LocalTraceCollector.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   : LocalTraceCollector.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 "LocalTraceCollector.hxx"
35
36 // Class attributes initialisation, for class method LocalTraceCollector::run
37
38 LocalTraceCollector* LocalTraceCollector::_singleton = 0;
39 pthread_mutex_t LocalTraceCollector::_singletonMutex;
40 int LocalTraceCollector::_threadToClose = 0;
41 pthread_t* LocalTraceCollector::_threadId = 0; // used to control single run
42 int LocalTraceCollector::_toFile = 0;
43 std::string LocalTraceCollector::_fileName = "";
44
45 // ============================================================================
46 /*!
47  *  This class is for use without CORBA, outside SALOME.
48  *  SALOME uses SALOMETraceCollector, to allow trace collection via CORBA.
49  *
50  *  guarantees a unique object instance of the class (singleton thread safe)
51  *  a separate thread for loop to print traces is launched.
52  *  \param typeTrace 0=standard out, 1=file(/tmp/tracetest.log)
53  *  If typeTrace=0, checks environment for "SALOME_trace". Test values in
54  *  the following order:
55  *  - "local"  standard out
56  *  - anything else is kept as a file name
57  */
58 // ============================================================================
59
60 LocalTraceCollector* LocalTraceCollector::instance(int typeTrace)
61 {
62   if (_singleton == 0) // no need of lock when singleton already exists
63     {
64       int ret;
65       ret = pthread_mutex_lock(&_singletonMutex); // acquire lock to be alone
66       if (_singleton == 0)                     // another thread may have got
67         {                                      // the lock after the first test
68           _singleton = new LocalTraceCollector();
69
70           _fileName = "/tmp/tracetest.log";
71           _toFile=0;
72
73           if (typeTrace)       // caller sets a value different from default=0
74             _toFile = typeTrace; 
75           else                 // check environment
76             {
77               char* traceKind = getenv("SALOME_trace");
78               //cout<<"SALOME_trace="<<traceKind<<endl;
79               if (traceKind)
80                 {
81                   if (strcmp(traceKind,"local")==0) _toFile=0;
82
83                   else
84                     {
85                       _toFile=1;
86                       _fileName = traceKind;
87                     }       
88                 }
89             }
90           //cout <<"_toFile: "<<_toFile<<" _fileName: "<<_fileName<<endl;
91
92           pthread_t traceThread;
93           int bid;
94           int re2 = pthread_create(&traceThread, NULL,
95                                    LocalTraceCollector::run, (void *)bid);
96         }
97       ret = pthread_mutex_unlock(&_singletonMutex); // release lock
98     }
99   return _singleton;
100 }
101
102 // ============================================================================
103 /*!
104  *  In a separate thread, loop to print traces.
105  *  Mutex garantees intialisation on instance method is done and only one run
106  *  allowed (double check ...)
107  *  Loop until there is no more buffer to print,
108  *  and no ask for end from destructor.
109  *  Get a buffer. If type = ABORT then exit application with message.
110  */
111 // ============================================================================
112
113 void* LocalTraceCollector::run(void *bid)
114 {
115   int isOKtoRun = 0;
116   int ret = pthread_mutex_lock(&_singletonMutex); // acquire lock to be alone
117   if (! _threadId)  // only one run
118     {
119       isOKtoRun = 1;
120       if(_threadId == 0) {
121         _threadId = new pthread_t;
122       }
123       *_threadId = pthread_self();
124     }
125   else cout << "----- Comment est-ce possible de passer la ? -------" <<endl;
126   ret = pthread_mutex_unlock(&_singletonMutex); // release lock
127
128   if (isOKtoRun)
129     { 
130       if(_threadId == 0) {
131         _threadId = new pthread_t;
132       }
133
134       *_threadId = pthread_self();
135       LocalTraceBufferPool* myTraceBuffer = LocalTraceBufferPool::instance();
136       LocalTrace_TraceInfo myTrace;
137
138       // if trace in file requested, opens a file with append mode
139       // so, several processes can share the same file
140
141
142       ofstream traceFile;
143
144       switch (_toFile)
145         {
146         case 1 :  // --- trace to file
147           {
148             const char *fileName = _fileName.c_str();
149             traceFile.open(fileName, ios::out | ios::app);
150             if (!traceFile)
151               {
152                 cerr << "impossible to open trace file "<< fileName << endl;
153                 exit (1);
154               }
155           }
156           break;
157
158         case 0 : ; // --- trace to standard output
159         default :  // --- on standard output, too
160           break;
161         }
162
163       // Loop until there is no more buffer to print,
164       // and no ask for end from destructor.
165
166       while ((!_threadToClose) || myTraceBuffer->toCollect() )
167         {
168           int fullBuf = myTraceBuffer->retrieve(myTrace);
169           if (myTrace.traceType == ABORT_MESS)
170             {
171               switch (_toFile)
172                 {
173                 case 1 :  // --- trace to file
174 #ifndef WNT
175                   traceFile << "INTERRUPTION from thread " << myTrace.threadId
176         << " : " <<  myTrace.trace;
177 #else
178                   traceFile << "INTERRUPTION from thread " << (void*)(&myTrace.threadId)
179         << " : " <<  myTrace.trace;
180 #endif
181                   traceFile.close();
182                   // no break here !
183                 case 0 :  // --- trace to standard output
184                 default : // --- on standard output, too
185                   cout << flush ;
186 #ifndef WNT
187                   cerr << "INTERRUPTION from thread " << myTrace.threadId
188                        << " : " <<  myTrace.trace;
189 #else
190                   cerr << "INTERRUPTION from thread " << (void*)(&myTrace.threadId)
191                        << " : " <<  myTrace.trace;
192 #endif
193       cerr << flush ; 
194                   exit(1);     
195                   break;
196                 }
197             }
198           else
199             {
200               switch (_toFile)
201                 {
202                 case 1 :  // --- trace to file
203 #ifndef WNT
204       traceFile << "th. " << myTrace.threadId
205                             << " " << myTrace.trace;
206 #else
207       traceFile << "th. " << (void*)(&myTrace.threadId)
208                             << " " << myTrace.trace;
209 #endif
210                   break;
211                 case 0 :  // --- trace to standard output
212                 default : // --- on standard output, too
213 #ifndef WNT
214                   cout << "th. " << myTrace.threadId << " " << myTrace.trace;
215 #else
216                   cout << "th. " << (void*)(&myTrace.threadId) << " " << myTrace.trace;
217 #endif
218                   break;
219                 }
220             }
221         }
222
223       if (_toFile==1) traceFile.close();
224     }
225   pthread_exit(NULL);
226   return NULL;
227 }
228
229 // ============================================================================
230 /*!
231  *  Destructor: wait until printing thread ends (LocalTraceCollector::run)
232  */
233 // ============================================================================
234
235 LocalTraceCollector:: ~LocalTraceCollector()
236 {
237   LocalTraceBufferPool* myTraceBuffer = LocalTraceBufferPool::instance();
238   _threadToClose = 1;
239   myTraceBuffer->insert(NORMAL_MESS,"end of trace "); //needed to wake up thread
240   if (_threadId)
241     {
242       int ret = pthread_join(*_threadId, NULL);
243       if (ret) cout << "error close LocalTraceCollector : "<< ret << endl;
244       else cout << "LocalTraceCollector destruction OK" << endl;
245     }
246   delete myTraceBuffer;
247 }
248
249 // ============================================================================
250 /*!
251  * Constructor: no need of LocalTraceBufferPool object initialization here,
252  * thread safe singleton used in LocalTraceBufferPool::instance()
253  */
254 // ============================================================================
255
256 LocalTraceCollector::LocalTraceCollector()
257 {
258   _threadId=0;
259 }
260
261