1 // Copyright (C) 2004 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either
7 // version 2.1 of the License.
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // Lesser General Public License for more details.
14 // You should have received a copy of the GNU Lesser General Public
15 // License along with this library; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 // File : LocalTraceCollector.cxx
23 // Author : Paul RASCLE (EDF)
27 #include <SALOMEconfig.h>
33 #include <omniORB4/CORBA.h>
37 #include "SALOMETraceCollector.hxx"
38 #include "TraceCollector_WaitForServerReadiness.hxx"
39 #include <SALOMEconfig.h>
40 #include CORBA_CLIENT_HEADER(Logger)
42 // Class attributes initialisation, for class method SALOMETraceCollector::run
44 CORBA::ORB_ptr SALOMETraceCollector::_orb = 0;
46 // ============================================================================
48 * This class is for use with CORBA, inside SALOME.
49 * Type of trace (and corresponding class) is choosen in LocalTraceBufferPool.
51 * Guarantees a unique object instance of the class (singleton thread safe)
52 * a separate thread for loop to print traces is launched.
54 // ============================================================================
56 BaseTraceCollector* SALOMETraceCollector::instance()
58 if (_singleton == 0) // no need of lock when singleton already exists
61 ret = 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();
67 char ** argv = &_argv;
68 _orb = CORBA::ORB_init (argc, argv);
70 sem_init(&_sem,0,0); // to wait until run thread is initialized
71 pthread_t traceThread;
73 pthread_create(&traceThread, NULL,
74 SALOMETraceCollector::run, (void *)bid);
76 _singleton = myInstance; // _singleton known only when init done
78 ret = pthread_mutex_unlock(&_singletonMutex); // release lock
83 // ============================================================================
85 * In a separate thread, loop to print traces.
86 * Mutex garantees intialisation on instance method is done and only one run
87 * allowed (double check ...)
88 * Loop until there is no more buffer to print,
89 * and no ask for end from destructor.
90 * Get a buffer. If type = ABORT then exit application with message.
92 // ============================================================================
94 void* SALOMETraceCollector::run(void *bid)
96 _threadId = new pthread_t;
97 *_threadId = pthread_self();
98 sem_post(&_sem); // unlock instance
100 LocalTraceBufferPool* myTraceBuffer = LocalTraceBufferPool::instance();
101 LocalTrace_TraceInfo myTrace;
103 SALOME_Logger::Logger_var m_pInterfaceLogger;
104 CORBA::Object_var obj;
106 obj = TraceCollector_WaitForServerReadiness(_orb,"Logger");
107 if (!CORBA::is_nil(obj))
108 m_pInterfaceLogger = SALOME_Logger::Logger::_narrow(obj);
109 if (CORBA::is_nil(m_pInterfaceLogger))
111 cerr << "Logger server not found ! Abort" << endl;
117 CORBA::String_var LogMsg =
118 CORBA::string_dup("\n---Init logger trace---\n");
119 m_pInterfaceLogger->putMessage(LogMsg);
120 DEVTRACE("Logger server found");
123 // --- Loop until there is no more buffer to print,
124 // and no ask for end from destructor.
126 while ((!_threadToClose) || myTraceBuffer->toCollect() )
130 DEVTRACE("SALOMETraceCollector _threadToClose");
134 myTraceBuffer->retrieve(myTrace);
135 if (!CORBA::is_nil(_orb))
137 if (myTrace.traceType == ABORT_MESS)
139 stringstream abortMessage("");
141 abortMessage << "INTERRUPTION from thread "
142 << myTrace.threadId << " : " << myTrace.trace;
144 abortMessage << "INTERRUPTION from thread "
145 << (void*)&myTrace.threadId
146 << " : " << myTrace.trace;
148 CORBA::String_var LogMsg =
149 CORBA::string_dup(abortMessage.str().c_str());
150 m_pInterfaceLogger->putMessage(LogMsg);
155 stringstream aMessage("");
157 aMessage << "th. " << myTrace.threadId
159 aMessage << "th. " << (void*)&myTrace.threadId
161 << " " << myTrace.trace;
162 CORBA::String_var LogMsg =
163 CORBA::string_dup(aMessage.str().c_str());
164 m_pInterfaceLogger->putMessage(LogMsg);
172 // ============================================================================
174 * Destructor: wait until printing thread ends (SALOMETraceCollector::run)
176 // ============================================================================
178 SALOMETraceCollector:: ~SALOMETraceCollector()
181 ret = pthread_mutex_lock(&_singletonMutex); // acquire lock to be alone
184 DEVTRACE("SALOMETraceCollector:: ~SALOMETraceCollector()");
185 LocalTraceBufferPool* myTraceBuffer = LocalTraceBufferPool::instance();
187 myTraceBuffer->insert(NORMAL_MESS,"end of trace\n"); // to wake up thread
190 int ret = pthread_join(*_threadId, NULL);
191 if (ret) cerr << "error close SALOMETraceCollector : "<< ret << endl;
192 else DEVTRACE("SALOMETraceCollector destruction OK");
199 ret = pthread_mutex_unlock(&_singletonMutex); // release lock
202 // ============================================================================
204 * Constructor: no need of LocalTraceBufferPool object initialization here,
205 * thread safe singleton used in LocalTraceBufferPool::instance()
207 // ============================================================================
209 SALOMETraceCollector::SALOMETraceCollector()
215 // ============================================================================
219 // ============================================================================
223 SALOMETRACECOLLECTOR_EXPORT
224 BaseTraceCollector *SingletonInstance(void)
226 BaseTraceCollector *instance = SALOMETraceCollector::instance();