Salome HOME
Management of :
[modules/kernel.git] / src / SALOMETraceCollector / SALOMETraceCollector.cxx
index 1c070984869bc95190154f320a421a54873c7166..82fdc3e67e9d56445eadec25868b6a2bf074f560 100644 (file)
-//  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 
-// 
-//  See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
+// Copyright (C) 2007-2021  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
+//
+
 //  File   : LocalTraceCollector.cxx
 //  Author : Paul RASCLE (EDF)
 //  Module : KERNEL
 //  $Header$
+//
+#include <SALOMEconfig.h>
 
 #include <iostream>
 #include <sstream>
 #include <fstream>
 #include <cstdlib>
-#include <CORBA.h>
-
-using namespace std;
+#include <omniORB4/CORBA.h>
 
+#include "Utils_SALOME_Exception.hxx"
 #include "SALOMETraceCollector.hxx"
 #include "TraceCollector_WaitForServerReadiness.hxx"
-//#include "SALOME_Log.hxx"
 #include <SALOMEconfig.h>
 #include CORBA_CLIENT_HEADER(Logger)
 
-// Class attributes initialisation, for class method SALOMETraceCollector::run
-
-SALOMETraceCollector* SALOMETraceCollector::_singleton = 0;
-pthread_mutex_t SALOMETraceCollector::_singletonMutex;
-int SALOMETraceCollector::_threadToClose = 0;
-pthread_t SALOMETraceCollector::_threadId = 0; // used to control single run
-int SALOMETraceCollector::_toFile = 0;
-std::string SALOMETraceCollector::_fileName = "";
-CORBA::ORB_ptr SALOMETraceCollector::_orb = 0;
-
 // ============================================================================
 /*!
- *  This class replaces LocalTraceCollector, which is to use outside SALOME,
- *  without CORBA.
+ *  This class is for use with CORBA, inside SALOME.
+ *  Type of trace (and corresponding class) is chosen in LocalTraceBufferPool.
  *
- *  guarantees a unique object instance of the class (singleton thread safe)
+ *  Guarantees a unique object instance of the class (singleton thread safe)
  *  a separate thread for loop to print traces is launched.
- *  \param typeTrace 0=standard out, 1=file(/tmp/tracetest.log), 2=CORBA log
- *  If typeTrace=0, checks environment for "SALOME_trace". Test values in
- *  the following order:
- *  - "local"  standard out
- *  - "with_logger" CORBA log
- *  - anything else is kept as a file name
  */
 // ============================================================================
 
-SALOMETraceCollector* SALOMETraceCollector::instance(CORBA::ORB_ptr theOrb,
-                                                  int typeTrace)
+BaseTraceCollector* SALOMETraceCollector::instance()
 {
   if (_singleton == 0) // no need of lock when singleton already exists
     {
-      int ret;
-      ret = pthread_mutex_lock(&_singletonMutex); // acquire lock to be alone
+      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 SALOMETraceCollector();
-
-         _fileName = "/tmp/tracetest.log";
-         _toFile=0;
-         _orb = theOrb;
-         if (typeTrace)       // caller sets a value different from default=0
-           _toFile = typeTrace; 
-         else                 // check environment
-           {
-             char* traceKind = getenv("SALOME_trace");
-             //cout<<"SALOME_trace="<<traceKind<<endl;
-             if (traceKind)
-               {
-                 if (strcmp(traceKind,"local")==0) _toFile=0;
-                 else if (strcmp(traceKind,"with_logger")==0) _toFile=2;
-                 else
-                   {
-                     _toFile=1;
-                     _fileName = traceKind;
-                   }       
-               }
-           }
-         //cout <<"_toFile: "<<_toFile<<" _fileName: "<<_fileName<<endl;
-
-         pthread_t traceThread;
-         int bid;
-         int re2 = pthread_create(&traceThread, NULL,
-                                  SALOMETraceCollector::run, (void *)bid);
-       }
-      ret = pthread_mutex_unlock(&_singletonMutex); // release lock
+        {                                      // the lock after the first test
+          BaseTraceCollector* myInstance = new SALOMETraceCollector();
+
+          sem_init(&_sem,0,0); // to wait until run thread is initialized
+          pthread_t traceThread;
+          int bid = 0;
+          pthread_create(&traceThread, NULL,
+                                   SALOMETraceCollector::run, &bid);
+          sem_wait(&_sem);
+          _singleton = myInstance; // _singleton known only when init done
+        }
+      pthread_mutex_unlock(&_singletonMutex); // release lock
     }
   return _singleton;
 }
