Parcourir la source

Introduce ctkWorkflow verbose property to print debug msg when needed

Jean-Christophe Fillion-Robin il y a 11 ans
Parent
commit
61493b1446

+ 88 - 42
Libs/Core/ctkWorkflow.cpp

@@ -30,15 +30,10 @@
 #include "ctkWorkflowStep.h"
 #include "ctkWorkflowStep_p.h"
 #include "ctkWorkflowTransitions.h"
-#include "ctkLogger.h"
 
 // STD includes
 #include <iostream>
 
-//--------------------------------------------------------------------------
-static ctkLogger logger("org.commontk.libs.core.ctkWorkflow");
-//--------------------------------------------------------------------------
-
 // --------------------------------------------------------------------------
 // ctkWorkflowPrivate methods
 
@@ -60,6 +55,8 @@ ctkWorkflowPrivate::ctkWorkflowPrivate(ctkWorkflow& object)
   this->GoBackToOriginStepUponSuccess = true;
 
   this->ARTIFICIAL_BRANCH_ID_PREFIX = "ctkWorkflowArtificialBranchId_";
+
+  this->Verbose = false;
 }
 
 // --------------------------------------------------------------------------
@@ -84,7 +81,7 @@ bool ctkWorkflowPrivate::addStep(ctkWorkflowStep* step)
     {
     // Check if steps are not already associated with a different workflow
     QString msg("addStep - step [%1] already associated with a different workfow !");
-    logger.error(msg.arg(step->id()));
+    qWarning() << msg.arg(step->id());
     return false;
     }
   if (!this->RegisteredSteps.contains(step))
