Salome HOME
PR: add comments to differentiate LocalTraceCollector and SALOMETraceCollector
[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       _threadId = pthread_self();
121     }
122   else cout << "----- Comment est-ce possible de passer la ? -------" <<endl;
123   ret = pthread_mutex_unlock(&_singletonMutex); // release lock
124
125   if (isOKtoRun)
126     { 
127       _threadId = pthread_self();
128       LocalTraceBufferPool* myTraceBuffer = LocalTraceBufferPool::instance();
129       LocalTrace_TraceInfo myTrace;
130
131       // if trace in file requested, opens a file with append mode
132       // so, several processes can share the same file
133
134
135       ofstream traceFile;
136
137       switch (_toFile)
138         {
139         case 1 :  // --- trace to file
140           {
141             const char *fileName = _fileName.c_str();
142             traceFile.open(fileName, ios::out | ios::app);
143             if (!traceFile)
144               {
145                 cerr << "impossible to open trace file "<< fileName << endl;
146                 exit (1);
147               }
148           }
149           break;
150
151         case 0 : ; // --- trace to standard output
152         default :  // --- on standard output, too
153           break;
154         }
155
156       // Loop until there is no more buffer to print,
157       // and no ask for end from destructor.
158
159       while ((!_threadToClose) || myTraceBuffer->toCollect() )
160         {
161           int fullBuf = myTraceBuffer->retrieve(myTrace);
162           if (myTrace.traceType == ABORT_MESS)
163             {
164               switch (_toFile)
165                 {
166                 case 1 :  // --- trace to file
167                   traceFile << "INTERRUPTION from thread " << myTrace.threadId
168                             << " : " <<  myTrace.trace;
169                   traceFile.close();
170                   // no break here !
171                 case 0 :  // --- trace to standard output
172                 default : // --- on standard output, too
173                   cout << flush ;
174                   cerr << "INTERRUPTION from thread " << myTrace.threadId
175                        << " : " <<  myTrace.trace;
176                   cerr << flush ; 
177                   exit(1);     
178                   break;
179                 }
180             }
181           else
182             {
183               switch (_toFile)
184                 {
185                 case 1 :  // --- trace to file
186                   traceFile << "th. " << myTrace.threadId
187                             << " " << myTrace.trace;
188                   break;
189                 case 0 :  // --- trace to standard output
190                 default : // --- on standard output, too
191                   cout << "th. " << myTrace.threadId << " " << myTrace.trace;
192                   break;
193                 }
194             }
195         }
196
197       if (_toFile==1) traceFile.close();
198     }
199   pthread_exit(NULL);
200 }
201
202 // ============================================================================
203 /*!
204  *  Destructor: wait until printing thread ends (LocalTraceCollector::run)
205  */
206 // ============================================================================
207
208 LocalTraceCollector:: ~LocalTraceCollector()
209 {
210   LocalTraceBufferPool* myTraceBuffer = LocalTraceBufferPool::instance();
211   _threadToClose = 1;
212   myTraceBuffer->insert(NORMAL_MESS,"end of trace "); //needed to wake up thread
213   if (_threadId)
214     {
215       int ret = pthread_join(_threadId, NULL);
216       if (ret) cout << "error close LocalTraceCollector : "<< ret << endl;
217       else cout << "LocalTraceCollector destruction OK" << endl;
218     }
219   delete myTraceBuffer;
220 }
221
222 // ============================================================================
223 /*!
224  * Constructor: no need of LocalTraceBufferPool object initialization here,
225  * thread safe singleton used in LocalTraceBufferPool::instance()
226  */
227 // ============================================================================
228
229 LocalTraceCollector::LocalTraceCollector()
230 {
231   _threadId=0;
232 }
233
234