]> SALOME platform Git repositories - modules/kernel.git/commitdiff
Salome HOME
PR: trace debug at exit
authorprascle <prascle>
Wed, 5 Oct 2005 18:25:48 +0000 (18:25 +0000)
committerprascle <prascle>
Wed, 5 Oct 2005 18:25:48 +0000 (18:25 +0000)
src/Basics/BasicsGenericDestructor.cxx
src/Basics/BasicsGenericDestructor.hxx
src/SALOMELocalTrace/FileTraceCollector.cxx
src/SALOMELocalTrace/LocalTraceBufferPool.cxx
src/SALOMETraceCollector/SALOMETraceCollector.cxx

index f7707902fbd1d7e0c4d299c32b00471a24afa4d4..91585a2ae1d1a92b4162e9f6b4c195575bfcde75 100644 (file)
@@ -42,6 +42,14 @@ pthread_mutex_t PROTECTED_DELETE::_listMutex;
 std::list<GENERIC_DESTRUCTOR*> *GENERIC_DESTRUCTOR::Destructors = 0;
 static bool atExitSingletonDone = false ;
 
+// ============================================================================
+/*! 
+ *  deleteInstance deletes only once the object. Only object present on the
+ *  static list of PROTECTED_DELETE* are deleted, then removed of the list.
+ *  The operation is protected by a mutex.
+ */
+// ============================================================================
+
 void PROTECTED_DELETE::deleteInstance(PROTECTED_DELETE *anObject)
   {
     if (std::find(_objList.begin(), _objList.end(),anObject) == _objList.end())
@@ -53,25 +61,37 @@ void PROTECTED_DELETE::deleteInstance(PROTECTED_DELETE *anObject)
        if (std::find(_objList.begin(), _objList.end(), anObject)
            != _objList.end())
          {
-           cerr << "PROTECTED_DELETE::deleteInstance1 " << anObject << endl;
+           DEVTRACE("PROTECTED_DELETE::deleteInstance1 " << anObject);
            delete anObject;
-           cerr << "PROTECTED_DELETE::deleteInstance2 " << &_objList << endl;
+           DEVTRACE("PROTECTED_DELETE::deleteInstance2 " << &_objList);
            _objList.remove(anObject);
-           cerr << "PROTECTED_DELETE::deleteInstance3" << endl;
          }
        ret = pthread_mutex_unlock(&_listMutex); // release lock
       }
   }
 
+// ============================================================================
+/*! 
+ * To allow a further destruction of a PRTECTED_DELETE object, it must be added
+ * to the static list of PROTECTED_DELETE*
+ */
+// ============================================================================
+
 void PROTECTED_DELETE::addObj(PROTECTED_DELETE *anObject)
 {
-  cerr << "PROTECTED_DELETE::addObj " << anObject << endl;
+  DEVTRACE("PROTECTED_DELETE::addObj " << anObject);
   _objList.push_back(anObject);
 }
 
+// ============================================================================
+/*! 
+ *  Herited classes have there own destructors
+ */
+// ============================================================================
+
 PROTECTED_DELETE::~PROTECTED_DELETE()
 {
-  cerr << "PROTECTED_DELETE::~PROTECTED_DELETE()" << endl;
+  DEVTRACE("PROTECTED_DELETE::~PROTECTED_DELETE()");
 }
 
 // ============================================================================
