diff --git a/Modules/Core/Common/include/otbLogger.h b/Modules/Core/Common/include/otbLogger.h index 9d7facfc01d68ad7aad48ebe68945a2c83c0b65d..4dd6825bd146d10a2fc6a37464db8d3e290e2fe2 100644 --- a/Modules/Core/Common/include/otbLogger.h +++ b/Modules/Core/Common/include/otbLogger.h @@ -69,6 +69,9 @@ public: /** Set the flag m_LogSetupInfoDone to true */ void LogSetupInformationDone(); + // Overwrite this to provide custom formatting of log entries + std::string BuildFormattedEntry(itk::Logger::PriorityLevelType, std::string const&) override; + protected: Logger(); virtual ~Logger() override = default; diff --git a/Modules/Core/Common/src/otbLogger.cxx b/Modules/Core/Common/src/otbLogger.cxx index 008101f1d924c85c1329153dfcccfd6a7c76cfa8..e6ba5c2b260eec3bb6ed968d3487ead81718a21d 100644 --- a/Modules/Core/Common/src/otbLogger.cxx +++ b/Modules/Core/Common/src/otbLogger.cxx @@ -26,6 +26,9 @@ #include "gdal.h" #include "itkMultiThreader.h" +#include <type_traits> +#include <cassert> + namespace otb { @@ -117,4 +120,21 @@ void Logger::LogSetupInformationDone() m_LogSetupInfoDone = true; } +std::string Logger::BuildFormattedEntry(itk::Logger::PriorityLevelType level, std::string const& content) +{ + static const std::string levelString[] = {"(MUSTFLUSH)", "(FATAL)", "(CRITICAL)", "(WARNING)", "(INFO)", "(DEBUG)", "(NOTSET)"}; + + assert(level <= std::extent<decltype(levelString)>::value); + + if (strcmp(this->GetName(), "") == 0) + { + return itksys::SystemTools::GetCurrentDateTime("%Y-%m-%d %H:%M:%S") + " " + levelString[level] + ": " + content; + } + else + { + return itksys::SystemTools::GetCurrentDateTime("%Y-%m-%d %H:%M:%S") + " " + levelString[level] + " " + this->GetName() + ": " + content; + } +} + + } // namespace otb diff --git a/Modules/IO/ImageIO/include/otbImageFileReader.hxx b/Modules/IO/ImageIO/include/otbImageFileReader.hxx index 770f8b791230c14cfe68f9e58c304c892e5191ff..09c7e0cae81c623443aeb33566093047a2a67c7c 100644 --- a/Modules/IO/ImageIO/include/otbImageFileReader.hxx +++ b/Modules/IO/ImageIO/include/otbImageFileReader.hxx @@ -601,11 +601,11 @@ ImageFileReader<TOutputImage, ConvertPixelTraits> // Test if the file exists. if (!itksys::SystemTools::FileExists(fileToCheck)) { - throw otb::ImageFileReaderException (__FILE__, __LINE__, "The file does not exist.", fileToCheck); + throw otb::ImageFileReaderException (__FILE__, __LINE__, std::string("Cannot open image ") + fileToCheck + std::string(". The file does not exist."), fileToCheck); } else { - throw otb::ImageFileReaderException(__FILE__, __LINE__, "Probably unsupported format or incorrect filename extension.", this->m_FileName); + throw otb::ImageFileReaderException(__FILE__, __LINE__, std::string("Cannot open image ") + this->m_FileName + std::string(". Probably unsupported format or incorrect filename extension."), this->m_FileName); } } } diff --git a/Modules/IO/TestKernel/include/otbTestMain.h b/Modules/IO/TestKernel/include/otbTestMain.h index 8b9e2e9fb6c850037d0ea9b68e39838531fb3557..40696ad53934f22e1864e316565cda2f79b7ae3e 100644 --- a/Modules/IO/TestKernel/include/otbTestMain.h +++ b/Modules/IO/TestKernel/include/otbTestMain.h @@ -300,56 +300,36 @@ int main(int ac, char* av[]) return -1; } else - { + { otb::Logger::Instance()->LogSetupInformation(); MainFuncPointer f = j->second; int result; try - { + { // Invoke the test's "main" function. result = (*f)(ac - 1, av + 1); - if (result != EXIT_SUCCESS ) - { + if (result != EXIT_SUCCESS) + { std::cout << "-> Test EXIT FAILURE (" << result << ")." << std::endl; itkGenericExceptionMacro(<< "Function returns EXIT_FAILURE (not from regression, failure inside the test)"); - } } - catch (const otb::ImageFileReaderException& e) - { - std::cerr << "otbTestMain '" << testToRun << "': ImageFileReaderException:" << std::endl; - std::cerr << e.GetFile() << ":" << e.GetLine() << ":" << std::endl; - std::cerr << std::string("Cannot open image ") + e.m_Filename + std::string(". ") + e.GetDescription() << std::endl; - result = EXIT_FAILURE; } - catch (const itk::ExceptionObject& e) - { - std::cerr << "otbTestMain '" << testToRun << "': ITK Exception thrown:" << std::endl; - std::cerr << e.GetFile() << ":" << e.GetLine() << ":" << std::endl; - std::cerr << e.GetDescription() << std::endl; - result = EXIT_FAILURE; - } - catch (const std::bad_alloc& err) - { - std::cerr << "otbTestMain '" << testToRun << "': Exception bad_alloc thrown: " << std::endl; - std::cerr << (char*) err.what() << std::endl; - result = EXIT_FAILURE; - } catch (const std::exception& e) - { - std::cerr << "otbTestMain '" << testToRun << "': std::exception thrown:" << std::endl; - std::cerr << e.what() << std::endl; + { + std::cerr << "otbTestMain '" << testToRun << "': exception caught:" << std::endl; + std::cerr << e.what() << std::endl; result = EXIT_FAILURE; - } + } catch (...) - { - std::cerr << "otbTestMain '" << testToRun << "': Unknown exception thrown !" << std::endl; + { + std::cerr << "otbTestMain '" << testToRun << "': unknown exception caught!" << std::endl; result = EXIT_FAILURE; - } + } - if (result != EXIT_SUCCESS ) - { - return -1; - } + if (result != EXIT_SUCCESS) + { + return -1; + } result = EXIT_SUCCESS; diff --git a/Modules/Wrappers/CommandLine/src/otbWrapperCommandLineLauncher.cxx b/Modules/Wrappers/CommandLine/src/otbWrapperCommandLineLauncher.cxx index 6c06362daa8a96f412c24dced81203110a335b5c..bed8cd21dcbbfd8af0cf2cd0491151a2d7aa5a0e 100644 --- a/Modules/Wrappers/CommandLine/src/otbWrapperCommandLineLauncher.cxx +++ b/Modules/Wrappers/CommandLine/src/otbWrapperCommandLineLauncher.cxx @@ -179,7 +179,7 @@ bool CommandLineLauncher::ExecuteAndWriteOutput() { 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")); + m_Application->GetLogger()->Fatal(err.GetDescription() + string("\n")); return false; } catch(itk::ExceptionObject& err) diff --git a/Modules/Wrappers/QtWidget/src/otbWrapperQtWidgetModel.cxx b/Modules/Wrappers/QtWidget/src/otbWrapperQtWidgetModel.cxx index 05a1d3f394673ee630008f45fe2abc83d3bc16fe..6803a555110064cfcb0199661c6280313618b32b 100644 --- a/Modules/Wrappers/QtWidget/src/otbWrapperQtWidgetModel.cxx +++ b/Modules/Wrappers/QtWidget/src/otbWrapperQtWidgetModel.cxx @@ -73,27 +73,26 @@ QtWidgetModel { m_Application->GetLogger()->Debug("Caught otb::ApplicationException during application update:\n"); m_Application->GetLogger()->Debug(string(err.what()) + "\n"); - emit ExceptionRaised( err.what() ); + emit ExceptionRaised(err.what()); } catch(otb::ImageFileReaderException& err) { m_Application->GetLogger()->Debug("Caught otb::ImageFileReaderException during application update:\n"); m_Application->GetLogger()->Debug(string(err.what()) + "\n"); - string message( string("Cannot open image ") + err.m_Filename + string(". ") + err.GetDescription() ); - m_Application->GetLogger()->Fatal( message + string("\n")); - emit ExceptionRaised( message.c_str() ); + m_Application->GetLogger()->Fatal(err.GetDescription() + string("\n")); + emit ExceptionRaised(err.what()); } catch(itk::ExceptionObject& err) { m_Application->GetLogger()->Debug("Caught itk::ExceptionObject during application update:\n"); m_Application->GetLogger()->Debug(string(err.what()) + "\n"); m_Application->GetLogger()->Fatal(string(err.GetDescription()) + "\n"); - emit ExceptionRaised( err.GetDescription() ); + emit ExceptionRaised(err.GetDescription()); } catch(std::exception& err) { m_Application->GetLogger()->Fatal(string("Caught std::exception during application update: ") + err.what() + "\n"); - emit ExceptionRaised( err.what() ); + emit ExceptionRaised(err.what()); } catch(...) { @@ -305,8 +304,8 @@ AppliThread { 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")); - emit ExceptionRaised( err.what() ); + m_Application->GetLogger()->Fatal(err.GetDescription() + string("\n")); + emit ExceptionRaised(err.what()); } catch(itk::ProcessAborted& /*err*/) { diff --git a/Modules/Wrappers/SWIG/src/itkBase.i b/Modules/Wrappers/SWIG/src/itkBase.i index e02857dcc7b3b17d38a4e826bf34f06f3614f22c..60e352a8a8b7578c42c321dd37a76305503c20a9 100644 --- a/Modules/Wrappers/SWIG/src/itkBase.i +++ b/Modules/Wrappers/SWIG/src/itkBase.i @@ -31,7 +31,7 @@ $action } catch(otb::ImageFileReaderException& err) { std::ostringstream oss; - oss << "Cannot open image " << err.m_Filename + ". " + err.GetDescription(); + oss << err.GetDescription(); SWIG_exception( SWIG_RuntimeError, oss.str().c_str() ); } catch( itk::ExceptionObject &ex ) { std::ostringstream oss;