Salome HOME
Try to kill Salome application when a YACS Launcher job is killed
[modules/kernel.git] / src / SALOMELocalTrace / LocalTraceCollector.cxx
index 0cb334b402b9ae5e64078bfc72499911e15b22e7..f444953cf2cd1e33fbff191e7b23293cc2ca939b 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-2011  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.
 //
+// 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 <iostream>
 #include <sstream>
 #include <fstream>
 #include <cstdlib>
-#include <CORBA.h>
-
-using namespace std;
 
 #include "LocalTraceCollector.hxx"
-#include "LocalTrace_WaitForServerReadiness.hxx"
-//#include "SALOME_Log.hxx"
-#include <SALOMEconfig.h>
-#include CORBA_CLIENT_HEADER(Logger)
-
-// Class attributes initialisation, for class method LocalTraceCollector::run
-
-LocalTraceCollector* LocalTraceCollector::_singleton = 0;
-pthread_mutex_t LocalTraceCollector::_singletonMutex;
-int LocalTraceCollector::_threadToClose = 0;
-pthread_t LocalTraceCollector::_threadId = 0; // used to control single run
-int LocalTraceCollector::_toFile = 0;
-std::string LocalTraceCollector::_fileName = "";
-CORBA::ORB_ptr LocalTraceCollector::_orb = 0;
 
 // ============================================================================
 /*!
- *  guarantees a unique object instance of the class (singleton thread safe)
+ *  This class is for use without CORBA, inside or outside SALOME.
+ *  SALOME uses SALOMETraceCollector, to allow trace collection via CORBA.
+ *  Type of trace (and corresponding class) is choosen in LocalTraceBufferPool.
+ *
+ *  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
  */
 // ============================================================================
 
-LocalTraceCollector* LocalTraceCollector::instance(CORBA::ORB_ptr theOrb,
-                                                  int typeTrace)
+BaseTraceCollector* LocalTraceCollector::instance()
 {
   if (_singleton == 0) // no need of lock when singleton already exists
     {
       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
-         _singleton = new LocalTraceCollector();
-
-         _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,
-                                  LocalTraceCollector::run, (void *)bid);
-       }
+        {                                      // the lock after the first test
+          BaseTraceCollector* myInstance = new LocalTraceCollector();
+
+          sem_init(&_sem,0,0); // to wait until run thread is initialized
+          pthread_t traceThread;
+          pthread_create(&traceThread, NULL,
+                                   LocalTraceCollector::run, NULL);
+          sem_wait(&_sem);
+          _singleton = myInstance; // _singleton known only when init done
+        }
       ret = pthread_mutex_unlock(&_singletonMutex); // release lock
     }
   return _singleton;
@@ -117,131 +78,52 @@ LocalTraceCollector* LocalTraceCollector::instance(CORBA::ORB_ptr theOrb,
 
 void* LocalTraceCollector::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
-
-  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;
+  _threadId = new pthread_t;
+  *_threadId = pthread_self();
+  sem_post(&_sem); // unlock instance
 
-      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 = LocalTrace_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;
-       }
-
-      // Loop until there is no more buffer to print,
-      // and no ask for end from destructor.
+  LocalTraceBufferPool* myTraceBuffer = LocalTraceBufferPool::instance();
+  LocalTrace_TraceInfo myTrace;
 
-      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("FileTraceCollector _threadToClose");
+          //break;
+        }
+
+      myTraceBuffer->retrieve(myTrace);
+      if (myTrace.traceType == ABORT_MESS)
+        {
+          std::cout << std::flush ;
+#ifndef WIN32
+          std::cerr << "INTERRUPTION from thread " << myTrace.threadId
+               << " : " <<  myTrace.trace;
+#else
+          std::cerr << "INTERRUPTION from thread " << (void*)(&myTrace.threadId)
+               << " : " <<  myTrace.trace;
+#endif
+          std::cerr << std::flush ; 
+          exit(1);     
+        }
+      else
+        {
+          std::cout << std::flush ;
+#ifndef WIN32
+          std::cerr << "th. " << myTrace.threadId << " " << myTrace.trace;
+#else
+          std::cerr << "th. " << (void*)(&myTrace.threadId)
+               << " " << myTrace.trace;
+#endif
+          std::cerr << std::flush ; 
+        }
     }
   pthread_exit(NULL);
+  return NULL;
 }
 
 // ============================================================================
@@ -252,15 +134,26 @@ void* LocalTraceCollector::run(void *bid)
 
 LocalTraceCollector:: ~LocalTraceCollector()
 {
-  _threadToClose = 1;
-  if (_threadId)
+  int ret;
+  ret = pthread_mutex_lock(&_singletonMutex); // acquire lock to be alone
+  if (_singleton)
     {
-      int ret = pthread_join(_threadId, NULL);
-      if (ret) cout << "error close LocalTraceCollector : "<< ret << endl;
-      else cout << "LocalTraceCollector destruction OK" << endl;
+      DEVTRACE("LocalTraceCollector:: ~LocalTraceCollector()");
+      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 LocalTraceCollector : "<< ret << std::endl;
+          else DEVTRACE("LocalTraceCollector destruction OK");
+          delete _threadId;
+          _threadId = 0;
+          _threadToClose = 0;
+        }
+      _singleton = 0;
     }
-  LocalTraceBufferPool* myTraceBuffer = LocalTraceBufferPool::instance();
-  delete myTraceBuffer;
+  ret = pthread_mutex_unlock(&_singletonMutex); // release lock
 }
 
 // ============================================================================
@@ -273,6 +166,7 @@ LocalTraceCollector:: ~LocalTraceCollector()
 LocalTraceCollector::LocalTraceCollector()
 {
   _threadId=0;
+  _threadToClose = 0;
 }