Salome HOME
Merge branch 'rbe/evol-job-newparams'
[modules/kernel.git] / src / SALOMELocalTrace / LocalTraceBufferPool.cxx
index 14f07cf6a0540753ce2c7d7e09b2cec593bb8871..969e550dde82bd67c0ad4ac45d25e2c9c882710e 100644 (file)
@@ -1,36 +1,40 @@
-//  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 
-//  modify it under the terms of the GNU Lesser General Public 
-//  License as published by the Free Software Foundation; either 
-//  version 2.1 of the License. 
-// 
-//  This library is distributed in the hope that it will be useful, 
-//  but WITHOUT ANY WARRANTY; without even the implied warranty of 
-//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
-//  Lesser General Public License for more details. 
-// 
-//  You should have received a copy of the GNU Lesser General Public 
-//  License along with this library; if not, write to the Free Software 
-//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA 
-// 
+// Copyright (C) 2007-2014  CEA/DEN, EDF R&D, OPEN CASCADE
+//
+// Copyright (C) 2003-2007  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
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+// Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public
+// 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.salome-platform.org/ or email : webmaster.salome@opencascade.com
 //
+
 //  Author : Paul RASCLE (EDF)
 //  Module : KERNEL
 //  $Header$
-//
 // Cf. C++ Users Journal, June 2004, Tracing Application Execution, Tomer Abramson
 //
-
 #include <iostream>
 #include <limits.h>
 #include <cassert>
+#include <string.h>
+#include <cstdio>
 
-#ifndef WNT
+#ifndef WIN32
 #include <dlfcn.h>
 #else
+#include <windows.h>
 #endif
 
 //#define _DEVDEBUG_
 #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"
@@ -51,7 +52,7 @@ using namespace std;
 // Class static attributes initialisation
 
 LocalTraceBufferPool* LocalTraceBufferPool::_singleton = 0;
-//#ifndef WNT
+//#ifndef WIN32
 //pthread_mutex_t LocalTraceBufferPool::_singletonMutex;
 //#else
 pthread_mutex_t LocalTraceBufferPool::_singletonMutex =
@@ -82,69 +83,72 @@ LocalTraceBufferPool* LocalTraceBufferPool::instance()
       int ret;
       ret = pthread_mutex_lock(&_singletonMutex); // acquire lock to be alone
       if (_singleton == 0)                     // another thread may have got
-       {                                      // the lock after the first test
-         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");
-         assert(traceKind);
-         //cerr<<"SALOME_trace="<<traceKind<<endl;
-
-         if (strcmp(traceKind,"local")==0)
-           {
-             _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
-           {
-             void* handle;
-#ifndef WNT
-             string impl_name = string ("lib") + traceKind 
-               + string("TraceCollector.so");
-             handle = dlopen( impl_name.c_str() , RTLD_LAZY ) ;
+        {                                      // the lock after the first test
+          DEVTRACE("New buffer pool");
+          LocalTraceBufferPool* myInstance = new LocalTraceBufferPool(); 
+
+          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)
+            {
+              const 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 WIN32
+              void* handle;
+              std::string impl_name = std::string ("lib") + traceKind 
+                + std::string("TraceCollector.so");
+              handle = dlopen( impl_name.c_str() , RTLD_LAZY ) ;
 #else
-             string impl_name = string ("lib") + traceKind + string(".dll");
-             handle = dlopen( impl_name.c_str() , 0 ) ;
+              HINSTANCE handle;
+              std::string impl_name = std::string ("lib") + traceKind + std::string(".dll");
+              handle = LoadLibrary( impl_name.c_str() );
+#endif
+              if ( handle )
+                {
+                  typedef BaseTraceCollector * (*FACTORY_FUNCTION) (void);
+#ifndef WIN32
+                  FACTORY_FUNCTION TraceCollectorFactory =
+                    (FACTORY_FUNCTION) dlsym(handle, "SingletonInstance");
+#else
+                  FACTORY_FUNCTION TraceCollectorFactory =
+                    (FACTORY_FUNCTION)GetProcAddress(handle, "SingletonInstance");
+#endif
+                  if ( !TraceCollectorFactory )
+                  {
+                                         std::cerr << "Can't resolve symbol: SingletonInstance" <<std::endl;
+#ifndef WIN32
+                      std::cerr << "dlerror: " << dlerror() << std::endl;
 #endif
-             if ( handle )
-               {
-                 typedef BaseTraceCollector * (*FACTORY_FUNCTION) (void);
-                 FACTORY_FUNCTION TraceCollectorFactory =
-                   (FACTORY_FUNCTION) dlsym(handle, "SingletonInstance");
-                 char *error ;
-                 if ( (error = dlerror() ) != NULL)
-                   {
-                     cerr << "Can't resolve symbol: SingletonInstance" <<endl;
-                     cerr << "dlerror: " << error << endl;
-                     assert(error == NULL); // to give file and line
-                     exit(1);               // in case assert is deactivated
-                   }
-                 _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");
-       }
+                      exit( 1 );
+                    }
+                  _myThreadTrace = (TraceCollectorFactory) ();
+                }
+              else
+                {
+                  std::cerr << "library: " << impl_name << " not found !" << std::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
     }
   return _singleton;
@@ -183,8 +187,8 @@ int LocalTraceBufferPool::insert(int traceType, const char* msg)
   // fill the buffer with message, thread id and type (normal or abort)
 
   strncpy(_myBuffer[myInsertPos%TRACE_BUFFER_SIZE].trace,
-         msg,
-         MAXMESS_LENGTH); // last chars always "...\n\0" if msg too long
+          msg,
+          MAXMESS_LENGTH); // last chars always "...\n\0" if msg too long
   _myBuffer[myInsertPos%TRACE_BUFFER_SIZE].threadId =pthread_self();//thread id
   _myBuffer[myInsertPos%TRACE_BUFFER_SIZE].traceType = traceType;
   _myBuffer[myInsertPos%TRACE_BUFFER_SIZE].position = myMessageNumber;
@@ -218,7 +222,7 @@ int LocalTraceBufferPool::retrieve(LocalTrace_TraceInfo& aTrace)
   while (ret)
     {
       ret = sem_wait(&_fullBufferSemaphore);
-      if (ret) perror(" LocalTraceBufferPool::retrieve, sem_wait");
+      if (ret) MESSAGE (" LocalTraceBufferPool::retrieve, sem_wait");
     }
 
   // get the next buffer to print
@@ -228,8 +232,8 @@ int LocalTraceBufferPool::retrieve(LocalTrace_TraceInfo& aTrace)
   // copy the buffer from the pool to the provided buffer
 
   memcpy((void*)&aTrace,
-        (void*)&_myBuffer[myRetrievePos%TRACE_BUFFER_SIZE],
-        sizeof(aTrace));
+         (void*)&_myBuffer[myRetrievePos%TRACE_BUFFER_SIZE],
+         sizeof(aTrace));
 
   // increment the free buffer semaphore
   // (if previously 0, awake one of the threads waiting to put a trace, if any)