Explorar o código

Merge branch 'fix-ctkErrorLogFDMessageHandler-to-support-enable-disable-cycle'

* fix-ctkErrorLogFDMessageHandler-to-support-enable-disable-cycle:
  Ensure ctkFDHandler can be enabled/disabled multiple times
  Remove unused code from ctkErrorLogFDMessageHandler
Jean-Christophe Fillion-Robin %!s(int64=12) %!d(string=hai) anos
pai
achega
8d7db25d65

+ 22 - 1
Libs/Core/Testing/Cpp/ctkErrorLogModelTest1.cpp

@@ -118,6 +118,13 @@ int ctkErrorLogModelTest1(int argc, char * argv [])
         printTextMessages(model);
         return EXIT_FAILURE;
         }
+        
+      // Check if handler can be enabled / disabled multiple times in a row
+      for (int idx = 0; idx < 3; ++idx)
+        {
+        model.setMsgHandlerEnabled(ctkErrorLogQtMessageHandler::HandlerName, false);
+        model.setMsgHandlerEnabled(ctkErrorLogQtMessageHandler::HandlerName, true);
+        }
 
       // Clear
       model.clear();
@@ -193,7 +200,14 @@ int ctkErrorLogModelTest1(int argc, char * argv [])
         printTextMessages(model);
         return EXIT_FAILURE;
         }
-
+      
+      // Check if handler can be enabled / disabled multiple times in a row
+      for (int idx = 0; idx < 3; ++idx)
+        {
+        model.setMsgHandlerEnabled(ctkErrorLogStreamMessageHandler::HandlerName, false);
+        model.setMsgHandlerEnabled(ctkErrorLogStreamMessageHandler::HandlerName, true);
+        }
+        
       // Clear
       model.clear();
 
@@ -276,6 +290,13 @@ int ctkErrorLogModelTest1(int argc, char * argv [])
         return EXIT_FAILURE;
         }
 
+      // Check if handler can be enabled / disabled multiple times in a row
+      for (int idx = 0; idx < 3; ++idx)
+        {
+        model.setMsgHandlerEnabled(ctkErrorLogFDMessageHandler::HandlerName, false);
+        model.setMsgHandlerEnabled(ctkErrorLogFDMessageHandler::HandlerName, true);
+        }
+
       // Clear
       model.clear();
 

+ 15 - 20
Libs/Core/ctkErrorLogFDMessageHandler.cpp

@@ -20,6 +20,7 @@
 
 // Qt includes
 #include <QDebug>
+#include <QFile>
 
 // CTK includes
 #include "ctkErrorLogFDMessageHandler.h"
@@ -50,22 +51,15 @@ ctkFDHandler::ctkFDHandler(ctkErrorLogFDMessageHandler* messageHandler,
   this->TerminalOutput = terminalOutput;
   this->SavedFDNumber = 0;
   this->Enabled = false;
-  this->Initialized = false;
-  this->init();
 }
 
 // --------------------------------------------------------------------------
 ctkFDHandler::~ctkFDHandler()
 {
-  if (this->Initialized)
-    {
-    this->RedirectionFile.close();
-    delete this->RedirectionStream;
-    }
 }
 
 // --------------------------------------------------------------------------
-void ctkFDHandler::init()
+void ctkFDHandler::setupPipe()
 {
 #ifdef Q_OS_WIN32
   int status = _pipe(this->Pipe, 65536, _O_TEXT);
@@ -77,9 +71,6 @@ void ctkFDHandler::init()
     qCritical().nospace() << "ctkFDHandler - Failed to create pipe !";
     return;
     }
-  this->RedirectionFile.open(this->Pipe[0], QIODevice::ReadOnly);
-  this->RedirectionStream = new QTextStream(&this->RedirectionFile);
-  this->Initialized = true;
 }
 
 // --------------------------------------------------------------------------