@@ -110,7 +74,7 @@ SALOMETraceCollector* SALOMETraceCollector::instance(CORBA::ORB_ptr theOrb,
 // ============================================================================
 /*!
  *  In a separate thread, loop to print traces.
- *  Mutex garantees intialisation on instance method is done and only one run
+ *  Mutex guarantees initialisation on instance method is done and only one run
  *  allowed (double check ...)
  *  Loop until there is no more buffer to print,
  *  and no ask for end from destructor.
@@ -118,133 +82,83 @@ SALOMETraceCollector* SALOMETraceCollector::instance(CORBA::ORB_ptr theOrb,
  */
 // ============================================================================
 
-void* SALOMETraceCollector::run(void *bid)
+void* SALOMETraceCollector::run(void* /*bid*/)
 {
-  int isOKtoRun = 0;
-  int ret = pthread_mutex_lock(&_singletonMutex); // acquire lock to be alone
-  if (! _threadId)  // only one run
-    {
-      isOKtoRun = 1;
-      _threadId = pthread_self();
-    }
-  else cout << "----- Comment est-ce possible de passer la ? -------" <<endl;
-  ret = pthread_mutex_unlock(&_singletonMutex); // release lock
+  _threadId = new pthread_t;
+  *_threadId = pthread_self();
+  sem_post(&_sem); // unlock instance
 
-  if (isOKtoRun)
-    { 
-      _threadId = pthread_self();
-      LocalTraceBufferPool* myTraceBuffer = LocalTraceBufferPool::instance();
-      LocalTrace_TraceInfo myTrace;
-
-      // if trace in file requested, opens a file with append mode
-      // so, several processes can share the same file
-      // if CORBA collection requested, wait for Logger server readiness
-
-      ofstream traceFile;
-      SALOME_Logger::Logger_var m_pInterfaceLogger;
-      CORBA::Object_var obj;
+  LocalTraceBufferPool* myTraceBuffer = LocalTraceBufferPool::instance();
+  LocalTrace_TraceInfo myTrace;
 
-      switch (_toFile)
-       {
-       case 1 :  // --- trace to file
-         {
-           const char *fileName = _fileName.c_str();
-           traceFile.open(fileName, ios::out | ios::app);
-           if (!traceFile)
-             {
-               cerr << "impossible to open trace file "<< fileName << endl;
-               exit (1);
-             }
-         }
-         break;
-       case 2 :  // --- trace collection via CORBA
-         obj = TraceCollector_WaitForServerReadiness(_orb,"Logger");
-         if (!CORBA::is_nil(obj))
-           m_pInterfaceLogger = SALOME_Logger::Logger::_narrow(obj);
-         if (CORBA::is_nil(m_pInterfaceLogger))
-           {
-             cerr << "Logger server not found ! Abort" << endl;
-             cerr << flush ; 
-             exit(1);
-           } 
-         else
-           {
-             CORBA::String_var LogMsg =
-               CORBA::string_dup("\n---Init logger trace---\n");
-             m_pInterfaceLogger->putMessage(LogMsg);
-             //cout << " Logger server found" << endl;
-           }
-         break;
-       case 0 : ; // --- trace to standard output
-       default :  // --- on standard output, too
-         break;
-       }
+  SALOME_Logger::Logger_var m_pInterfaceLogger;
+  CORBA::Object_var obj;
 
-      // Loop until there is no more buffer to print,
-      // and no ask for end from destructor.
+  obj = TraceCollector_WaitForServerReadiness("Logger");
+  if (!CORBA::is_nil(obj))
+    m_pInterfaceLogger = SALOME_Logger::Logger::_narrow(obj);
+  if (CORBA::is_nil(m_pInterfaceLogger))
+    {
+      std::cerr << "Logger server not found ! Abort" << std::endl;
+      std::cerr << std::flush ; 
+      exit(1);
+    } 
+  else
+    {
+      CORBA::String_var LogMsg =
+        CORBA::string_dup("\n---Init logger trace---\n");
+      m_pInterfaceLogger->putMessage(LogMsg);
+      DEVTRACE("Logger server found");
+    }
 
-      while ((!_threadToClose) || myTraceBuffer->toCollect() )
-       {
-         int fullBuf = myTraceBuffer->retrieve(myTrace);
-         if (myTrace.traceType == ABORT_MESS)
-           {
-             switch (_toFile)
-               {
-               case 2 :  // --- trace collection via CORBA
-                 {
-                   stringstream abortMessage("");
-                   abortMessage << "INTERRUPTION from thread "
-                                << myTrace.threadId << " : " << myTrace.trace;
-                   CORBA::String_var LogMsg =
-                     CORBA::string_dup(abortMessage.str().c_str());
-                   m_pInterfaceLogger->putMessage(LogMsg);
-                   exit(1);
-                 }
-                 break;
-               case 1 :  // --- trace to file
-                 traceFile << "INTERRUPTION from thread " << myTrace.threadId
-                           << " : " <<  myTrace.trace;
-                 traceFile.close();
-                 // no break here !
-               case 0 :  // --- trace to standard output
-               default : // --- on standard output, too
-                 cout << flush ;
-                 cerr << "INTERRUPTION from thread " << myTrace.threadId
-                      << " : " <<  myTrace.trace;
-                 cerr << flush ; 
-                 exit(1);     
-                 break;
-               }
-           }
-         else
-           {
-             switch (_toFile)
-               {
-               case 2 :  // --- trace collection via CORBA
-                 {
-                   stringstream aMessage("");
-                   aMessage << "th. " << myTrace.threadId
-                            << " " << myTrace.trace;
-                   CORBA::String_var LogMsg =
-                     CORBA::string_dup(aMessage.str().c_str());
-                   m_pInterfaceLogger->putMessage(LogMsg);
-                 }
-                 break;
-               case 1 :  // --- trace to file
-                 traceFile << "th. " << myTrace.threadId
-                           << " " << myTrace.trace;
-                 break;
-               case 0 :  // --- trace to standard output
-               default : // --- on standard output, too
-                 cout << "th. " << myTrace.threadId << " " << myTrace.trace;
-                 break;
-               }
-           }
-       }
+  // --- Loop until there is no more buffer to print,
+  //     and no ask for end from destructor.
 
-      if (_toFile==1) traceFile.close();
+  while ((!_threadToClose) || myTraceBuffer->toCollect() )
+    {
+      if (_threadToClose)
+        {
+          DEVTRACE("SALOMETraceCollector _threadToClose");
+          //break;
+        }
+
+      myTraceBuffer->retrieve(myTrace);
+      //if (!CORBA::is_nil(_orb))
+      if (true)
+        {
+          if (myTrace.traceType == ABORT_MESS)
+            {
+              std::stringstream abortMessage("");
+#ifndef WIN32
+              abortMessage << "INTERRUPTION from thread "
+                           << myTrace.threadId << " : " << myTrace.trace;
+#else
+              abortMessage << "INTERRUPTION from thread "
+                           << (void*)&myTrace.threadId 
+                           << " : " << myTrace.trace;
+#endif
+              CORBA::String_var LogMsg =
+                CORBA::string_dup(abortMessage.str().c_str());
+              m_pInterfaceLogger->putMessage(LogMsg);
+              exit(1);
+            }
+          else
+            {
+              std::stringstream aMessage("");
+#ifndef WIN32
+              aMessage << "th. " << myTrace.threadId
+#else
+                aMessage << "th. " << (void*)&myTrace.threadId
+#endif
+                       << " " << myTrace.trace;
+              CORBA::String_var LogMsg =
+                CORBA::string_dup(aMessage.str().c_str());
+              m_pInterfaceLogger->putMessage(LogMsg);
+            }
+        }
     }
   pthread_exit(NULL);
+  return NULL;
 }
 
 // ============================================================================
@@ -255,16 +169,25 @@ void* SALOMETraceCollector::run(void *bid)
 
 SALOMETraceCollector:: ~SALOMETraceCollector()
 {
-  LocalTraceBufferPool* myTraceBuffer = LocalTraceBufferPool::instance();
-  _threadToClose = 1;
-  myTraceBuffer->insert(NORMAL_MESS,"end of trace "); //needed to wake up thread
-  if (_threadId)
+  pthread_mutex_lock(&_singletonMutex); // acquire lock to be alone
+  if (_singleton)
     {
-      int ret = pthread_join(_threadId, NULL);
-      if (ret) cout << "error close SALOMETraceCollector : "<< ret << endl;
-      else cout << "SALOMETraceCollector destruction OK" << endl;
+      DEVTRACE("SALOMETraceCollector:: ~SALOMETraceCollector()");
+      LocalTraceBufferPool* myTraceBuffer = LocalTraceBufferPool::instance();
+      _threadToClose = 1;
+      myTraceBuffer->insert(NORMAL_MESS,"end of trace\n"); // to wake up thread
+      if (_threadId)
+        {
+          int ret = pthread_join(*_threadId, NULL);
+          if (ret) { std::cerr << "error close SALOMETraceCollector : "<< ret << std::endl; }
+          else { DEVTRACE("SALOMETraceCollector destruction OK") };
+          delete _threadId;
+          _threadId = 0;
+          _threadToClose = 0;
+        }
+      _singleton = 0;
     }
-  delete myTraceBuffer;
+  pthread_mutex_unlock(&_singletonMutex); // release lock
 }
 
 // ============================================================================
@@ -277,6 +200,33 @@ SALOMETraceCollector:: ~SALOMETraceCollector()
 SALOMETraceCollector::SALOMETraceCollector()
 {
   _threadId=0;
+  _threadToClose = 0;
 }
 
+// ============================================================================
+/*!
+ * 
+ */
+// ============================================================================
+
+#include "KernelBasis.hxx"
+#include "SALOME_Logger_Server.hxx"
+#include "SALOME_Fake_NamingService.hxx"
+
+#include <memory>
 
+extern "C"
+{
+ SALOMETRACECOLLECTOR_EXPORT
+  BaseTraceCollector *SingletonInstance(void)
+  {
+    if(getSSLMode())
+    {
+      SALOME_Logger::Logger_var logger = KERNEL::getLoggerServantSA();
+      std::unique_ptr<SALOME_Fake_NamingService> ns(new SALOME_Fake_NamingService);
+      ns->Register(logger,"/Logger");
+    }
+    BaseTraceCollector *instance = SALOMETraceCollector::instance();
+    return instance;
+  }
+}