]> SALOME platform Git repositories - modules/yacs.git/blob - src/SALOMELocalTrace/LocalTraceCollector.cxx
Salome HOME
Revert "Revert "bos #18467 [CEA] Remove any addition to the environment at launch...
[modules/yacs.git] / src / SALOMELocalTrace / LocalTraceCollector.cxx
1 // Copyright (C) 2007-2019  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 #include <iostream>
29 #include <sstream>
30 #include <fstream>
31 #include <cstdlib>
32
33 #include "LocalTraceCollector.hxx"
34
35 // ============================================================================
36 /*!
37  *  This class is for use without CORBA, inside or outside SALOME.
38  *  SALOME uses SALOMETraceCollector, to allow trace collection via CORBA.
39  *  Type of trace (and corresponding class) is chosen in LocalTraceBufferPool.
40  *
41  *  Guarantees a unique object instance of the class (singleton thread safe)
42  *  a separate thread for loop to print traces is launched.
43  */
44 // ============================================================================
45
46 BaseTraceCollector* LocalTraceCollector::instance()
47 {
48   if (_singleton == 0) // no need of lock when singleton already exists
49     {
50       int ret;
51       ret = pthread_mutex_lock(&_singletonMutex); // acquire lock to be alone
52       if (_singleton == 0)                     // another thread may have got
53         {                                      // the lock after the first test
54           BaseTraceCollector* myInstance = new LocalTraceCollector();
55
56           sem_init(&_sem,0,0); // to wait until run thread is initialized
57           pthread_t traceThread;
58           pthread_create(&traceThread, NULL,
59                                    LocalTraceCollector::run, NULL);
60           sem_wait(&_sem);
61           _singleton = myInstance; // _singleton known only when init done
62         }
63       ret = pthread_mutex_unlock(&_singletonMutex); // release lock
64     }
65   return _singleton;
66 }
67
68 // ============================================================================
69 /*!
70  *  In a separate thread, loop to print traces.
71  *  Mutex guarantees initialisation on instance method is done and only one run
72  *  allowed (double check ...)
73  *  Loop until there is no more buffer to print,
74  *  and no ask for end from destructor.
75  *  Get a buffer. If type = ABORT then exit application with message.
76  */
77 // ============================================================================
78
79 void* LocalTraceCollector::run(void *bid)
80 {
81   _threadId = new pthread_t;
82   *_threadId = pthread_self();
83   sem_post(&_sem); // unlock instance
84
85   LocalTraceBufferPool* myTraceBuffer = LocalTraceBufferPool::instance();
86   LocalTrace_TraceInfo myTrace;
87
88   // --- Loop until there is no more buffer to print,
89   //     and no ask for end from destructor.
90
91   while ((!_threadToClose) || myTraceBuffer->toCollect() )
92     {
93       if (_threadToClose)
94         {
95           DEVTRACE("FileTraceCollector _threadToClose");
96           //break;
97         }
98
99       myTraceBuffer->retrieve(myTrace);
100       if (myTrace.traceType == ABORT_MESS)
101         {
102           std::cout << std::flush ;
103 #ifndef WIN32
104           std::cerr << "INTERRUPTION from thread " << myTrace.threadId
105                << " : " <<  myTrace.trace;
106 #else
107           std::cerr << "INTERRUPTION from thread " << (void*)(&myTrace.threadId)
108                << " : " <<  myTrace.trace;
109 #endif
110           std::cerr << std::flush ; 
111           exit(1);     
112         }
113       else if (myTrace.traceType == NORMAL_MESS)
114         {
115           std::cout << std::flush ;
116 #ifndef WIN32
117           std::cerr << "th. " << myTrace.threadId << " " << myTrace.trace;
118 #else
119           std::cerr << "th. " << (void*)(&myTrace.threadId)
120                << " " << myTrace.trace;
121 #endif
122           std::cerr << std::flush ; 
123         }
124       else 
125         {
126           std::cout << std::flush ;
127 #ifndef WIN32
128           std::cerr << myTrace.trace;
129 #else
130           std::cerr << myTrace.trace;
131 #endif
132           std::cerr << std::flush ; 
133         }
134     }
135   pthread_exit(NULL);
136   return NULL;
137 }
138
139 // ============================================================================
140 /*!
141  *  Destructor: wait until printing thread ends (LocalTraceCollector::run)
142  */
143 // ============================================================================
144
145 LocalTraceCollector:: ~LocalTraceCollector()
146 {
147   int ret;
148   ret = pthread_mutex_lock(&_singletonMutex); // acquire lock to be alone
149   if (_singleton)
150     {
151       DEVTRACE("LocalTraceCollector:: ~LocalTraceCollector()");
152       LocalTraceBufferPool* myTraceBuffer = LocalTraceBufferPool::instance();
153       _threadToClose = 1;
154       myTraceBuffer->insert(NORMAL_MESS,"end of trace\n"); // to wake up thread
155       if (_threadId)
156         {
157           int ret = pthread_join(*_threadId, NULL);
158           if (ret) std::cerr << "error close LocalTraceCollector : "<< ret << std::endl;
159           else DEVTRACE("LocalTraceCollector destruction OK");
160           delete _threadId;
161           _threadId = 0;
162           _threadToClose = 0;
163         }
164       _singleton = 0;
165     }
166   ret = pthread_mutex_unlock(&_singletonMutex); // release lock
167 }
168
169 // ============================================================================
170 /*!
171  * Constructor: no need of LocalTraceBufferPool object initialization here,
172  * thread safe singleton used in LocalTraceBufferPool::instance()
173  */
174 // ============================================================================
175
176 LocalTraceCollector::LocalTraceCollector()
177 {
178   _threadId=0;
179   _threadToClose = 0;
180 }
181
182