1
0
mirror of https://github.com/opencv/opencv.git synced 2026-07-29 23:33:05 +04:00

Merge pull request #13909 from kinchungwong:logging_20190220

OE-11 Logging revamp (#13909)

* Initial commit for log tag support.

Part of #11003, incomplete. Should pass build.

Moved LogLevel enum to logger.defines.hpp

LogTag struct used to convey both name and log level threshold as
one argument to the new logging macro. See logtag.hpp file, and
CV_LOG_WITH_TAG macro.

Global log level is now associated with a global log tag, when a
logging statement doesn't specify any log tag. See getLogLevel and
getGlobalLogTag functions.

A macro CV_LOGTAG_FALLBACK is allowed to be re-defined by other modules
or compilation units, internally, so that logging statements inside
that unit that specify NULL as tag will fall back to the re-defined tag.

Line-of-code information (file name, line number, function name),
together with tag name, are passed into the new log message sink.
See writeLogMessageEx function.

Fixed old incorrect CV_LOG_VERBOSE usage in ocl4dnn_conv_spatial.cpp.

* Implemented tag-based log filtering

Added LogTagManager. This is an initial version, using standard C++
approach as much as possible, to allow easier code review. Will
optimize later.

A workaround for all static dynamic initialization issues is
implemented. Refer to code comments.

* Added LogTagConfigParser.

Note: new code does not fully handle old log config parsing behavior.

* Fix log tag config vs registering ordering issue.

* Started testing LogTagConfigParser, incomplete.

The intention of this commit is to illustrate the capabilities of
the current design of LogTagConfigParser.

The test contained in this commit is not complete. Also, design changes
may require throwing away this commit and rewriting test code from
scratch.

Does not test whitespace segmentation (multiple tags on the config);
will do in next commit.

* Added CV_LOGTAG_EXPAND_NAME macro

This macro allows to be re-defined locally in other compilation units
to apply a prefix to whatever argument is passed as the "tag" argument
into CV_LOG_WITH_TAG. The default definition in logger.hpp does not
modify the argument. It is recommended to include the address-of
operator (ampersand) when re-defined locally.

* Added a few tests for LogTagManager, some fail.

See test_logtagmanager.cpp
Failed tests are: non-global ("something"), setting level by name-part
(first part or any part) has no effect at all.

* LogTagManagerTests substring non-confusion tests

* Fix major bugs in LogTagManager

The code change is intended to approximate the spec documented in
https://gist.github.com/kinchungwong/ec25bc1eba99142e0be4509b0f67d0c6

Refer to test suite in test_logtagmanager.cpp

Filter test result in "opencv_test_core" ...
with gtest_filter "LogTagManager*"

To see the test code that finds the bugs, refer to original commits
(before rebase; might be gone)

.. f3451208 (2019-03-03T19:45:17Z)
.... LogTagManagerTests substring non-confusion tests

.. 1b848f5f (2019-03-03T01:55:18Z)
.... Added a few tests for LogTagManager, some fail.

* Added LogTagManagerNamePartNonConfusionTest.

See test_logtagmanager.cpp in modules/core/test.

* Added LogTagAuto for auto registration in ctor

* Rewritten LogTagManager to resolve issues.

* Resolves code review issues around 2019-04-10

LogTagConfigParser::parseLogLevel - as part of resolving code review
issues, this function is rewritten to simplify control flow and to
improve conformance with legacy usage (for string values "OFF",
"DISABLED", and "WARNINGS").
This commit is contained in:
Ryan Wong
2019-04-21 14:01:10 -07:00
committed by Alexander Alekhin
parent f439fc4306
commit 8af96248bf
11 changed files with 2351 additions and 60 deletions
+158 -29
View File
@@ -6,6 +6,8 @@
#include <opencv2/core/utils/configuration.private.hpp>
#include <opencv2/core/utils/logger.hpp>
#include "utils/logtagmanager.hpp"
#include "utils/logtagconfigparser.hpp"
#include <sstream>
#include <iostream>
@@ -19,52 +21,156 @@ namespace cv {
namespace utils {
namespace logging {
static LogLevel parseLogLevelConfiguration()
namespace internal
{
static cv::String param_log_level = utils::getConfigurationParameterString("OPENCV_LOG_LEVEL",
// Combining several things that require static dynamic initialization in a
// well-defined order into a struct.
//
struct GlobalLoggingInitStruct
{
public:
#if defined NDEBUG
"WARNING"
static constexpr bool m_isDebugBuild = false;
#else
"INFO"
static constexpr bool m_isDebugBuild = true;
#endif
);
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;
public:
static constexpr LogLevel m_defaultUnconfiguredGlobalLevel =
m_isDebugBuild ? LOG_LEVEL_DEBUG : LOG_LEVEL_WARNING;
public:
LogTagManager logTagManager;
GlobalLoggingInitStruct()
: logTagManager(m_defaultUnconfiguredGlobalLevel)
{
applyConfigString();
handleMalformed();
}
private:
void applyConfigString()
{
logTagManager.setConfigString(utils::getConfigurationParameterString("OPENCV_LOG_LEVEL", ""));
}
void handleMalformed()
{
// need to print warning for malformed log tag config strings?
if (m_isDebugBuild)
{
const auto& parser = logTagManager.getConfigParser();
if (parser.hasMalformed())
{
const auto& malformedList = parser.getMalformed();
for (const auto& malformed : malformedList)
{
std::cout << "Malformed log level config: \"" << malformed << "\"\n";
}
std::cout.flush();
}
}
}
};
// Static dynamic initialization guard function for the combined struct
// just defined above
//
// An initialization guard function guarantees that outside code cannot
// accidentally see not-yet-dynamically-initialized data, by routing
// all outside access request to this function, so that this function
// has a chance to run the initialization code if necessary.
//
// An initialization guard function only guarantees initialization upon
// the first call to this function.
//
static GlobalLoggingInitStruct& getGlobalLoggingInitStruct()
{
static GlobalLoggingInitStruct globalLoggingInitInstance;
return globalLoggingInitInstance;
}
// To ensure that the combined struct defined above is initialized even
// if the initialization guard function wasn't called, a dummy static
// instance of a struct is defined below, which will call the
// initialization guard function.
//
struct GlobalLoggingInitCall
{
GlobalLoggingInitCall()
{
getGlobalLoggingInitStruct();
}
};
static GlobalLoggingInitCall globalLoggingInitCall;
static LogTagManager& getLogTagManager()
{
static LogTagManager& logTagManagerInstance = getGlobalLoggingInitStruct().logTagManager;
return logTagManagerInstance;
}
static LogLevel& getLogLevelVariable()
{
static LogLevel g_logLevel = parseLogLevelConfiguration();
return g_logLevel;
static LogLevel& refGlobalLogLevel = getGlobalLogTag()->level;
return refGlobalLogLevel;
}
LogTag* getGlobalLogTag()
{
static LogTag* globalLogTagPtr = getGlobalLoggingInitStruct().logTagManager.get("global");
return globalLogTagPtr;
}
} // namespace
void registerLogTag(LogTag* plogtag)
{
if (!plogtag || !plogtag->name)
{
return;
}
internal::getLogTagManager().assign(plogtag->name, plogtag);
}
void setLogTagLevel(const char* tag, LogLevel level)
{
if (!tag)
{
return;
}
internal::getLogTagManager().setLevelByFullName(std::string(tag), level);
}
LogLevel getLogTagLevel(const char* tag)
{
if (!tag)
{
return getLogLevel();
}
const LogTag* ptr = internal::getLogTagManager().get(std::string(tag));
if (!ptr)
{
return getLogLevel();
}
return ptr->level;
}
LogLevel setLogLevel(LogLevel logLevel)
{
LogLevel old = getLogLevelVariable();
getLogLevelVariable() = logLevel;
// note: not thread safe, use sparingly and do not critically depend on outcome
LogLevel& refGlobalLevel = internal::getLogLevelVariable();
const LogLevel old = refGlobalLevel;
refGlobalLevel = logLevel;
return old;
}
LogLevel getLogLevel()
{
return getLogLevelVariable();
return internal::getLogLevelVariable();
}
namespace internal {
@@ -105,6 +211,29 @@ void writeLogMessage(LogLevel logLevel, const char* message)
(*out) << std::flush;
}
void writeLogMessageEx(LogLevel logLevel, const char* tag, const char* file, int line, const char* func, const char* message)
{
std::ostringstream strm;
if (tag)
{
strm << tag << " ";
}
if (file)
{
strm << file << " ";
}
if (line > 0)
{
strm << "(" << line << ") ";
}
if (func)
{
strm << func << " ";
}
strm << message;
writeLogMessage(logLevel, strm.str().c_str());
}
} // namespace
}}} // namespace