Salome HOME
3fb88c572c209bd0a47030547941183bf63d0a9b
[modules/kernel.git] / src / SALOMELocalTrace / LocalTraceCollector.cxx
1 // Copyright (C) 2007-2023  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 //  File   : LocalTraceCollector.cxx
24 //  Author : Paul RASCLE (EDF)
25 //  Module : KERNEL
26 //  $Header$
27 //
28
29 #include "LocalTraceCollector.hxx"
30 #include "libSALOMELog.hxx"
31
32 #include <iostream>
33 #include <sstream>
34 #include <fstream>
35 #include <cstdlib>
36
37 // ============================================================================
38 /*!
39  *  This class is for use without CORBA, inside or outside SALOME.
40  *  SALOME uses SALOMETraceCollector, to allow trace collection via CORBA.
41  *  Type of trace (and corresponding class) is chosen in LocalTraceBufferPool.
42  *
43  *  Guarantees a unique object instance of the class (singleton thread safe)
44  *  a separate thread for loop to print traces is launched.
45  */
46 // ============================================================================
47
48 BaseTraceCollector* LocalTraceCollector::instance()
49 {
50   if (_singleton == 0) // no need of lock when singleton already exists
51     {
52       pthread_mutex_lock(&_singletonMutex);    // acquire lock to be alone
53       if (_singleton == 0)                     // another thread may have got
54         {                                      // the lock after the first test
55           BaseTraceCollector* myInstance = new LocalTraceCollector();
56
57           sem_init(&_sem,0,0); // to wait until run thread is initialized
58           pthread_t traceThread;
59           pthread_create(&traceThread, NULL,
60                                    LocalTraceCollector::run, NULL);
61           sem_wait(&_sem);
62           _singleton = myInstance; // _singleton known only when init done
63         }
64       pthread_mutex_unlock(&_singletonMutex); // release lock
65     }
66   return _singleton;
67 }
68
69 // ============================================================================
70 /*!
71  *  In a separate thread, loop to print traces.
72  *  Mutex guarantees initialisation on instance method is done and only one run
73  *  allowed (double check ...)
74  *  Loop until there is no more buffer to print,
75  *  and no ask for end from destructor.
76  *  Get a buffer. If type = ABORT then exit application with message.
77  */
78 // ============================================================================
79
80 void* LocalTraceCollector::run(void* /*bid*/)
81 {
82   _threadId = new pthread_t;
83   *_threadId = pthread_self();
84   sem_post(&_sem); // unlock instance
85
86   LocalTraceBufferPool* myTraceBuffer = LocalTraceBufferPool::instance();
87   LocalTrace_TraceInfo myTrace;
88
89   // --- Loop until there is no more buffer to print,
90   //     and no ask for end from destructor.
91
92   while ((!_threadToClose) || myTraceBuffer->toCollect() )
93     {
94       if (_threadToClose)
95         {
96           DEVTRACE("FileTraceCollector _threadToClose");
97           //break;
98         }
99
100       myTraceBuffer->retrieve(myTrace);
101       if (myTrace.traceType == ABORT_MESS)
102         {
103           if( SALOME::VerbosityActivated() )
104           {
105             std::cout << std::flush ;
106 #ifndef WIN32
107             std::cerr << "INTERRUPTION from thread " << myTrace.threadId
108                 << " : " <<  myTrace.trace;
109 #else
110             std::cerr << "INTERRUPTION from thread " << (void*)(&myTrace.threadId)
111                 << " : " <<  myTrace.trace;
112 #endif
113             std::cerr << std::flush ; 
114           }
115           exit(1);     
116         }
117       else if (myTrace.traceType == NORMAL_MESS)
118         {
119           if( SALOME::VerbosityActivated() )
120           {
121             std::cout << std::flush ;
122 #ifndef WIN32
123             std::cerr << "th. " << myTrace.threadId << " " << myTrace.trace;
124 #else
125             std::cerr << "th. " << (void*)(&myTrace.threadId)
126                 << " " << myTrace.trace;
127 #endif
128             std::cerr << std::flush ;
129           } 
130         }
131       else 
132         {
133           if( SALOME::VerbosityActivated() )
134           {
135             std::cout << std::flush ;
136             std::cerr << myTrace.trace;
137             std::cerr << std::flush ;
138           }
139         }
140     }
141   pthread_exit(NULL);
142   return NULL;
143 }
144
145 // ============================================================================
146 /*!
147  *  Destructor: wait until printing thread ends (LocalTraceCollector::run)
148  */
149 // ============================================================================
150
151 LocalTraceCollector:: ~LocalTraceCollector()
152 {
153   pthread_mutex_lock(&_singletonMutex); // acquire lock to be alone
154   if (_singleton)
155     {
156       DEVTRACE("LocalTraceCollector:: ~LocalTraceCollector()");
157       LocalTraceBufferPool* myTraceBuffer = LocalTraceBufferPool::instance();
158       _threadToClose = 1;
159       myTraceBuffer->insert(NORMAL_MESS,"end of trace\n"); // to wake up thread
160       if (_threadId)
161         {
162           int ret = pthread_join(*_threadId, NULL);
163           if (ret) { std::cerr << "error close LocalTraceCollector : "<< ret << std::endl; }
164           else { DEVTRACE("LocalTraceCollector destruction OK"); }
165           delete _threadId;
166           _threadId = 0;
167           _threadToClose = 0;
168         }
169       _singleton = 0;
170     }
171   pthread_mutex_unlock(&_singletonMutex); // release lock
172 }
173
174 // ============================================================================
175 /*!
176  * Constructor: no need of LocalTraceBufferPool object initialization here,
177  * thread safe singleton used in LocalTraceBufferPool::instance()
178  */
179 // ============================================================================
180
181 LocalTraceCollector::LocalTraceCollector()
182 {
183   _threadId=0;
184   _threadToClose = 0;
185 }
186
187