@@ -92,7 +112,7 @@ public:
   {
     if (Make_ATEXIT && !atExitSingletonDone)
       {
-       cerr << "atExitSingleton(" << Make_ATEXIT << ")" << endl;
+       DEVTRACE("atExitSingleton(" << Make_ATEXIT << ")");
        assert(GENERIC_DESTRUCTOR::Destructors == 0);
        GENERIC_DESTRUCTOR::Destructors = new std::list<GENERIC_DESTRUCTOR*>;
        int cr = atexit(HouseKeeping);
@@ -103,7 +123,7 @@ public:
 
   ~atExitSingleton()
   {
-    cerr << "atExitSingleton::~atExitSingleton()" << endl << flush;
+    DEVTRACE("atExitSingleton::~atExitSingleton()");
   }
 };
 
@@ -121,7 +141,7 @@ static atExitSingleton HouseKeeper = atExitSingleton(false);
 
 void HouseKeeping( void )
 {
-  cerr << "HouseKeeping()" << endl;
+  DEVTRACE("HouseKeeping()");
   assert(GENERIC_DESTRUCTOR::Destructors);
   if(GENERIC_DESTRUCTOR::Destructors->size())
     {
@@ -132,22 +152,22 @@ void HouseKeeping( void )
        {
          it-- ;
          GENERIC_DESTRUCTOR* ptr = *it ;
-         cerr << "HouseKeeping() " << typeid(ptr).name() << endl;
+         DEVTRACE("HouseKeeping() " << typeid(ptr).name());
          (*ptr)();
          delete ptr ;
        }
       while (it !=  GENERIC_DESTRUCTOR::Destructors->begin()) ;
 
-      cerr << "HouseKeeping() end list "<< endl;
+      DEVTRACE("HouseKeeping() end list ");
       GENERIC_DESTRUCTOR::Destructors->clear() ;
       assert(GENERIC_DESTRUCTOR::Destructors->size() == 0);
       assert(GENERIC_DESTRUCTOR::Destructors->empty());
-      cerr << "HouseKeeping()after clear "<< endl;
+      DEVTRACE("HouseKeeping()after clear ");
     }
 
   delete GENERIC_DESTRUCTOR::Destructors;
   GENERIC_DESTRUCTOR::Destructors = 0;
-  cerr << "HouseKeeping() very end "<< endl;
+  DEVTRACE("HouseKeeping() very end ");
   return ;
 }
 
@@ -160,18 +180,13 @@ void HouseKeeping( void )
 
 const int GENERIC_DESTRUCTOR::Add(GENERIC_DESTRUCTOR &anObject)
 {
-  cerr << "GENERIC_DESTRUCTOR::Add("<<typeid(anObject).name()<<") "
-       << &anObject << endl;
+  DEVTRACE("GENERIC_DESTRUCTOR::Add("<<typeid(anObject).name()<<") "
+          << &anObject);
   if (!atExitSingletonDone)
     {
       HouseKeeper = atExitSingleton(true);
     }
   assert(Destructors);
-//   if (std::find(Destructors->begin(), Destructors->end(), &anObject)
-//       == Destructors->end())
-//     {
-//       Destructors->push_back(&anObject);
-//     }
   Destructors->push_back(&anObject);
   return Destructors->size();
 }
index 607d849d4969f0722eee920083be613478740ce6..a5d934dd8d100fb9e839c27b0b3e50fc87c9d4d5 100644 (file)
@@ -33,6 +33,8 @@
 #include <algorithm>
 #include <cassert>
 #include <typeinfo>
+#include <iostream>
+#include <cstdlib>
 #include <pthread.h>
 
 #if defined BASICS_EXPORTS
 #endif
 #endif
 
+//#define _DEVDEBUG_
+
+#ifdef _DEVDEBUG_
+#define MYDEVTRACE {std::cerr << __FILE__ << " [" << __LINE__ << "] : ";}
+#define DEVTRACE(msg) {MYDEVTRACE; std::cerr<<msg<<std::endl<<std::flush;}
+#else
+#define MYDEVTRACE
+#define DEVTRACE(msg)
+#endif
+
+// ============================================================================
+/*!
+ * The PROTECTED_DELETE base class provides a protected destructor. 
+ * The only way to use PROTECTED_DELETE is inheritance:
+ *   example: class LocalTraceBufferPool : public PROTECTED_DELETE
+ * Herited class destructor must stay protected.
+ * Normal use is:
+ * - create an instance of herited class on the heap (new),
+ * - use addObj(instance) to store the instance on the static list _objList,
+ * - delete instance with deleteInstance(instance)
+ *
+ * This class is utilised with GENERIC_DESTRUCTOR and DESTRUCTOR_OF, 
+ * to program automatic deletion of objects at the end of the process, while
+ * keeping the possibility of an early destruction, if required. This is used
+ * for unit testing and trace mecanism.
+ */ 
+// ============================================================================
+
 class PROTECTED_DELETE
 {
 public:
@@ -122,8 +152,8 @@ public:
   DESTRUCTOR_OF(TYPE &anObject):
     _objectPtr(&anObject)
   {
-    std::cerr << " DESTRUCTOR_OF " << typeid(anObject).name() 
-             << " " << _objectPtr << " " << this << endl;
+    DEVTRACE(" DESTRUCTOR_OF " << typeid(anObject).name() 
+            << " " << _objectPtr << " " << this );
     PROTECTED_DELETE::addObj(_objectPtr);
     assert(GENERIC_DESTRUCTOR::Add(*this) >= 0);
   }
@@ -137,11 +167,7 @@ public:
   {
     if (_objectPtr)
       {
-       //TYPE* aPtr = static_cast<TYPE*>(_objectPtr);
-       //std::cerr << "DESTRUCTOR_OF<" << typeid(*aPtr).name() 
-        //          << ">::operator() " << _objectPtr << " " << aPtr << endl;
-       //if (aPtr) PROTECTED_DELETE::deleteInstance(_objectPtr);
-       std::cerr << "DESTRUCTOR_OF<>::operator() " << _objectPtr << endl;
+       DEVTRACE("DESTRUCTOR_OF<>::operator() " << _objectPtr);
        if (_objectPtr) PROTECTED_DELETE::deleteInstance(_objectPtr);
        _objectPtr = NULL;
       }
@@ -149,7 +175,7 @@ public:
 
   virtual ~DESTRUCTOR_OF()
   {
-    cerr << "~DESTRUCTOR_OF() " << this << endl;
+    DEVTRACE("~DESTRUCTOR_OF() " << this);
     assert(!_objectPtr);
   }
 
index 6015c5070d54652fef9c6652d36ebfbfa04ed675..5dd1074be9b549fb874427ed8a33cfd66f9f0296 100644 (file)
@@ -56,17 +56,17 @@ BaseTraceCollector* FileTraceCollector::instance(const char *fileName)
       ret = pthread_mutex_lock(&_singletonMutex); // acquire lock to be alone
       if (_singleton == 0)                     // another thread may have got
        {                                      // the lock after the first test
-         //cerr << "FileTraceCollector:: instance()" << endl << flush;
+         DEVTRACE("FileTraceCollector:: instance()");
          _singleton = new FileTraceCollector();
 
          _fileName = fileName;
-         //cerr << " _fileName: " << _fileName << endl;
+         DEVTRACE(" _fileName: " << _fileName);
 
          pthread_t traceThread;
          int bid;
          int re2 = pthread_create(&traceThread, NULL,
                                   FileTraceCollector::run, (void *)bid);
-         //cerr << "FileTraceCollector:: instance()-end" << endl << flush;
+         DEVTRACE("FileTraceCollector:: instance()-end");
        }
       ret = pthread_mutex_unlock(&_singletonMutex); // release lock
     }
@@ -131,7 +131,7 @@ void* FileTraceCollector::run(void *bid)
       while ((!_threadToClose) || myTraceBuffer->toCollect() )
        {
          if (_threadToClose)
-           cerr << "FileTraceCollector _threadToClose" << endl << flush;
+           DEVTRACE("FileTraceCollector _threadToClose");
 
          int fullBuf = myTraceBuffer->retrieve(myTrace);
          if (myTrace.traceType == ABORT_MESS)
@@ -167,9 +167,9 @@ void* FileTraceCollector::run(void *bid)
 #endif
            }
        }
-      cerr <<"traceFile.close();" << endl << flush;
+      DEVTRACE("traceFile.close()");
       traceFile.close();
-      cerr <<"traceFile.close()_end;" << endl << flush;
+      DEVTRACE("traceFile.close()_end");
       pthread_exit(NULL);
     }
   //return NULL;
@@ -187,7 +187,7 @@ FileTraceCollector:: ~FileTraceCollector()
   ret = pthread_mutex_lock(&_singletonMutex); // acquire lock to be alone
   if (_singleton)
     {
-      cerr << "FileTraceCollector:: ~FileTraceCollector()" << endl << flush;
+      DEVTRACE("FileTraceCollector:: ~FileTraceCollector()");
       LocalTraceBufferPool* myTraceBuffer = LocalTraceBufferPool::instance();
       _threadToClose = 1;
       myTraceBuffer->insert(NORMAL_MESS,"end of trace\n"); // to wake up thread
@@ -195,7 +195,7 @@ FileTraceCollector:: ~FileTraceCollector()
        {
          int ret = pthread_join(*_threadId, NULL);
          if (ret) cerr << "error close FileTraceCollector : "<< ret << endl;
-         else cerr << "FileTraceCollector destruction OK" << endl;
+         else DEVTRACE("FileTraceCollector destruction OK");
          _threadId = 0;
          _threadToClose = 0;
        }
index bdc98695dfb520979c85be35f61d7c7434ebdf74..d875fe5bee8572ed42f6758b866ac8de2da65814 100644 (file)
@@ -285,14 +285,14 @@ LocalTraceBufferPool::~LocalTraceBufferPool()
   int ret = pthread_mutex_lock(&_singletonMutex); // acquire lock to be alone
   if (_singleton)
     {
-      cerr << "LocalTraceBufferPool::~LocalTraceBufferPool()" << endl<<flush;
+      DEVTRACE("LocalTraceBufferPool::~LocalTraceBufferPool()");
       delete (_myThreadTrace);
       _myThreadTrace = 0;
       int ret;
       ret=sem_destroy(&_freeBufferSemaphore);
       ret=sem_destroy(&_fullBufferSemaphore);
       ret=pthread_mutex_destroy(&_incrementMutex);
-      cerr<<"LocalTraceBufferPool::~LocalTraceBufferPool()-end"<<endl<<flush;
+      DEVTRACE("LocalTraceBufferPool::~LocalTraceBufferPool()-end");
       _singleton = 0;
       ret = pthread_mutex_unlock(&_singletonMutex); // release lock
     }
index 3b31be363af8391d2ecd77bc950d070e293a7a82..de0331d96a1036bc4e2428b3f6238189707c4711 100644 (file)
@@ -132,7 +132,7 @@ void* SALOMETraceCollector::run(void *bid)
          CORBA::String_var LogMsg =
            CORBA::string_dup("\n---Init logger trace---\n");
          m_pInterfaceLogger->putMessage(LogMsg);
-         //cerr << " Logger server found" << endl;
+         DEVTRACE("Logger server found");
        }
 
       // --- Loop until there is no more buffer to print,
@@ -191,7 +191,7 @@ SALOMETraceCollector:: ~SALOMETraceCollector()
   ret = pthread_mutex_lock(&_singletonMutex); // acquire lock to be alone
   if (_singleton)
     {
-      //cerr << "SALOMETraceCollector:: ~SALOMETraceCollector()" <<endl<<flush;
+      DEVTRACE("SALOMETraceCollector:: ~SALOMETraceCollector()");
       LocalTraceBufferPool* myTraceBuffer = LocalTraceBufferPool::instance();
       _threadToClose = 1;
       myTraceBuffer->insert(NORMAL_MESS,"end of trace\n"); // to wake up thread
@@ -199,7 +199,7 @@ SALOMETraceCollector:: ~SALOMETraceCollector()
        {
          int ret = pthread_join(*_threadId, NULL);
          if (ret) cerr << "error close SALOMETraceCollector : "<< ret << endl;
-         //else cerr << "SALOMETraceCollector destruction OK" << endl;
+         else DEVTRACE("SALOMETraceCollector destruction OK");
          _threadId = 0;
          _threadToClose = 0;
        }