Salome HOME
Fix compilation pb.
[modules/kernel.git] / src / SALOMELocalTrace / LocalTraceCollector.cxx
index 37c2557ece6f470c2083c61bdfb2f34c995e80a4..6cbaed7b557eddbfb97b7fcb43ee6e4aad914589 100644 (file)
@@ -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
 //
 //
 //
@@ -33,28 +33,18 @@ using namespace std;
 
 #include "LocalTraceCollector.hxx"
 
-// 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 = "";
-
 // ============================================================================
 /*!
- *  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)
- *  If typeTrace=0, checks environment for "SALOME_trace". Test values in
- *  the following order:
- *  - "local"  standard out
- *  - anything else is kept as a file name
  */
 // ============================================================================
 
-LocalTraceCollector* LocalTraceCollector::instance(int typeTrace)
+BaseTraceCollector* LocalTraceCollector::instance()
 {
   if (_singleton == 0) // no need of lock when singleton already exists
     {
@@ -62,34 +52,14 @@ LocalTraceCollector* LocalTraceCollector::instance(int typeTrace)
       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;
-
-         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
-                   {
-                     _toFile=1;
-                     _fileName = traceKind;
-                   }       
-               }
-           }
-         //cout <<"_toFile: "<<_toFile<<" _fileName: "<<_fileName<<endl;
+         BaseTraceCollector* myInstance = new LocalTraceCollector();
 
+         sem_init(&_sem,0,0); // to wait until run thread is initialized
          pthread_t traceThread;
-         int bid;
          int re2 = pthread_create(&traceThread, NULL,
-                                  LocalTraceCollector::run, (void *)bid);
+                                  LocalTraceCollector::run, NULL);
+         sem_wait(&_sem);
+         _singleton = myInstance; // _singleton known only when init done
        }
       ret = pthread_mutex_unlock(&_singletonMutex); // release lock
     }
@@ -109,91 +79,52 @@ LocalTraceCollector* LocalTraceCollector::instance(int typeTrace)
 
 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
+  _threadId = new pthread_t;
+  *_threadId = pthread_self();
+  sem_post(&_sem); // unlock instance
 
+  LocalTraceBufferPool* myTraceBuffer = LocalTraceBufferPool::instance();
+  LocalTrace_TraceInfo myTrace;
 
-      ofstream traceFile;
+  // --- Loop until there is no more buffer to print,
+  //     and no ask for end from destructor.
 
-      switch (_toFile)
+  while ((!_threadToClose) || myTraceBuffer->toCollect() )
+    {
+      if (_threadToClose)
        {
-       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 0 : ; // --- trace to standard output
-       default :  // --- on standard output, too
-         break;
+         DEVTRACE("FileTraceCollector _threadToClose");
+         //break;
        }
 
-      // Loop until there is no more buffer to print,
-      // and no ask for end from destructor.
-
-      while ((!_threadToClose) || myTraceBuffer->toCollect() )
+      int fullBuf = myTraceBuffer->retrieve(myTrace);
+      if (myTrace.traceType == ABORT_MESS)
        {
-         int fullBuf = myTraceBuffer->retrieve(myTrace);
-         if (myTrace.traceType == ABORT_MESS)
-           {
-             switch (_toFile)
-               {
-               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 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;
-               }
-           }
+         cout << flush ;
+#ifndef WNT
+         cerr << "INTERRUPTION from thread " << myTrace.threadId
+              << " : " <<  myTrace.trace;
+#else
+         cerr << "INTERRUPTION from thread " << (void*)(&myTrace.threadId)
+              << " : " <<  myTrace.trace;
+#endif
+         cerr << flush ; 
+         exit(1);     
+       }
+      else
+       {
+         cout << flush ;
+#ifndef WNT
+         cerr << "th. " << myTrace.threadId << " " << myTrace.trace;
+#else
+         cerr << "th. " << (void*)(&myTrace.threadId)
+              << " " << myTrace.trace;
+#endif
+         cerr << flush ; 
        }
-
-      if (_toFile==1) traceFile.close();
     }
   pthread_exit(NULL);
+  return NULL;
 }
 
 // ============================================================================
@@ -204,16 +135,25 @@ void* LocalTraceCollector::run(void *bid)
 
 LocalTraceCollector:: ~LocalTraceCollector()
 {
-  LocalTraceBufferPool* myTraceBuffer = LocalTraceBufferPool::instance();
-  _threadToClose = 1;
-  myTraceBuffer->insert(NORMAL_MESS,"end of trace "); //needed to wake up thread
-  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) cerr << "error close LocalTraceCollector : "<< ret << endl;
+         else DEVTRACE("LocalTraceCollector destruction OK");
+         _threadId = 0;
+         _threadToClose = 0;
+       }
+      _singleton = 0;
     }
-  delete myTraceBuffer;
+  ret = pthread_mutex_unlock(&_singletonMutex); // release lock
 }
 
 // ============================================================================
@@ -226,6 +166,7 @@ LocalTraceCollector:: ~LocalTraceCollector()
 LocalTraceCollector::LocalTraceCollector()
 {
   _threadId=0;
+  _threadToClose = 0;
 }