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