mirror of https://github.com/opencv/opencv.git
Merge pull request #10340 from alalek:log_level_option
commit
ae8bb718cd
3 changed files with 128 additions and 19 deletions
@ -0,0 +1,22 @@ |
||||
// This file is part of OpenCV project.
|
||||
// It is subject to the license terms in the LICENSE file found in the top-level directory
|
||||
// of this distribution and at http://opencv.org/license.html.
|
||||
|
||||
#ifndef OPENCV_LOGGER_DEFINES_HPP |
||||
#define OPENCV_LOGGER_DEFINES_HPP |
||||
|
||||
//! @addtogroup core_logging
|
||||
//! @{
|
||||
|
||||
// Supported logging levels and their semantic
|
||||
#define CV_LOG_LEVEL_SILENT 0 //!< for using in setLogLevel() call
|
||||
#define CV_LOG_LEVEL_FATAL 1 //!< Fatal (critical) error (unrecoverable internal error)
|
||||
#define CV_LOG_LEVEL_ERROR 2 //!< Error message
|
||||
#define CV_LOG_LEVEL_WARN 3 //!< Warning message
|
||||
#define CV_LOG_LEVEL_INFO 4 //!< Info message
|
||||
#define CV_LOG_LEVEL_DEBUG 5 //!< Debug message. Disabled in the "Release" build.
|
||||
#define CV_LOG_LEVEL_VERBOSE 6 //!< Verbose (trace) messages. Requires verbosity level. Disabled in the "Release" build.
|
||||
|
||||
//! @}
|
||||
|
||||
#endif // OPENCV_LOGGER_DEFINES_HPP
|
@ -0,0 +1,85 @@ |
||||
// This file is part of OpenCV project.
|
||||
// It is subject to the license terms in the LICENSE file found in the top-level directory
|
||||
// of this distribution and at http://opencv.org/license.html.
|
||||
|
||||
#include <precomp.hpp> |
||||
|
||||
#include <opencv2/core/utils/configuration.private.hpp> |
||||
#include <opencv2/core/utils/logger.hpp> |
||||
|
||||
#include <sstream> |
||||
#include <iostream> |
||||
#include <fstream> |
||||
|
||||
namespace cv { |
||||
namespace utils { |
||||
namespace logging { |
||||
|
||||
static LogLevel parseLogLevelConfiguration() |
||||
{ |
||||
static cv::String param_log_level = utils::getConfigurationParameterString("OPENCV_LOG_LEVEL", "INFO"); |
||||
if (param_log_level == "DISABLED" || param_log_level == "disabled" || |
||||
param_log_level == "0" || param_log_level == "OFF" || param_log_level == "off") |
||||
return LOG_LEVEL_SILENT; |
||||
if (param_log_level == "FATAL" || param_log_level == "fatal") |
||||
return LOG_LEVEL_FATAL; |
||||
if (param_log_level == "ERROR" || param_log_level == "error") |
||||
return LOG_LEVEL_ERROR; |
||||
if (param_log_level == "WARNING" || param_log_level == "warning" || |
||||
param_log_level == "WARNINGS" || param_log_level == "warnings" || |
||||
param_log_level == "WARN" || param_log_level == "warn") |
||||
return LOG_LEVEL_WARNING; |
||||
if (param_log_level == "INFO" || param_log_level == "info") |
||||
return LOG_LEVEL_INFO; |
||||
if (param_log_level == "DEBUG" || param_log_level == "debug") |
||||
return LOG_LEVEL_DEBUG; |
||||
if (param_log_level == "VERBOSE" || param_log_level == "verbose") |
||||
return LOG_LEVEL_VERBOSE; |
||||
std::cerr << "ERROR: Unexpected logging level value: " << param_log_level << std::endl; |
||||
return LOG_LEVEL_INFO; |
||||
} |
||||
|
||||
static LogLevel& getLogLevelVariable() |
||||
{ |
||||
static LogLevel g_logLevel = parseLogLevelConfiguration(); |
||||
return g_logLevel; |
||||
} |
||||
|
||||
LogLevel setLogLevel(LogLevel logLevel) |
||||
{ |
||||
LogLevel old = getLogLevelVariable(); |
||||
getLogLevelVariable() = logLevel; |
||||
return old; |
||||
} |
||||
|
||||
LogLevel getLogLevel() |
||||
{ |
||||
return getLogLevelVariable(); |
||||
} |
||||
|
||||
namespace internal { |
||||
|
||||
void writeLogMessage(LogLevel logLevel, const char* message) |
||||
{ |
||||
const int threadID = cv::utils::getThreadID(); |
||||
std::ostream* out = (logLevel <= LOG_LEVEL_WARNING) ? &std::cerr : &std::cout; |
||||
std::stringstream ss; |
||||
switch (logLevel) |
||||
{ |
||||
case LOG_LEVEL_FATAL: ss << "[FATAL:" << threadID << "] " << message << std::endl; break; |
||||
case LOG_LEVEL_ERROR: ss << "[ERROR:" << threadID << "] " << message << std::endl; break; |
||||
case LOG_LEVEL_WARNING: ss << "[ WARN:" << threadID << "] " << message << std::endl; break; |
||||
case LOG_LEVEL_INFO: ss << "[ INFO:" << threadID << "] " << message << std::endl; break; |
||||
case LOG_LEVEL_DEBUG: ss << "[DEBUG:" << threadID << "] " << message << std::endl; break; |
||||
case LOG_LEVEL_VERBOSE: ss << message << std::endl; break; |
||||
default: |
||||
return; |
||||
} |
||||
(*out) << ss.str(); |
||||
if (logLevel <= LOG_LEVEL_WARNING) |
||||
(*out) << std::flush; |
||||
} |
||||
|
||||
} // namespace
|
||||
|
||||
}}} // namespace
|
Loading…
Reference in new issue