1 // Copyright (C) 2004 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
18 // See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
22 // File : LocalTraceCollector.cxx
23 // Author : Paul RASCLE (EDF)
34 #include "LocalTraceCollector.hxx"
36 // Class attributes initialisation, for class method LocalTraceCollector::run
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 = "";
45 // ============================================================================
47 * This class is for use without CORBA, outside SALOME.
48 * SALOME uses SALOMETraceCollector, to allow trace collection via CORBA.
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
58 // ============================================================================
60 LocalTraceCollector* LocalTraceCollector::instance(int typeTrace)
62 if (_singleton == 0) // no need of lock when singleton already exists
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();
70 _fileName = "/tmp/tracetest.log";
73 if (typeTrace) // caller sets a value different from default=0
75 else // check environment
77 char* traceKind = getenv("SALOME_trace");
78 //cout<<"SALOME_trace="<<traceKind<<endl;
81 if (strcmp(traceKind,"local")==0) _toFile=0;
86 _fileName = traceKind;
90 //cout <<"_toFile: "<<_toFile<<" _fileName: "<<_fileName<<endl;
92 pthread_t traceThread;
94 int re2 = pthread_create(&traceThread, NULL,
95 LocalTraceCollector::run, (void *)bid);
97 ret = pthread_mutex_unlock(&_singletonMutex); // release lock
102 // ============================================================================
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.
111 // ============================================================================
113 void* LocalTraceCollector::run(void *bid)
116 int ret = pthread_mutex_lock(&_singletonMutex); // acquire lock to be alone
117 if (! _threadId) // only one run
120 _threadId = pthread_self();
122 else cout << "----- Comment est-ce possible de passer la ? -------" <<endl;
123 ret = pthread_mutex_unlock(&_singletonMutex); // release lock
127 _threadId = pthread_self();
128 LocalTraceBufferPool* myTraceBuffer = LocalTraceBufferPool::instance();
129 LocalTrace_TraceInfo myTrace;
131 // if trace in file requested, opens a file with append mode
132 // so, several processes can share the same file
139 case 1 : // --- trace to file
141 const char *fileName = _fileName.c_str();
142 traceFile.open(fileName, ios::out | ios::app);
145 cerr << "impossible to open trace file "<< fileName << endl;
151 case 0 : ; // --- trace to standard output
152 default : // --- on standard output, too
156 // Loop until there is no more buffer to print,
157 // and no ask for end from destructor.
159 while ((!_threadToClose) || myTraceBuffer->toCollect() )
161 int fullBuf = myTraceBuffer->retrieve(myTrace);
162 if (myTrace.traceType == ABORT_MESS)
166 case 1 : // --- trace to file
167 traceFile << "INTERRUPTION from thread " << myTrace.threadId
168 << " : " << myTrace.trace;
171 case 0 : // --- trace to standard output
172 default : // --- on standard output, too
174 cerr << "INTERRUPTION from thread " << myTrace.threadId
175 << " : " << myTrace.trace;
185 case 1 : // --- trace to file
186 traceFile << "th. " << myTrace.threadId
187 << " " << myTrace.trace;
189 case 0 : // --- trace to standard output
190 default : // --- on standard output, too
191 cout << "th. " << myTrace.threadId << " " << myTrace.trace;
197 if (_toFile==1) traceFile.close();
202 // ============================================================================
204 * Destructor: wait until printing thread ends (LocalTraceCollector::run)
206 // ============================================================================
208 LocalTraceCollector:: ~LocalTraceCollector()
210 LocalTraceBufferPool* myTraceBuffer = LocalTraceBufferPool::instance();
212 myTraceBuffer->insert(NORMAL_MESS,"end of trace "); //needed to wake up thread
215 int ret = pthread_join(_threadId, NULL);
216 if (ret) cout << "error close LocalTraceCollector : "<< ret << endl;
217 else cout << "LocalTraceCollector destruction OK" << endl;
219 delete myTraceBuffer;
222 // ============================================================================
224 * Constructor: no need of LocalTraceBufferPool object initialization here,
225 * thread safe singleton used in LocalTraceBufferPool::instance()
227 // ============================================================================
229 LocalTraceCollector::LocalTraceCollector()