Back to index

salome-kernel  6.5.0
FileTraceCollector.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00019 //
00020 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 //
00022 
00023 //  File   : FileTraceCollector.cxx
00024 //  Author : Paul RASCLE (EDF)
00025 //  Module : KERNEL
00026 //  $Header: /home/server/cvs/KERNEL/KERNEL_SRC/src/SALOMELocalTrace/FileTraceCollector.cxx,v 1.7.2.1.10.2.12.1 2012-04-12 14:05:31 vsr Exp $
00027 //
00028 #include <iostream>
00029 #include <sstream>
00030 #include <fstream>
00031 #include <cstdlib>
00032 
00033 //#define _DEVDEBUG_
00034 #include "FileTraceCollector.hxx"
00035 
00036 // Class attributes initialisation, for class method FileTraceCollector::run
00037 
00038 std::string FileTraceCollector::_fileName = "";
00039 
00040 // ============================================================================
00049 // ============================================================================
00050 
00051 BaseTraceCollector* FileTraceCollector::instance(const char *fileName)
00052 {
00053   if (_singleton == 0) // no need of lock when singleton already exists
00054     {
00055       int ret;
00056       ret = pthread_mutex_lock(&_singletonMutex); // acquire lock to be alone
00057       if (_singleton == 0)                     // another thread may have got
00058         {                                      // the lock after the first test
00059           DEVTRACE("FileTraceCollector:: instance()");
00060           BaseTraceCollector* myInstance = new FileTraceCollector();
00061           _fileName = fileName;
00062           DEVTRACE(" _fileName: " << _fileName);
00063 
00064           sem_init(&_sem,0,0); // to wait until run thread is initialized
00065           pthread_t traceThread;
00066           int bid = 0;
00067           pthread_create(&traceThread, NULL,
00068                                    FileTraceCollector::run, &bid);
00069           sem_wait(&_sem);
00070           _singleton = myInstance; // _singleton known only when init done
00071           DEVTRACE("FileTraceCollector:: instance()-end");
00072         }
00073       ret = pthread_mutex_unlock(&_singletonMutex); // release lock
00074     }
00075   return _singleton;
00076 }
00077 
00078 // ============================================================================
00087 // ============================================================================
00088 
00089 void* FileTraceCollector::run(void *bid)
00090 {
00091   //DEVTRACE("init run");
00092   _threadId = new pthread_t;
00093   *_threadId = pthread_self();
00094   sem_post(&_sem); // unlock instance
00095 
00096   LocalTraceBufferPool* myTraceBuffer = LocalTraceBufferPool::instance();
00097   LocalTrace_TraceInfo myTrace;
00098 
00099   // --- opens a file with append mode
00100   //     so, several processes can share the same file
00101 
00102   std::ofstream traceFile;
00103   const char *theFileName = _fileName.c_str();
00104   DEVTRACE("try to open trace file "<< theFileName);
00105   traceFile.open(theFileName, std::ios::out | std::ios::app);
00106   if (!traceFile)
00107     {
00108       std::cerr << "impossible to open trace file "<< theFileName << std::endl;
00109       exit (1);
00110     }
00111 
00112   // --- Loop until there is no more buffer to print,
00113   //     and no ask for end from destructor.
00114   DEVTRACE("Begin loop");
00115   while ((!_threadToClose) || myTraceBuffer->toCollect() )
00116     {
00117       if (_threadToClose)
00118         {
00119           DEVTRACE("FileTraceCollector _threadToClose");
00120           //break;
00121         }
00122 
00123       myTraceBuffer->retrieve(myTrace);
00124       if (myTrace.traceType == ABORT_MESS)
00125         {
00126 #ifndef WIN32
00127           traceFile << "INTERRUPTION from thread " << myTrace.threadId
00128                     << " : " <<  myTrace.trace;
00129 #else
00130           traceFile << "INTERRUPTION from thread "
00131                     << (void*)(&myTrace.threadId)
00132                     << " : " <<  myTrace.trace;
00133 #endif
00134           traceFile.close();
00135           std::cout << std::flush ;
00136 #ifndef WIN32
00137           std::cerr << "INTERRUPTION from thread " << myTrace.threadId
00138                << " : " <<  myTrace.trace;
00139 #else
00140           std::cerr << "INTERRUPTION from thread " << (void*)(&myTrace.threadId)
00141                << " : " <<  myTrace.trace;
00142 #endif
00143           std::cerr << std::flush ; 
00144           exit(1);     
00145         }
00146       else
00147         {
00148 #ifndef WIN32
00149           traceFile << "th. " << myTrace.threadId
00150                     << " " << myTrace.trace;
00151 #else
00152           traceFile << "th. " << (void*)(&myTrace.threadId)
00153                     << " " << myTrace.trace;
00154 #endif
00155         }
00156     }
00157   DEVTRACE("traceFile.close()");
00158   traceFile.close();
00159   DEVTRACE("traceFile.close()_end");
00160   pthread_exit(NULL);
00161 }
00162 
00163 // ============================================================================
00167 // ============================================================================
00168 
00169 FileTraceCollector:: ~FileTraceCollector()
00170 {
00171   int ret;
00172   ret = pthread_mutex_lock(&_singletonMutex); // acquire lock to be alone
00173   if (_singleton)
00174     {
00175       DEVTRACE("FileTraceCollector:: ~FileTraceCollector()");
00176       LocalTraceBufferPool* myTraceBuffer = LocalTraceBufferPool::instance();
00177       _threadToClose = 1;
00178       myTraceBuffer->insert(NORMAL_MESS,"end of trace\n"); // to wake up thread
00179       if (_threadId)
00180         {
00181           int ret = pthread_join(*_threadId, NULL);
00182           if (ret) std::cerr << "error close FileTraceCollector : "<< ret << std::endl;
00183           else DEVTRACE("FileTraceCollector destruction OK");
00184           delete _threadId;
00185           _threadId = 0;
00186           _threadToClose = 0;
00187         }
00188       _singleton = 0;
00189     }
00190   ret = pthread_mutex_unlock(&_singletonMutex); // release lock
00191 }
00192 
00193 // ============================================================================
00198 // ============================================================================
00199 
00200 FileTraceCollector::FileTraceCollector()
00201 {
00202   _threadId=0;
00203   _threadToClose = 0;
00204 }
00205 
00206