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