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)
31 #include <omniORB4/CORBA.h>
35 #include "SALOMETraceCollector.hxx"
36 #include "TraceCollector_WaitForServerReadiness.hxx"
37 #include <SALOMEconfig.h>
38 #include CORBA_CLIENT_HEADER(Logger)
40 // Class attributes initialisation, for class method SALOMETraceCollector::run
42 CORBA::ORB_ptr SALOMETraceCollector::_orb = 0;
44 // ============================================================================
46 * This class is for use with CORBA, inside SALOME.
47 * Type of trace (and corresponding class) is choosen in LocalTraceBufferPool.
49 * Guarantees a unique object instance of the class (singleton thread safe)
50 * a separate thread for loop to print traces is launched.
52 // ============================================================================
54 BaseTraceCollector* SALOMETraceCollector::instance()
56 if (_singleton == 0) // no need of lock when singleton already exists
59 ret = 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();
65 char ** argv = &_argv;
66 _orb = CORBA::ORB_init (argc, argv);
68 sem_init(&_sem,0,0); // to wait until run thread is initialized
69 pthread_t traceThread;
71 int re2 = pthread_create(&traceThread, NULL,
72 SALOMETraceCollector::run, (void *)bid);
74 _singleton = myInstance; // _singleton known only when init done
76 ret = pthread_mutex_unlock(&_singletonMutex); // release lock
81 // ============================================================================
83 * In a separate thread, loop to print traces.
84 * Mutex garantees intialisation 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.
90 // ============================================================================
92 void* SALOMETraceCollector::run(void *bid)
94 _threadId = new pthread_t;
95 *_threadId = pthread_self();
96 sem_post(&_sem); // unlock instance
98 LocalTraceBufferPool* myTraceBuffer = LocalTraceBufferPool::instance();
99 LocalTrace_TraceInfo myTrace;
101 SALOME_Logger::Logger_var m_pInterfaceLogger;
102 CORBA::Object_var obj;
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))
109 cerr << "Logger server not found ! Abort" << endl;
115 CORBA::String_var LogMsg =
116 CORBA::string_dup("\n---Init logger trace---\n");
117 m_pInterfaceLogger->putMessage(LogMsg);
118 DEVTRACE("Logger server found");
121 // --- Loop until there is no more buffer to print,
122 // and no ask for end from destructor.
124 while ((!_threadToClose) || myTraceBuffer->toCollect() )
128 DEVTRACE("SALOMETraceCollector _threadToClose");
132 int fullBuf = myTraceBuffer->retrieve(myTrace);
133 if (!CORBA::is_nil(_orb))
135 if (myTrace.traceType == ABORT_MESS)
137 stringstream abortMessage("");
139 abortMessage << "INTERRUPTION from thread "
140 << myTrace.threadId << " : " << myTrace.trace;
142 abortMessage << "INTERRUPTION from thread "
143 << (void*)&myTrace.threadId
144 << " : " << myTrace.trace;
146 CORBA::String_var LogMsg =
147 CORBA::string_dup(abortMessage.str().c_str());
148 m_pInterfaceLogger->putMessage(LogMsg);
153 stringstream aMessage("");
155 aMessage << "th. " << myTrace.threadId
157 aMessage << "th. " << (void*)&myTrace.threadId
159 << " " << myTrace.trace;
160 CORBA::String_var LogMsg =
161 CORBA::string_dup(aMessage.str().c_str());
162 m_pInterfaceLogger->putMessage(LogMsg);
170 // ============================================================================
172 * Destructor: wait until printing thread ends (SALOMETraceCollector::run)
174 // ============================================================================
176 SALOMETraceCollector:: ~SALOMETraceCollector()
179 ret = pthread_mutex_lock(&_singletonMutex); // acquire lock to be alone
182 DEVTRACE("SALOMETraceCollector:: ~SALOMETraceCollector()");
183 LocalTraceBufferPool* myTraceBuffer = LocalTraceBufferPool::instance();
185 myTraceBuffer->insert(NORMAL_MESS,"end of trace\n"); // to wake up thread
188 int ret = pthread_join(*_threadId, NULL);
189 if (ret) cerr << "error close SALOMETraceCollector : "<< ret << endl;
190 else DEVTRACE("SALOMETraceCollector destruction OK");
196 ret = pthread_mutex_unlock(&_singletonMutex); // release lock
199 // ============================================================================
201 * Constructor: no need of LocalTraceBufferPool object initialization here,
202 * thread safe singleton used in LocalTraceBufferPool::instance()
204 // ============================================================================
206 SALOMETraceCollector::SALOMETraceCollector()
212 // ============================================================================
216 // ============================================================================
220 SALOMETRACECOLLECTOR_EXPORT
221 BaseTraceCollector *SingletonInstance(void)
223 BaseTraceCollector *instance = SALOMETraceCollector::instance();