Commit 7a8866cb authored by Antoine Regimbeau's avatar Antoine Regimbeau
Browse files

Merge branch 'logger_bug' into 'develop'

Logger bug

Closes #1839

See merge request orfeotoolbox/otb!379
parents e062106e ea56ec26
......@@ -54,12 +54,11 @@ public:
*
* See https://stackoverflow.com/questions/335369/finding-c-static-initialization-order-problems#335746
*/
static Pointer Instance();
static Logger * Instance();
itkNewMacro(Self);
// Overwrite this to provide custom formatting of log entries
std::string BuildFormattedEntry(itk::Logger::PriorityLevelType, std::string const&) override;
static Pointer New();
itkCreateAnotherMacro( Logger )
itkCloneMacro( Logger )
/** Output logs about the RAM, caching and multi-threading settings */
void LogSetupInformation();
......@@ -72,13 +71,14 @@ public:
protected:
Logger();
virtual ~Logger() override;
virtual ~Logger() override = default;
private:
Logger(const Self &) = delete;
void operator =(const Self&) = delete;
static Pointer CreateInstance();
static Logger * CreateInstance();
bool m_LogSetupInfoDone;
......
......@@ -71,28 +71,36 @@ itk::LoggerBase::PriorityLevelType ConfigurationManager::GetLoggerLevel()
// Default value is INFO
itk::LoggerBase::PriorityLevelType level = itk::LoggerBase::INFO;
if(itksys::SystemTools::GetEnv("OTB_LOGGER_LEVEL",svalue))
itksys::SystemTools::GetEnv("OTB_LOGGER_LEVEL",svalue);
// on windows a variable set with set EX="" will keep the "". We need
// to remove them.
if( !svalue.empty() )
{
if(svalue.compare("DEBUG") == 0)
auto quot_pos = svalue.find_first_not_of("\"") ;
svalue = svalue.substr( quot_pos );
quot_pos = svalue.find_last_not_of("\"") ;
svalue = svalue.substr( 0 , quot_pos + 1 );
if( svalue == "DEBUG" )
{
level = itk::LoggerBase::DEBUG;
}
else if(svalue.compare("INFO") == 0)
else if( svalue == "INFO" )
{
level = itk::LoggerBase::INFO;
}
else if(svalue.compare("WARNING") == 0)
else if( svalue == "WARNING" )
{
level = itk::LoggerBase::WARNING;
}
else if(svalue.compare("CRITICAL") == 0)
else if( svalue == "CRITICAL" )
{
level = itk::LoggerBase::CRITICAL;
}
else
{
otbLogMacro(Error,<<"Unknown value for OTB_LOGGER_LEVEL_MACRO. Possible values are DEBUG, INFO, WARNING, CRITICAL.");
otbLogMacro(Warning,<<"Unknown value for OTB_LOGGER_LEVEL_MACRO (set to: "
<<svalue<<"). Possible values are DEBUG, INFO, WARNING, CRITICAL. "
<<"Level set to INFO.");
}
}
return level;
......
......@@ -29,41 +29,50 @@
namespace otb
{
Logger::Pointer Logger::CreateInstance()
namespace
{
Logger::Pointer instance = Logger::New();
// By default, redirect logs to std::cout
itk::StdStreamLogOutput::Pointer defaultOutput = itk::StdStreamLogOutput::New();
defaultOutput->SetStream(std::cout);
instance->AddLogOutput(defaultOutput);
return instance;
static bool is_logger_singleton_level_set;
}
Logger::Pointer Logger::Instance()
Logger::Logger() :
m_LogSetupInfoDone(false)
{
// Static locales are initialized once in a thread-safe way
static Logger::Pointer instance = CreateInstance();
return instance;
m_LevelForFlushing = itk::LoggerBase::CRITICAL;
m_TimeStampFormat = itk::LoggerBase::HUMANREADABLE;
m_HumanReadableFormat = "%Y-%m-%d %H:%M:%S";
}
Logger::Logger()
Logger * Logger::CreateInstance()
{
this->SetPriorityLevel(otb::ConfigurationManager::GetLoggerLevel());
Logger * logger = new Logger;
this->SetLevelForFlushing(itk::LoggerBase::CRITICAL);
// By default, redirect logs to std::cout
itk::StdStreamLogOutput::Pointer defaultOutput =
itk::StdStreamLogOutput::New();
defaultOutput->SetStream(std::cout);
logger->AddLogOutput(defaultOutput);
return logger;
}
this->SetTimeStampFormat(itk::LoggerBase::HUMANREADABLE);
this->SetHumanReadableFormat("%Y-%m-%d %H:%M:%S");
m_LogSetupInfoDone = false;
Logger * Logger::Instance()
{
static Logger * logger_singleton = CreateInstance();
if ( !is_logger_singleton_level_set )
{
is_logger_singleton_level_set = true;
logger_singleton->SetPriorityLevel(ConfigurationManager::GetLoggerLevel());
}
return logger_singleton;
}
Logger::~Logger()
Logger::Pointer Logger::New()
{
Pointer smartPtr = new Logger;
smartPtr->UnRegister();
smartPtr->SetPriorityLevel(Instance()->GetPriorityLevel());
return smartPtr;
}
void Logger::LogSetupInformation()
......@@ -72,57 +81,32 @@ void Logger::LogSetupInformation()
{
std::ostringstream oss;
oss<<"Default RAM limit for OTB is "<<otb::ConfigurationManager::GetMaxRAMHint()<<" MB"<<std::endl;
oss<<"Default RAM limit for OTB is "<<
otb::ConfigurationManager::GetMaxRAMHint()<<" MB"<<std::endl;
this->Info(oss.str());
oss.str("");
oss.clear();
oss<<"GDAL maximum cache size is "<<GDALGetCacheMax64()/(1024*1024)<<" MB"<<std::endl;
oss<<"GDAL maximum cache size is "<<
GDALGetCacheMax64()/(1024*1024)<<" MB"<<std::endl;
this->Info(oss.str());
oss.str("");
oss.clear();
oss<<"OTB will use at most "<<itk::MultiThreader::GetGlobalDefaultNumberOfThreads()<<" threads"<<std::endl;
oss<<"OTB will use at most "<<
itk::MultiThreader::GetGlobalDefaultNumberOfThreads()<<
" threads"<<std::endl;
this->Info(oss.str());
oss.str("");
oss.clear();
// ensure LogSetupInformation is done once per logger, and also that it is
// skipped by the singleton when it has already been printed by an other instance
// ensure LogSetupInformation is done once per logger, and also that it is
// skipped by the singleton when it has already been printed by an other instance
LogSetupInformationDone();
Instance()->LogSetupInformationDone();
}
}
std::string Logger::BuildFormattedEntry(itk::Logger::PriorityLevelType level, std::string const & content)
{
static const std::string levelString[] = { "(MUSTFLUSH)", "(FATAL)", "(CRITICAL)",
"(WARNING)", "(INFO)", "(DEBUG)", "(NOTSET)" };
// TODO: assert(level <= std::extent<decltype(levelString)>::value); // requires C++11
std::ostringstream s;
switch ( this->m_TimeStampFormat )
{
case REALVALUE:
{
s.precision(30);
s << m_Clock->GetTimeInSeconds();
break;
}
case HUMANREADABLE:
{
s << itksys::SystemTools::GetCurrentDateTime( this->m_HumanReadableFormat.c_str() );
break;
}
}
s << " " << levelString[level] << ": " << content;
return s.str();
}
bool Logger::IsLogSetupInformationDone()
{
return m_LogSetupInfoDone;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment