Salome HOME
Corrected implementation of MESSAGE functionality on Windows
[modules/kernel.git] / src / SALOMELocalTrace / LocalTraceBufferPool.cxx
index 73046c6ec5140365fa4d423ece8c9dda00885387..f41e57593b0c60599283b6b11c3468b1f44a820e 100644 (file)
@@ -1,4 +1,4 @@
-//  Copyright (C) 2004  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
+//  copyright (C) 2004  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS 
 // 
 //  This library is free software; you can redistribute it and/or 
@@ -15,7 +15,7 @@
 //  License along with this library; if not, write to the Free Software 
 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA 
 // 
-//  See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
+// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
 //
 //  Author : Paul RASCLE (EDF)
 //  Module : KERNEL
 
 #include <iostream>
 #include <limits.h>
+#include <cassert>
 
+#ifndef WNT
+#include <dlfcn.h>
+#else
+#include <windows.h>
+#endif
+
+//#define _DEVDEBUG_
 #include "LocalTraceBufferPool.hxx"
+#include "BaseTraceCollector.hxx"
+#include "LocalTraceCollector.hxx"
+#include "FileTraceCollector.hxx"
+#include "BasicsGenericDestructor.hxx"
 #include "utilities.h"
 
+using namespace std;
+
 // In case of truncated message, end of trace contains "...\n\0"
+
 #define TRUNCATED_MESSAGE "...\n"
 #define MAXMESS_LENGTH MAX_TRACE_LENGTH-5
 
-using namespace std;
+// Class static attributes initialisation
 
 LocalTraceBufferPool* LocalTraceBufferPool::_singleton = 0;
-pthread_mutex_t LocalTraceBufferPool::_singletonMutex;
+//#ifndef WNT
+//pthread_mutex_t LocalTraceBufferPool::_singletonMutex;
+//#else
+pthread_mutex_t LocalTraceBufferPool::_singletonMutex =
+  PTHREAD_MUTEX_INITIALIZER;
+//#endif
+BaseTraceCollector *LocalTraceBufferPool::_myThreadTrace = 0;
 
 // ============================================================================
 /*!
- *  guarantees a unique object instance of the class (singleton thread safe)
+ *  Guarantees a unique object instance of the class (singleton thread safe).
+ *  When the LocalTraceBufferPool instance is created, the trace collector is
+ *  also created (singleton). Type of trace collector to create depends on 
+ *  environment variable "SALOME_trace":
+ *  - "local" implies standard err trace, LocalTraceCollector is launched.
+ *  - "file" implies trace in /tmp/tracetest.log
+ *  - "file:pathname" implies trace in file pathname
+ *  - anything else like "other" : try to load dynamically a library named
+ *    otherTraceCollector, and invoque C method instance() to start a singleton
+ *    instance of the trace collector. Example: with_loggerTraceCollector, for
+ *    CORBA Log.
  */
 // ============================================================================
 
@@ -53,7 +84,71 @@ LocalTraceBufferPool* LocalTraceBufferPool::instance()
       ret = pthread_mutex_lock(&_singletonMutex); // acquire lock to be alone
       if (_singleton == 0)                     // another thread may have got
        {                                      // the lock after the first test
-         _singleton = new LocalTraceBufferPool(); 
+         DEVTRACE("New buffer pool");
+         LocalTraceBufferPool* myInstance = new LocalTraceBufferPool(); 
+
+         DESTRUCTOR_OF<LocalTraceBufferPool> *ptrDestroy =
+           new DESTRUCTOR_OF<LocalTraceBufferPool> (*myInstance);
+         _singleton = myInstance;
+
+         // --- start a trace Collector
+
+         char* traceKind = getenv("SALOME_trace");
+
+         if ( !traceKind || strcmp(traceKind,"local")==0 ) // mkr : 27.11.2006 : PAL13967 - Distributed supervision graphs - Problem with "SALOME_trace"
+           {
+             _myThreadTrace = LocalTraceCollector::instance();
+           }
+         else if (strncmp(traceKind,"file",strlen("file"))==0)
+           {
+             char *fileName;
+             if (strlen(traceKind) > strlen("file"))
+               fileName = &traceKind[strlen("file")+1];
+             else
+               fileName = "/tmp/tracetest.log";
+             
+             _myThreadTrace = FileTraceCollector::instance(fileName);
+           }
+         else // --- try a dynamic library
+           {
+#ifndef WNT
+             void* handle;
+             string impl_name = string ("lib") + traceKind 
+               + string("TraceCollector.so");
+             handle = dlopen( impl_name.c_str() , RTLD_LAZY ) ;
+#else
+             HINSTANCE handle;
+             string impl_name = string ("lib") + traceKind + string(".dll");
+             handle = LoadLibrary( impl_name.c_str() );
+#endif
+             if ( handle )
+               {
+                 typedef BaseTraceCollector * (*FACTORY_FUNCTION) (void);
+#ifndef WNT
+                 FACTORY_FUNCTION TraceCollectorFactory =
+                   (FACTORY_FUNCTION) dlsym(handle, "SingletonInstance");
+#else
+                 FACTORY_FUNCTION TraceCollectorFactory =
+                   (FACTORY_FUNCTION)GetProcAddress(handle, "SingletonInstance");
+#endif
+                 if ( !TraceCollectorFactory )
+                 {
+                     cerr << "Can't resolve symbol: SingletonInstance" <<endl;
+#ifndef WNT
+                     cerr << "dlerror: " << dlerror() << endl;
+#endif
+                     exit( 1 );
+                   }
+                 _myThreadTrace = (TraceCollectorFactory) ();
+               }
+             else
+               {
+                 cerr << "library: " << impl_name << " not found !" << endl;
+                 assert(handle); // to give file and line
+                 exit(1);        // in case assert is deactivated
+               }             
+           }
+         DEVTRACE("New buffer pool: end");
        }
       ret = pthread_mutex_unlock(&_singletonMutex); // release lock
     }