@@ -91,10 +82,6 @@ FILE* ctkFDHandler::terminalOutputFile()
 // --------------------------------------------------------------------------
 void ctkFDHandler::setEnabled(bool value)
 {
-  if (!this->Initialized)
-    {
-    return;
-    }
   if (this->Enabled == value)
     {
     return;
@@ -102,6 +89,8 @@ void ctkFDHandler::setEnabled(bool value)
 
   if (value)
     {
+    this->setupPipe();
+
     // Flush (stdout|stderr) so that any buffered messages are delivered
     fflush(this->terminalOutputFile());
 
@@ -123,12 +112,15 @@ void ctkFDHandler::setEnabled(bool value)
     }
   else
     {
+    // Print one character to "unblock" the read function associated with the polling thread
+    ssize_t res = write(fileno(this->terminalOutputFile()), "\n", 1);
+    if (res == -1)
+      {
+      return;
+      }
     // Flush stdout or stderr so that any buffered messages are delivered
     fflush(this->terminalOutputFile());
 
-    // Flush current stream so that any buffered messages are delivered
-    this->RedirectionFile.flush();
-
     // Stop polling thread
     {
       QMutexLocker locker(&this->EnableMutex);
@@ -156,11 +148,14 @@ void ctkFDHandler::setEnabled(bool value)
     clearerr(this->terminalOutputFile());
     fsetpos(this->terminalOutputFile(), &this->SavedFDPos);
 
+
 #ifdef Q_OS_WIN32
-    this->SavedFDNumber = _fileno(this->terminalOutputFile());
+    _close(this->Pipe[0]);
 #else
-    this->SavedFDNumber = fileno(this->terminalOutputFile());
+    close(this->Pipe[0]);
 #endif
+
+    this->SavedFDNumber = 0;
     }
 
   ctkErrorLogTerminalOutput * terminalOutput =

+ 1 - 5
Libs/Core/ctkErrorLogFDMessageHandler_p.h

@@ -22,7 +22,6 @@
 #define __ctkErrorLogFDMessageHandler_p_h
 
 // Qt includes
-#include <QFile>
 #include <QMutex>
 #include <QThread>
 
@@ -33,7 +32,6 @@
 #include <cstdio>
 
 class ctkErrorLogFDMessageHandler;
-class QTextStream;
 
 // --------------------------------------------------------------------------
 // ctkFDHandler
@@ -61,7 +59,7 @@ public:
   FILE* terminalOutputFile();
 
 protected:
-  void init();
+  void setupPipe();
 
   void run();
 
@@ -75,8 +73,6 @@ private:
   fpos_t SavedFDPos;
 
   int          Pipe[2]; // 0: Read, 1: Write
-  QFile        RedirectionFile;
-  QTextStream* RedirectionStream;
 
   bool Initialized;
 

+ 7 - 0
Libs/ImageProcessing/ITK/Core/Testing/Cpp/ctkITKErrorLogModelTest1.cpp

@@ -106,6 +106,13 @@ int ctkITKErrorLogModelTest1(int argc, char * argv [])
       return EXIT_FAILURE;
       }
 
+    // Check if handler can be enabled / disabled multiple times in a row
+    for (int idx = 0; idx < 3; ++idx)
+      {
+      model.setMsgHandlerEnabled(ctkITKErrorLogMessageHandler::HandlerName, false);
+      model.setMsgHandlerEnabled(ctkITKErrorLogMessageHandler::HandlerName, true);
+      }
+
     // Clear
     model.clear();
 

+ 7 - 0
Libs/Visualization/VTK/Core/Testing/Cpp/ctkVTKErrorLogModelTest1.cpp

@@ -97,6 +97,13 @@ int ctkVTKErrorLogModelTest1(int argc, char * argv [])
       return EXIT_FAILURE;
       }
 
+    // Check if handler can be enabled / disabled multiple times in a row
+    for (int idx = 0; idx < 3; ++idx)
+      {
+      model.setMsgHandlerEnabled(ctkVTKErrorLogMessageHandler::HandlerName, false);
+      model.setMsgHandlerEnabled(ctkVTKErrorLogMessageHandler::HandlerName, true);
+      }
+
     // Clear
     model.clear();