Salome HOME
a5e3bb7c7f477b356219b79aaed5011ef94baa0c
[modules/kernel.git] / src / SALOMETraceCollector / SALOMETraceCollector.cxx
1 // Copyright (C) 2007-2021  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 <SALOMEconfig.h>
29
30 #include <iostream>
31 #include <sstream>
32 #include <fstream>
33 #include <cstdlib>
34 #include <omniORB4/CORBA.h>
35
36 #include "SALOMETraceCollector.hxx"
37 #include "TraceCollector_WaitForServerReadiness.hxx"
38 #include <SALOMEconfig.h>
39 #include CORBA_CLIENT_HEADER(Logger)
40
41 // Class attributes initialisation, for class method SALOMETraceCollector::run
42
43 CORBA::ORB_ptr SALOMETraceCollector::_orb = 0;
44
45 // ============================================================================
46 /*!
47  *  This class is for use with CORBA, inside SALOME.
48  *  Type of trace (and corresponding class) is chosen in LocalTraceBufferPool.
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  */
53 // ============================================================================
54
55 BaseTraceCollector* SALOMETraceCollector::instance()
56 {
57   if (_singleton == 0) // no need of lock when singleton already exists
58     {
59       pthread_mutex_lock(&_singletonMutex);    // acquire lock to be alone
60       if (_singleton == 0)                     // another thread may have got
61         {                                      // the lock after the first test
62           BaseTraceCollector* myInstance = new SALOMETraceCollector();
63           int argc=0;
64           char *_argv=0;
65           char ** argv = &_argv;
66           _orb = CORBA::ORB_init (argc, argv);
67
68           sem_init(&_sem,0,0); // to wait until run thread is initialized
69           pthread_t traceThread;
70           int bid = 0;
71           pthread_create(&traceThread, NULL,
72                                    SALOMETraceCollector::run, &bid);
73           sem_wait(&_sem);
74           _singleton = myInstance; // _singleton known only when init done
75         }
76       pthread_mutex_unlock(&_singletonMutex); // release lock
77     }
78   return _singleton;
79 }
80
81 // ============================================================================
82 /*!
83  *  In a separate thread, loop to print traces.
84  *  Mutex guarantees initialisation on instance method is done and only one run
85  *  allowed (double check ...)
86  *  Loop until there is no more buffer to print,
87  *  and no ask for end from destructor.
88  *  Get a buffer. If type = ABORT then exit application with message.
89  */
90 // ============================================================================
91
92 void* SALOMETraceCollector::run(void* /*bid*/)
93 {
94   _threadId = new pthread_t;
95   *_threadId = pthread_self();
96   sem_post(&_sem); // unlock instance
97
98   LocalTraceBufferPool* myTraceBuffer = LocalTraceBufferPool::instance();
99   LocalTrace_TraceInfo myTrace;
100
101   SALOME_Logger::Logger_var m_pInterfaceLogger;
102   CORBA::Object_var obj;
103
104   obj = TraceCollector_WaitForServerReadiness(_orb,"Logger");
105   if (!CORBA::is_nil(obj))
106     m_pInterfaceLogger = SALOME_Logger::Logger::_narrow(obj);
107   if (CORBA::is_nil(m_pInterfaceLogger))
108     {
109       std::cerr << "Logger server not found ! Abort" << std::endl;
110       std::cerr << std::flush ; 
111       exit(1);
112     } 
113   else
114     {
115       CORBA::String_var LogMsg =
116         CORBA::string_dup("\n---Init logger trace---\n");
117       m_pInterfaceLogger->putMessage(LogMsg);
118       DEVTRACE("Logger server found");
119     }
120
121   // --- Loop until there is no more buffer to print,
122   //     and no ask for end from destructor.
123
124   while ((!_threadToClose) || myTraceBuffer->toCollect() )
125     {
126       if (_threadToClose)
127         {
128           DEVTRACE("SALOMETraceCollector _threadToClose");
129           //break;
130         }
131
132       myTraceBuffer->retrieve(myTrace);
133       if (!CORBA::is_nil(_orb))
134         {
135           if (myTrace.traceType == ABORT_MESS)
136             {
137               std::stringstream abortMessage("");
138 #ifndef WIN32
139               abortMessage << "INTERRUPTION from thread "
140                            << myTrace.threadId << " : " << myTrace.trace;
141 #else
142               abortMessage << "INTERRUPTION from thread "
143                            << (void*)&myTrace.threadId 
144                            << " : " << myTrace.trace;
145 #endif
146               CORBA::String_var LogMsg =
147                 CORBA::string_dup(abortMessage.str().c_str());
148               m_pInterfaceLogger->putMessage(LogMsg);
149               exit(1);
150             }
151           else
152             {
153               std::stringstream aMessage("");
154 #ifndef WIN32
155               aMessage << "th. " << myTrace.threadId
156 #else
157                 aMessage << "th. " << (void*)&myTrace.threadId
158 #endif
159                        << " " << myTrace.trace;
160               CORBA::String_var LogMsg =
161                 CORBA::string_dup(aMessage.str().c_str());
162               m_pInterfaceLogger->putMessage(LogMsg);
163             }
164         }
165     }
166   pthread_exit(NULL);
167   return NULL;
168 }
169
170 // ============================================================================
171 /*!
172  *  Destructor: wait until printing thread ends (SALOMETraceCollector::run)
173  */
174 // ============================================================================
175
176 SALOMETraceCollector:: ~SALOMETraceCollector()
177 {
178   pthread_mutex_lock(&_singletonMutex); // acquire lock to be alone
179   if (_singleton)
180     {
181       DEVTRACE("SALOMETraceCollector:: ~SALOMETraceCollector()");
182       LocalTraceBufferPool* myTraceBuffer = LocalTraceBufferPool::instance();
183       _threadToClose = 1;
184       myTraceBuffer->insert(NORMAL_MESS,"end of trace\n"); // to wake up thread
185       if (_threadId)
186         {
187           int ret = pthread_join(*_threadId, NULL);
188           if (ret) { std::cerr << "error close SALOMETraceCollector : "<< ret << std::endl; }
189           else { DEVTRACE("SALOMETraceCollector destruction OK") };
190           delete _threadId;
191           _threadId = 0;
192           _threadToClose = 0;
193         }
194       _singleton = 0;
195     }
196   pthread_mutex_unlock(&_singletonMutex); // release lock
197 }
198
199 // ============================================================================
200 /*!
201  * Constructor: no need of LocalTraceBufferPool object initialization here,
202  * thread safe singleton used in LocalTraceBufferPool::instance()
203  */
204 // ============================================================================
205
206 SALOMETraceCollector::SALOMETraceCollector()
207 {
208   _threadId=0;
209   _threadToClose = 0;
210 }
211
212 // ============================================================================
213 /*!
214  * 
215  */
216 // ============================================================================
217
218 extern "C"
219 {
220  SALOMETRACECOLLECTOR_EXPORT
221   BaseTraceCollector *SingletonInstance(void)
222   {
223     BaseTraceCollector *instance = SALOMETraceCollector::instance();
224     return instance;
225   }
226 }