@@ -79,7 +174,12 @@ int LocalTraceBufferPool::insert(int traceType, const char* msg)
 
   // wait until there is a free buffer in the pool
 
-  int ret = sem_wait(&_freeBufferSemaphore);
+  int ret = -1;
+  while (ret)
+    {
+      ret = sem_wait(&_freeBufferSemaphore);
+      if (ret) perror(" LocalTraceBufferPool::insert, sem_wait");
+    }
 
   // get the next free buffer available (mutex protected) 
 
@@ -119,7 +219,12 @@ int LocalTraceBufferPool::retrieve(LocalTrace_TraceInfo& aTrace)
 
   // wait until there is a buffer in the pool, with a message to print
 
-  int ret = sem_wait(&_fullBufferSemaphore);
+  int ret = -1;
+  while (ret)
+    {
+      ret = sem_wait(&_fullBufferSemaphore);
+      if (ret) perror(" LocalTraceBufferPool::retrieve, sem_wait");
+    }
 
   // get the next buffer to print
 
@@ -168,7 +273,7 @@ unsigned long LocalTraceBufferPool::toCollect()
 
 LocalTraceBufferPool::LocalTraceBufferPool()
 {
-  //cout << "LocalTraceBufferPool::LocalTraceBufferPool()" << endl;
+  //cerr << "LocalTraceBufferPool::LocalTraceBufferPool()" << endl;
 
   _insertPos   = ULONG_MAX;  // first increment will give 0
   _retrievePos = ULONG_MAX;
@@ -184,6 +289,8 @@ LocalTraceBufferPool::LocalTraceBufferPool()
   if (ret!=0) IMMEDIATE_ABORT(ret);
   ret=pthread_mutex_init(&_incrementMutex,NULL); // default = fast mutex
   if (ret!=0) IMMEDIATE_ABORT(ret);
+
+  //cerr << "LocalTraceBufferPool::LocalTraceBufferPool()-end" << endl;
 }
 
 // ============================================================================
@@ -194,10 +301,20 @@ LocalTraceBufferPool::LocalTraceBufferPool()
 
 LocalTraceBufferPool::~LocalTraceBufferPool()
 {
-  int ret;
-  ret=sem_destroy(&_freeBufferSemaphore);
-  ret=sem_destroy(&_fullBufferSemaphore);
-  ret=pthread_mutex_destroy(&_incrementMutex);
+  int ret = pthread_mutex_lock(&_singletonMutex); // acquire lock to be alone
+  if (_singleton)
+    {
+      DEVTRACE("LocalTraceBufferPool::~LocalTraceBufferPool()");
+      delete (_myThreadTrace);
+      _myThreadTrace = 0;
+      int ret;
+      ret=sem_destroy(&_freeBufferSemaphore);
+      ret=sem_destroy(&_fullBufferSemaphore);
+      ret=pthread_mutex_destroy(&_incrementMutex);
+      DEVTRACE("LocalTraceBufferPool::~LocalTraceBufferPool()-end");
+      _singleton = 0;
+    }
+  ret = pthread_mutex_unlock(&_singletonMutex); // release lock
 }
 
 // ============================================================================
@@ -210,8 +327,8 @@ unsigned long LocalTraceBufferPool::lockedIncrement(unsigned long& pos)
 {
   int ret;
   ret = pthread_mutex_lock(&_incrementMutex);   // lock access to counters
-  pos++;
+  unsigned long mypos = ++pos;
   ret = pthread_mutex_unlock(&_incrementMutex); // release lock
-  return pos;
+  return mypos;
 }