@@ -329,7 +326,10 @@ void ctkWorkflowPrivate::validateInternal(ctkWorkflowStep* step)
 {
   Q_ASSERT(step);
 
-  logger.debug(QString("validateInternal - validating input from %1").arg(step->name()));
+  if(this->Verbose)
+    {
+    qDebug() << QString("validateInternal - validating input from %1").arg(step->name());
+    }
 
   if (step->hasValidateCommand())
     {
@@ -349,7 +349,10 @@ void ctkWorkflowPrivate::onEntryInternal(
 {
   Q_ASSERT(step);
 
-  logger.debug(QString("onEntryInternal - entering input from %1").arg(step->name()));
+  if(this->Verbose)
+    {
+    qDebug() << QString("onEntryInternal - entering input from %1").arg(step->name());
+    }
 
   //Ensure we are transitioning between steps or starting the workflow
   Q_ASSERT(transitionType == ctkWorkflowInterstepTransition::TransitionToNextStep
@@ -372,12 +375,15 @@ void ctkWorkflowPrivate::processingAfterOnEntry()
 {
   Q_Q(ctkWorkflow);
 
-  logger.debug("processingAfterOnEntry");
+  if (this->Verbose)
+    {
+    qDebug() << "processingAfterOnEntry";
+    }
 
   if (!this->DestinationStep)
     {
-    logger.error("processingAfterOnEntry - Called processingAfterOnEntry without "
-                 "having set a destination step");
+    qWarning() << "processingAfterOnEntry - Called processingAfterOnEntry without "
+                  "having set a destination step";
     return;
     }
 
@@ -431,7 +437,10 @@ void ctkWorkflowPrivate::onExitInternal(
 {
   Q_ASSERT(step);
 
-  logger.debug(QString("onExitInternal - exiting %1").arg(step->name()));
+  if (this->Verbose)
+    {
+    qDebug() << QString("onExitInternal - exiting %1").arg(step->name());
+    }
 
   // Ensure we are transitioning between steps or starting the workflow
   Q_ASSERT (transitionType == ctkWorkflowInterstepTransition::TransitionToNextStep ||
@@ -577,7 +586,7 @@ bool ctkWorkflow::addTransition(ctkWorkflowStep* origin, ctkWorkflowStep* destin
 
   if (d->StateMachine->isRunning())
     {
-    logger.warn("addTransition - Cannot add a transition while the workflow is started !");
+    qWarning() << "addTransition - Cannot add a transition while the workflow is started !";
     return false;
     }
 
@@ -591,8 +600,8 @@ bool ctkWorkflow::addTransition(ctkWorkflowStep* origin, ctkWorkflowStep* destin
   // adding a transition from a step to itself
   if (origin && destination && (QString::compare(origin->id(), destination->id(), Qt::CaseInsensitive) == 0))
     {
-    logger.error("addTransition - Workflow does not currently support a transition"
-                 " from a step to itself.  Use GoToStep instead !");
+    qWarning() << "addTransition - Workflow does not currently support a transition"
+                  " from a step to itself.  Use GoToStep instead !";
     return false;
     }
 
@@ -627,8 +636,8 @@ bool ctkWorkflow::addTransition(ctkWorkflowStep* origin, ctkWorkflowStep* destin
     // ensure we haven't already added a transition with the same origin, destination and directionality
     if (this->hasTransition(origin, destination, branchId, directionality))
       {
-      logger.warn("addTransition - Cannot create a transition that matches a "
-                  "previously created transition");
+      qWarning() << "addTransition - Cannot create a transition that matches a "
+                    "previously created transition";
       return false;
       }
 
@@ -796,7 +805,7 @@ void ctkWorkflow::start()
   Q_D(ctkWorkflow);
   if (!d->InitialStep)
     {
-    logger.warn("start - Cannot start workflow without an initial step");
+    qWarning() << "start - Cannot start workflow without an initial step";
     return;
     }
 
@@ -845,7 +854,7 @@ void ctkWorkflow::goForward(const QString& desiredBranchId)
 
   if (!this->isRunning())
     {
-    logger.warn("goForward - The workflow is not running !");
+    qWarning() << "goForward - The workflow is not running !";
     return;
     }
 
@@ -855,14 +864,17 @@ void ctkWorkflow::goForward(const QString& desiredBranchId)
     {
     if (!this->canGoForward())
       {
-      logger.warn("goForward - Attempt to goForward from a finish step !");
+      qWarning() << "goForward - Attempt to goForward from a finish step !";
       return;
       }
     }
 
   d->DesiredBranchId = desiredBranchId;
 
-  logger.info("goForward - posting ValidationTransition");
+  if (d->Verbose)
+    {
+    qDebug() << "goForward - posting ValidationTransition";
+    }
   d->StateMachine->postEvent(
       new ctkWorkflowIntrastepTransitionEvent(ctkWorkflowIntrastepTransition::ValidationTransition));
 }
@@ -874,13 +886,13 @@ void ctkWorkflow::goBackward(const QString& desiredBranchId)
 
   if (!this->isRunning())
     {
-    logger.warn("goBackward - The workflow is not running !");
+    qWarning() << "goBackward - The workflow is not running !";
     return;
     }
 
   if (!this->canGoBackward())
     {
-    logger.warn("goBackward - Attempt to goBackward from first step !");
+    qWarning() << "goBackward - Attempt to goBackward from first step !";
     return;
     }
 
@@ -891,7 +903,10 @@ void ctkWorkflow::goBackward(const QString& desiredBranchId)
 
   d->DesiredBranchId = desiredBranchId;
 
-  logger.info("goBackward - posting TransitionToPreviousStep");
+  if (d->Verbose)
+    {
+    qDebug() << "goBackward - posting TransitionToPreviousStep";
+    }
   d->StateMachine->postEvent(
                              new ctkWorkflowInterstepTransitionEvent(ctkWorkflowInterstepTransition::TransitionToPreviousStep, branchId));
 }
@@ -901,20 +916,24 @@ CTK_GET_CPP(ctkWorkflow, bool, goBackToOriginStepUponSuccess, GoBackToOriginStep
 CTK_SET_CPP(ctkWorkflow, bool, setGoBackToOriginStepUponSuccess, GoBackToOriginStepUponSuccess);
 
 // --------------------------------------------------------------------------
+CTK_GET_CPP(ctkWorkflow, bool, verbose, Verbose);
+CTK_SET_CPP(ctkWorkflow, bool, setVerbose, Verbose);
+
+// --------------------------------------------------------------------------
 void ctkWorkflow::goToStep(const QString& targetId)
 {
   Q_D(ctkWorkflow);
 
   if (!this->isRunning())
     {
-    logger.warn("goToStep - The workflow is not running !");
+    qWarning() << "goToStep - The workflow is not running !";
     return;
     }
 
   // TODO currently returns true only if the workflow is running - need logic here
   if (!this->canGoToStep(targetId))
     {
-    logger.warn(QString("goToStep - Cannot goToStep %1 ").arg(targetId));
+    qWarning() << QString("goToStep - Cannot goToStep %1 ").arg(targetId);
     return;
     }
 
@@ -923,11 +942,17 @@ void ctkWorkflow::goToStep(const QString& targetId)
   Q_ASSERT(step);
 #endif
 
-  logger.info(QString("goToStep - Attempting to go to finish step %1").arg(targetId));
+  if (d->Verbose)
+    {
+    qDebug() << QString("goToStep - Attempting to go to finish step %1").arg(targetId);
+    }
 
   // if (step == d->CurrentStep)
   //   {
-  //   qDebug() << "we are already in the desired finish step";
+  //   if (d->Verbose)
+  //     {
+  //     qDebug() << "we are already in the desired finish step";
+  //     }
   //   return;
   //   }
 
@@ -939,9 +964,12 @@ void ctkWorkflow::goToStep(const QString& targetId)
 // --------------------------------------------------------------------------
 void ctkWorkflow::attemptToGoToNextStep()
 {
-  logger.info("attemptToGoToNextStep - Attempting to go to the next step ");
-
   Q_D(ctkWorkflow);
+  if (d->Verbose)
+    {
+    qDebug() << "attemptToGoToNextStep - Attempting to go to the next step ";
+    }
+
   Q_ASSERT(d->CurrentStep);
   //Q_ASSERT(this->canGoForward(d->CurrentStep));
 
@@ -975,8 +1003,11 @@ void ctkWorkflow::evaluateValidationResults(bool validationSucceeded, const QStr
 void ctkWorkflow::goToNextStepAfterSuccessfulValidation(const QString& branchId)
 {
   Q_D(ctkWorkflow);
-  logger.debug("goToNextStepAfterSuccessfulValidation - Calidation succeeded");
-  logger.info("goToNextStepAfterSuccessfulValidation - Posting TransitionToNextStep");
+  if (d->Verbose)
+    {
+    qDebug() << "goToNextStepAfterSuccessfulValidation - Validation succeeded";
+    qDebug() << "goToNextStepAfterSuccessfulValidation - Posting TransitionToNextStep";
+    }
 
   // we may already be in the 'goTo' step - i.e. looping on a finish step
   if (d->GoToStep && d->CurrentStep == d->GoToStep)
@@ -999,9 +1030,9 @@ void ctkWorkflow::goToNextStepAfterSuccessfulValidation(const QString& branchId)
     transitionBranchId = firstForwardBranchId;
     if (numberOfForwardSteps > 1)
       {
-      logger.warn("goToNextStepAfterSuccessfulValidation - ctkWorkflowStep::ValidatComplete() "
-                  "did not provide branchId at a branch in the workflow - will follow first "
-                  "transition that was created");
+      qWarning() << "goToNextStepAfterSuccessfulValidation - ctkWorkflowStep::ValidatComplete() "
+                    "did not provide branchId at a branch in the workflow - will follow first "
+                    "transition that was created";
       }
     }
   // validationComplete() gives us a branchId
@@ -1010,8 +1041,8 @@ void ctkWorkflow::goToNextStepAfterSuccessfulValidation(const QString& branchId)
     if (numberOfForwardSteps == 1 && firstForwardBranchId.contains(d->ARTIFICIAL_BRANCH_ID_PREFIX))
       {
       transitionBranchId = firstForwardBranchId;
-      logger.warn("goToNextStepAfterSuccessfulValidation -  ctkWorkflowStep::ValidationComplete()"
-                  " returns a branchId, but was overridden by the workflow");
+      qWarning() << "goToNextStepAfterSuccessfulValidation -  ctkWorkflowStep::ValidationComplete()"
+                    " returns a branchId, but was overridden by the workflow";
       }
     else
       {
@@ -1032,15 +1063,21 @@ void ctkWorkflow::goToNextStepAfterSuccessfulValidation(const QString& branchId)
 // --------------------------------------------------------------------------
 void ctkWorkflow::goToProcessingStateAfterValidationFailed()
 {
-  logger.debug("goToNextStepAfterSuccessfulValidation - Validation failed");
   Q_D(ctkWorkflow);
+  if (d->Verbose)
+    {
+    qDebug() << "goToNextStepAfterSuccessfulValidation - Validation failed";
+    }
   // Validation failed in the process of attempting to go to the finish step
   if (d->GoToStep)
     {
     this->goToStepFailed();
     }
 
-  logger.info("goToNextStepAfterSuccessfulValidation - Posting ValidationFailedTransition");
+  if (d->Verbose)
+    {
+    qDebug() << "goToNextStepAfterSuccessfulValidation - Posting ValidationFailedTransition";
+    }
   d->StateMachine->postEvent(new ctkWorkflowIntrastepTransitionEvent(ctkWorkflowIntrastepTransition::ValidationFailedTransition));
 }
 
@@ -1048,7 +1085,10 @@ void ctkWorkflow::goToProcessingStateAfterValidationFailed()
 void ctkWorkflow::performTransitionBetweenSteps()
 {
   Q_D(ctkWorkflow);
-  logger.debug("performTransitionBetweenSteps - Performing transition between steps");
+  if (d->Verbose)
+    {
+    qDebug() << "performTransitionBetweenSteps - Performing transition between steps";
+    }
 
   // Alternative: could find the origin and destination step based on
   // d->CurrentStep rather than QObject::sender(), but would require
@@ -1081,7 +1121,10 @@ void ctkWorkflow::goToStepSucceeded()
 {
   Q_D(ctkWorkflow);
 
-  logger.debug("goToStepSucceeded");
+  if (d->Verbose)
+    {
+    qDebug() << "goToStepSucceeded";
+    }
 
   // after success, go back to the step at which we begin looking for
   // the finish step (will exit the current step and enter the starting step)
@@ -1106,7 +1149,10 @@ void ctkWorkflow::goToStepSucceeded()
 void ctkWorkflow::goFromGoToStepToStartingStep()
 {
   Q_D(ctkWorkflow);
-  logger.info("goFromGoToStepToStartingStep - Posting TransitionToPreviousStartingStep");
+  if (d->Verbose)
+    {
+    qDebug() << "goFromGoToStepToStartingStep - Posting TransitionToPreviousStartingStep";
+    }
   d->StateMachine->postEvent(new ctkWorkflowInterstepTransitionEvent(ctkWorkflowInterstepTransition::TransitionToPreviousStartingStepAfterSuccessfulGoToFinishStep));
 }
 

+ 5 - 0
Libs/Core/ctkWorkflow.h

@@ -42,6 +42,7 @@ class CTK_CORE_EXPORT ctkWorkflow : public QObject
   Q_ENUMS(TransitionDirectionality)
   Q_PROPERTY(bool isRunning READ isRunning DESIGNABLE false)
   Q_PROPERTY(bool goBackToOriginStepUponSuccess READ goBackToOriginStepUponSuccess WRITE setGoBackToOriginStepUponSuccess)
+  Q_PROPERTY(bool verbose READ verbose WRITE setVerbose)
 
 public:
 
@@ -184,6 +185,10 @@ public:
   bool goBackToOriginStepUponSuccess()const;
   void setGoBackToOriginStepUponSuccess(bool flag);
 
+  /// If set debug messages will be displayed on standard output.
+  bool verbose()const;
+  void setVerbose(bool value);
+
 public Q_SLOTS:
 
   /// Use this to trigger evaluation of the processing state of the current step, and subsequent

+ 6 - 8
Libs/Core/ctkWorkflowStep.cpp

@@ -28,15 +28,10 @@
 #include "ctkWorkflowStep.h"
 #include "ctkWorkflowStep_p.h"
 #include "ctkWorkflow.h"
-#include "ctkLogger.h"
 
 // STD includes
 #include <iostream>
 
-//--------------------------------------------------------------------------
-static ctkLogger logger("org.commontk.core.ctkWorkflowStep");
-//--------------------------------------------------------------------------
-
 // --------------------------------------------------------------------------
 // ctkWorkflowStepPrivate methods
 
@@ -164,8 +159,8 @@ void ctkWorkflowStep::setId(const QString& newId)
   Q_D(ctkWorkflowStep);
   if (d->Workflow && d->Workflow->hasStep(newId) && !this->id().isEmpty())
     {
-    logger.error(QString("ctkWorkflowStep - Failed to change id from '%1' to '%2' - "
-                         "Step already added to a workflow !").arg(this->id()).arg(newId));
+    qWarning() << QString("ctkWorkflowStep - Failed to change id from '%1' to '%2' - "
+                          "Step already added to a workflow !").arg(this->id()).arg(newId);
     return;
     }
   d->Id = newId;
@@ -260,7 +255,10 @@ void ctkWorkflowStep::invokeOnExitCommand(const ctkWorkflowStep* goingTo, const
 void ctkWorkflowStep::validate(const QString& desiredBranchId)
 {
   Q_D(ctkWorkflowStep);
-  logger.info(QString("validate - validating the input from %1").arg(d->Name));
+  if (this->workflow()->verbose())
+    {
+    qDebug() << QString("validate - validating the input from %1").arg(d->Name);
+    }
 
   this->validationComplete(true, desiredBranchId);
 }

+ 1 - 0
Libs/Core/ctkWorkflow_p.h

@@ -297,6 +297,7 @@ public:
 
   QString ARTIFICIAL_BRANCH_ID_PREFIX;
 
+  bool Verbose;
 };
 
 #endif

+ 1 - 6
Libs/Widgets/ctkWorkflowAbstractPagedWidget.cpp

@@ -29,15 +29,10 @@
 #include "ctkWorkflowWidget.h"
 #include "ctkWorkflowGroupBox.h"
 #include "ctkWorkflowButtonBoxWidget.h"
-#include "ctkLogger.h"
 
 // STD includes
 #include <iostream>
 
-//--------------------------------------------------------------------------
-static ctkLogger logger("org.commontk.libs.widgets.ctkWorkflowWidget");
-//--------------------------------------------------------------------------
-
 //-----------------------------------------------------------------------------
 class ctkWorkflowAbstractPagedWidgetPrivate
 {
@@ -123,7 +118,7 @@ void ctkWorkflowAbstractPagedWidget::associateStepWithPage(ctkWorkflowStep* step
 
   if (index < 0)
     {
-    logger.error(QString("Cannot associate step with a page of index -1"));
+    qWarning() << "Cannot associate step with a page of index -1";
     return;
     }
 

+ 1 - 6
Libs/Widgets/ctkWorkflowGroupBox.cpp

@@ -30,11 +30,6 @@
 #include "ctkWorkflowWidgetStep.h"
 #include "ctkFittedTextBrowser.h"
 #include "ui_ctkWorkflowGroupBox.h"
-#include "ctkLogger.h"
-
-//--------------------------------------------------------------------------
-static ctkLogger logger("org.commontk.libs.widgets.ctkWorkflowGroupBox");
-//--------------------------------------------------------------------------
 
 //-----------------------------------------------------------------------------
 class ctkWorkflowGroupBoxPrivate: public Ui_ctkWorkflowGroupBox
@@ -137,7 +132,7 @@ void ctkWorkflowGroupBox::updateGroupBox(ctkWorkflowStep* currentStep)
   // disable/hide the previously shown step
   if (ctkWorkflowWidgetStep* prevStep = dynamic_cast<ctkWorkflowWidgetStep*>(d->StepShownPreviously))
     {
-    logger.debug(QString("updateClientArea - hiding %1").arg(prevStep->name()));
+    //qDebug() << QString("updateClientArea - hiding %1").arg(prevStep->name());
     if (QWidget* stepArea = prevStep->stepArea())
       {
       stepArea->setEnabled(false);

+ 0 - 5
Libs/Widgets/ctkWorkflowStackedWidget.cpp

@@ -26,15 +26,10 @@
 // CTK includes
 #include "ctkWorkflowStackedWidget.h"
 #include "ctkWorkflowButtonBoxWidget.h"
-#include "ctkLogger.h"
 
 // STD includes
 #include <iostream>
 
-//--------------------------------------------------------------------------
-static ctkLogger logger("org.commontk.libs.widgets.ctkWorkflowStackedWidget");
-//--------------------------------------------------------------------------
-
 //-----------------------------------------------------------------------------
 class ctkWorkflowStackedWidgetPrivate
 {

+ 0 - 5
Libs/Widgets/ctkWorkflowTabWidget.cpp

@@ -27,15 +27,10 @@
 // CTK includes
 #include "ctkWorkflowTabWidget.h"
 #include "ctkWorkflowButtonBoxWidget.h"
-#include "ctkLogger.h"
 
 // STD includes
 #include <iostream>
 
-//--------------------------------------------------------------------------
-static ctkLogger logger("org.commontk.libs.widgets.ctkWorkflowTabWidget");
-//--------------------------------------------------------------------------
-
 //-----------------------------------------------------------------------------
 class ctkWorkflowTabWidgetPrivate
 {

+ 1 - 6
Libs/Widgets/ctkWorkflowWidget.cpp

@@ -32,15 +32,10 @@
 #include "ctkWorkflow.h"
 #include "ctkWorkflowButtonBoxWidget.h"
 #include "ctkWorkflowGroupBox.h"
-#include "ctkLogger.h"
 
 // STD includes
 #include <iostream>
 
-//--------------------------------------------------------------------------
-static ctkLogger logger("org.commontk.libs.widgets.ctkWorkflowWidget");
-//--------------------------------------------------------------------------
-
 //-----------------------------------------------------------------------------
 class ctkWorkflowWidgetPrivate
 {
@@ -122,7 +117,7 @@ void ctkWorkflowWidget::setWorkflow(ctkWorkflow* newWorkflow)
 
   if (!newWorkflow)
     {
-    logger.error(QString("setWorkflow - cannot set workflow to NULL"));
+    qWarning() << "setWorkflow - cannot set workflow to NULL";
     return;
     }
 

+ 5 - 7
Libs/Widgets/ctkWorkflowWidgetStep.cpp

@@ -27,16 +27,11 @@
 #include "ctkWorkflowWidgetStep_p.h"
 #include "ctkWorkflowWidget.h"
 #include "ctkWorkflow.h"
-#include "ctkLogger.h"
 
 // STD includes
 #include <iostream>
 
 //-----------------------------------------------------------------------------
-static ctkLogger logger("org.commontk.libs.widgets.ctkWorkflowWidgetStep");
-//-----------------------------------------------------------------------------
-
-//-----------------------------------------------------------------------------
 // ctkWorkflowWidgetStepPrivate methods
 
 //-----------------------------------------------------------------------------
@@ -167,7 +162,7 @@ CTK_SET_CPP(ctkWorkflowWidgetStep, ctkWorkflowWidgetStep::ButtonBoxHints,
 //     {
 //     if (!this->workflow())
 //       {
-//       logger.error("buttonBoxWidget - Cannot create buttonBoxWidget without a workflow");
+//       qWarning() << "buttonBoxWidget - Cannot create buttonBoxWidget without a workflow";
 //       return 0;
 //       }
 //     d->buttonBoxWidget = new ctkWorkflowButtonBoxWidget(this->workflow());
@@ -188,7 +183,10 @@ void ctkWorkflowWidgetStep::showUserInterface()
     }
 
   // otherwise we provide an implementation here
-  logger.debug(QString("showUserInterface - showing %1").arg(this->name()));
+  if (this->workflow()->verbose())
+    {
+    qDebug() << QString("showUserInterface - showing %1").arg(this->name());
+    }
 
   // create the user interface if this is the first time we're showing this step
   if (!d->created)