diff --git a/Modules/Wrappers/CommandLine/include/otbWrapperCommandLineLauncher.h b/Modules/Wrappers/CommandLine/include/otbWrapperCommandLineLauncher.h index 120c4ea4f61b0f698df54885bb7a7f61625f8e8c..f7837c6c0e5c9423cb23b3fc923cc8f35c38c72a 100644 --- a/Modules/Wrappers/CommandLine/include/otbWrapperCommandLineLauncher.h +++ b/Modules/Wrappers/CommandLine/include/otbWrapperCommandLineLauncher.h @@ -172,6 +172,12 @@ private: CommandLineLauncher(const CommandLineLauncher &) = delete; void operator =(const CommandLineLauncher&) = delete; + /** + * Actually launch the process and write outputs, without catching exceptions. + */ + bool ExecuteAndWriteOutputNoCatch(); + + std::string m_Path; Application::Pointer m_Application; diff --git a/Modules/Wrappers/CommandLine/src/otbWrapperCommandLineLauncher.cxx b/Modules/Wrappers/CommandLine/src/otbWrapperCommandLineLauncher.cxx index e0a324191d796c8d2f8f3324664b13a6116b1379..3fb816d1341c6268a959a299b77de1bedd2c6168 100644 --- a/Modules/Wrappers/CommandLine/src/otbWrapperCommandLineLauncher.cxx +++ b/Modules/Wrappers/CommandLine/src/otbWrapperCommandLineLauncher.cxx @@ -137,68 +137,79 @@ bool CommandLineLauncher::Execute() return false; } -bool CommandLineLauncher::ExecuteAndWriteOutput() +bool CommandLineLauncher::ExecuteAndWriteOutputNoCatch() { - try - { - if (this->BeforeExecute() == false) - { + if (this->BeforeExecute() == false) + { return false; - } - + } if( m_Application->ExecuteAndWriteOutput() == 0 ) - { + { this->DisplayOutputParameters(); - } + } else - { + { return false; + } + return true; +} + +bool CommandLineLauncher::ExecuteAndWriteOutput() +{ + // If testenv is used, do not catch exceptions + if (m_Parser->IsAttributExists("-testenv", m_VExpression) == true) + { + ExecuteAndWriteOutputNoCatch(); } - } - catch(otb::ApplicationException& err) - { + else + { + try + { + ExecuteAndWriteOutputNoCatch(); + } + catch(otb::ApplicationException& err) + { // These are thrown with otbAppLogFATAL, a macro which logs a user // friendly error message before throwing. So log exception details only // in debug. m_Application->GetLogger()->Debug("Caught otb::ApplicationException during application execution:\n"); m_Application->GetLogger()->Debug(string(err.what()) + "\n"); return false; - } - catch(otb::ImageFileReaderException& err) - { + } + catch(otb::ImageFileReaderException& err) + { m_Application->GetLogger()->Debug("Caught otb::ImageFileReaderException during application execution:\n"); m_Application->GetLogger()->Debug(string(err.what()) + "\n"); m_Application->GetLogger()->Fatal(string("Cannot open image ") + err.m_Filename + string(". ") + err.GetDescription() + string("\n")); return false; - } - catch(itk::ExceptionObject& err) - { - m_Application->GetLogger()->Debug("Caught itk::ExceptionObject during application execution:\n"); - m_Application->GetLogger()->Debug(string(err.what()) + "\n"); - m_Application->GetLogger()->Fatal(string(err.GetDescription()) + "\n"); - return false; - } - catch(std::exception& err) - { - m_Application->GetLogger()->Fatal(std::string("Caught std::exception during application execution: ") + err.what() + "\n"); - return false; - } - catch(...) - { - m_Application->GetLogger()->Fatal("Caught unknown exception during application execution.\n"); - return false; - } - - return true; + } + catch(itk::ExceptionObject& err) + { + m_Application->GetLogger()->Debug("Caught itk::ExceptionObject during application execution:\n"); + m_Application->GetLogger()->Debug(string(err.what()) + "\n"); + m_Application->GetLogger()->Fatal(string(err.GetDescription()) + "\n"); + return false; + } + catch(std::exception& err) + { + m_Application->GetLogger()->Fatal(std::string("Caught std::exception during application execution: ") + err.what() + "\n"); + return false; + } + catch(...) + { + m_Application->GetLogger()->Fatal("Caught unknown exception during application execution.\n"); + return false; + } + } } bool CommandLineLauncher::BeforeExecute() { if (m_Application.IsNull()) - { + { std::cerr << "ERROR: No loaded application." << std::endl; return false; - } + } // Check if there's keys in the expression if the application takes // at least 1 mandatory parameter @@ -263,15 +274,15 @@ bool CommandLineLauncher::BeforeExecute() ParamResultType result = this->LoadParameters(); if (result == MISSINGMANDATORYPARAMETER) - { + { std::cerr << std::endl; this->DisplayHelp(); return false; - } + } else if (result != OKPARAM) - { + { return false; - } + } return true; } @@ -365,22 +376,22 @@ CommandLineLauncher::ParamResultType CommandLineLauncher::LoadParameters() // Check for the progress report parameter if (m_Parser->IsAttributExists("-progress", m_VExpression) == true) - { + { std::vector<std::string> val = m_Parser->GetAttribut("-progress", m_VExpression); if (val.size() == 1 && (val[0] == "1" || val[0] == "true")) - { + { m_ReportProgress = true; - } + } else if (val.size() == 1 && (val[0] == "0" || val[0] == "false")) - { + { m_ReportProgress = false; - } + } else - { + { std::cerr << "ERROR: Invalid value for parameter -progress. It must be 0, 1, false or true." << std::endl; return WRONGPARAMETERVALUE; + } } - } const std::vector<std::string> appKeyList = m_Application->GetParametersKeys(true); // Loop over each parameter key declared in the application @@ -421,22 +432,22 @@ CommandLineLauncher::ParamResultType CommandLineLauncher::LoadParameters() m_Application->SetParameterStringList(paramKey, values); } else if (type == ParameterType_Choice || - type == ParameterType_Float || - type == ParameterType_Int || - type == ParameterType_Radius || - type == ParameterType_Directory || - type == ParameterType_String || - type == ParameterType_InputFilename || - type == ParameterType_OutputFilename || - type == ParameterType_ComplexInputImage || - type == ParameterType_InputImage || - type == ParameterType_OutputImage || - type == ParameterType_ComplexOutputImage || - type == ParameterType_InputVectorData || - type == ParameterType_OutputVectorData || - type == ParameterType_RAM || - type == ParameterType_OutputProcessXML || - type == ParameterType_Bool) // || type == ParameterType_InputProcessXML) + type == ParameterType_Float || + type == ParameterType_Int || + type == ParameterType_Radius || + type == ParameterType_Directory || + type == ParameterType_String || + type == ParameterType_InputFilename || + type == ParameterType_OutputFilename || + type == ParameterType_ComplexInputImage || + type == ParameterType_InputImage || + type == ParameterType_OutputImage || + type == ParameterType_ComplexOutputImage || + type == ParameterType_InputVectorData || + type == ParameterType_OutputVectorData || + type == ParameterType_RAM || + type == ParameterType_OutputProcessXML || + type == ParameterType_Bool) // || type == ParameterType_InputProcessXML) { // Single value parameter m_Application->SetParameterString(paramKey, values[0]); @@ -459,7 +470,7 @@ CommandLineLauncher::ParamResultType CommandLineLauncher::LoadParameters() { std::cerr << "ERROR: Too many values for parameter -" << paramKey << " (expected 2 or 1, got " << values.size() << ")." - << std::endl; + << std::endl; return INVALIDNUMBEROFVALUE; } } @@ -756,11 +767,11 @@ std::string CommandLineLauncher::DisplayParameterHelp(const Parameter::Pointer & oss << "["; for(unsigned int i=0; i<keys.size(); i++) - { + { oss<<keys[i]; if( i != keys.size()-1 ) oss << "/"; - } + } oss << "]"; }