mirror of
https://github.com/opencv/opencv.git
synced 2025-06-14 21:50:50 +08:00
Merge pull request #27154 from kinchungwong:logging_callback_simple_c
User-defined logger callback, C-style. #27154 This is a competing PR, an alternative to #27140 Both functions accept C-style pointer to static functions. Both functions allow restoring the OpenCV built-in implementation by passing in a nullptr. - replaceWriteLogMessage - replaceWriteLogMessageEx This implementation is not compatible with C++ log handler objects. This implementation has minimal thread safety, in the sense that the function pointer are stored and read atomically. But otherwise, the user-defined static functions must accept calls at all times, even after having been deregistered, because some log calls may have started before deregistering. ### Pull Request Readiness Checklist See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request - [x] I agree to contribute to the project under Apache 2 License. - [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV - [ ] The PR is proposed to the proper branch - [ ] There is a reference to the original bug report and related work - [ ] There is accuracy test, performance test and test data in opencv_extra repository, if applicable Patch to opencv_extra has the same branch name. - [ ] The feature is well documented and sample code can be built with the project CMake
This commit is contained in:
parent
767407e711
commit
afc7c0a89c
@ -43,6 +43,44 @@ CV_EXPORTS void writeLogMessage(LogLevel logLevel, const char* message);
|
|||||||
/** Write log message */
|
/** Write log message */
|
||||||
CV_EXPORTS void writeLogMessageEx(LogLevel logLevel, const char* tag, const char* file, int line, const char* func, const char* message);
|
CV_EXPORTS void writeLogMessageEx(LogLevel logLevel, const char* tag, const char* file, int line, const char* func, const char* message);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Function pointer type for writeLogMessage. Used by replaceWriteLogMessage.
|
||||||
|
*/
|
||||||
|
typedef void (*WriteLogMessageFuncType)(LogLevel, const char*);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Function pointer type for writeLogMessageEx. Used by replaceWriteLogMessageEx.
|
||||||
|
*/
|
||||||
|
typedef void (*WriteLogMessageExFuncType)(LogLevel, const char*, const char*, int, const char*, const char*);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Replaces the OpenCV writeLogMessage function with a user-defined function.
|
||||||
|
* @note The user-defined function must have the same signature as writeLogMessage.
|
||||||
|
* @note The user-defined function must accept arguments that can be potentially null.
|
||||||
|
* @note The user-defined function must be thread-safe, as OpenCV logging may be called
|
||||||
|
* from multiple threads.
|
||||||
|
* @note The user-defined function must not perform any action that can trigger
|
||||||
|
* deadlocks or infinite loop. Many OpenCV functions are not re-entrant.
|
||||||
|
* @note Once replaced, logs will not go through the OpenCV writeLogMessage function.
|
||||||
|
* @note To restore, call this function with a nullptr.
|
||||||
|
*/
|
||||||
|
CV_EXPORTS void replaceWriteLogMessage(WriteLogMessageFuncType f);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Replaces the OpenCV writeLogMessageEx function with a user-defined function.
|
||||||
|
* @note The user-defined function must have the same signature as writeLogMessage.
|
||||||
|
* @note The user-defined function must accept arguments that can be potentially null.
|
||||||
|
* @note The user-defined function must be thread-safe, as OpenCV logging may be called
|
||||||
|
* from multiple threads.
|
||||||
|
* @note The user-defined function must not perform any action that can trigger
|
||||||
|
* deadlocks or infinite loop. Many OpenCV functions are not re-entrant.
|
||||||
|
* @note Once replaced, logs will not go through any of the OpenCV logging functions
|
||||||
|
* such as writeLogMessage or writeLogMessageEx, until their respective restore
|
||||||
|
* methods are called.
|
||||||
|
* @note To restore, call this function with a nullptr.
|
||||||
|
*/
|
||||||
|
CV_EXPORTS void replaceWriteLogMessageEx(WriteLogMessageExFuncType f);
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
struct LogTagAuto
|
struct LogTagAuto
|
||||||
|
@ -12,6 +12,7 @@
|
|||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
|
#include <atomic>
|
||||||
|
|
||||||
#ifdef __ANDROID__
|
#ifdef __ANDROID__
|
||||||
# include <android/log.h>
|
# include <android/log.h>
|
||||||
@ -181,6 +182,12 @@ LogLevel getLogLevel()
|
|||||||
|
|
||||||
namespace internal {
|
namespace internal {
|
||||||
|
|
||||||
|
namespace //unnamed
|
||||||
|
{
|
||||||
|
std::atomic<WriteLogMessageFuncType> stc_userWriteLogMessageFunc{};
|
||||||
|
std::atomic<WriteLogMessageExFuncType> stc_userWriteLogMessageExFunc{};
|
||||||
|
} //unnamed
|
||||||
|
|
||||||
static int getShowTimestampMode()
|
static int getShowTimestampMode()
|
||||||
{
|
{
|
||||||
static bool param_timestamp_enable = utils::getConfigurationParameterBool("OPENCV_LOG_TIMESTAMP", true);
|
static bool param_timestamp_enable = utils::getConfigurationParameterBool("OPENCV_LOG_TIMESTAMP", true);
|
||||||
@ -190,6 +197,13 @@ static int getShowTimestampMode()
|
|||||||
|
|
||||||
void writeLogMessage(LogLevel logLevel, const char* message)
|
void writeLogMessage(LogLevel logLevel, const char* message)
|
||||||
{
|
{
|
||||||
|
WriteLogMessageFuncType userFunc = stc_userWriteLogMessageFunc.load();
|
||||||
|
if (userFunc && userFunc != writeLogMessage)
|
||||||
|
{
|
||||||
|
(*userFunc)(logLevel, message);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
const int threadID = cv::utils::getThreadID();
|
const int threadID = cv::utils::getThreadID();
|
||||||
|
|
||||||
std::string message_id;
|
std::string message_id;
|
||||||
@ -230,8 +244,10 @@ void writeLogMessage(LogLevel logLevel, const char* message)
|
|||||||
std::ostream* out = (logLevel <= LOG_LEVEL_WARNING) ? &std::cerr : &std::cout;
|
std::ostream* out = (logLevel <= LOG_LEVEL_WARNING) ? &std::cerr : &std::cout;
|
||||||
(*out) << ss.str();
|
(*out) << ss.str();
|
||||||
if (logLevel <= LOG_LEVEL_WARNING)
|
if (logLevel <= LOG_LEVEL_WARNING)
|
||||||
|
{
|
||||||
(*out) << std::flush;
|
(*out) << std::flush;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
static const char* stripSourceFilePathPrefix(const char* file)
|
static const char* stripSourceFilePathPrefix(const char* file)
|
||||||
{
|
{
|
||||||
@ -252,6 +268,13 @@ static const char* stripSourceFilePathPrefix(const char* file)
|
|||||||
|
|
||||||
void writeLogMessageEx(LogLevel logLevel, const char* tag, const char* file, int line, const char* func, const char* message)
|
void writeLogMessageEx(LogLevel logLevel, const char* tag, const char* file, int line, const char* func, const char* message)
|
||||||
{
|
{
|
||||||
|
WriteLogMessageExFuncType userFunc = stc_userWriteLogMessageExFunc.load();
|
||||||
|
if (userFunc && userFunc != writeLogMessageEx)
|
||||||
|
{
|
||||||
|
(*userFunc)(logLevel, tag, file, line, func, message);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
std::ostringstream strm;
|
std::ostringstream strm;
|
||||||
if (tag)
|
if (tag)
|
||||||
{
|
{
|
||||||
@ -274,6 +297,24 @@ void writeLogMessageEx(LogLevel logLevel, const char* tag, const char* file, int
|
|||||||
writeLogMessage(logLevel, strm.str().c_str());
|
writeLogMessage(logLevel, strm.str().c_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void replaceWriteLogMessage(WriteLogMessageFuncType f)
|
||||||
|
{
|
||||||
|
if (f == writeLogMessage)
|
||||||
|
{
|
||||||
|
f = nullptr;
|
||||||
|
}
|
||||||
|
stc_userWriteLogMessageFunc.store(f);
|
||||||
|
}
|
||||||
|
|
||||||
|
void replaceWriteLogMessageEx(WriteLogMessageExFuncType f)
|
||||||
|
{
|
||||||
|
if (f == writeLogMessageEx)
|
||||||
|
{
|
||||||
|
f = nullptr;
|
||||||
|
}
|
||||||
|
stc_userWriteLogMessageExFunc.store(f);
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
}}} // namespace
|
}}} // namespace
|
||||||
|
115
modules/core/test/test_logger_replace.cpp
Normal file
115
modules/core/test/test_logger_replace.cpp
Normal file
@ -0,0 +1,115 @@
|
|||||||
|
// 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 "test_precomp.hpp"
|
||||||
|
#include <atomic>
|
||||||
|
#include <opencv2/core/utils/logger.hpp>
|
||||||
|
|
||||||
|
namespace opencv_test {
|
||||||
|
namespace {
|
||||||
|
|
||||||
|
using namespace cv::utils::logging;
|
||||||
|
using namespace cv::utils::logging::internal;
|
||||||
|
|
||||||
|
TEST(Core_Logger_Replace, WriteLogMessageRestoreCallWithNullOk)
|
||||||
|
{
|
||||||
|
replaceWriteLogMessage(nullptr);
|
||||||
|
writeLogMessage(cv::utils::logging::LOG_LEVEL_DEBUG, "msg");
|
||||||
|
SUCCEED();
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(Core_Logger_Replace, WriteLogMessageExRestoreCallWithNullOk)
|
||||||
|
{
|
||||||
|
replaceWriteLogMessageEx(nullptr);
|
||||||
|
writeLogMessageEx(cv::utils::logging::LOG_LEVEL_DEBUG, "tag", "file", 1000, "func", "msg");
|
||||||
|
SUCCEED();
|
||||||
|
}
|
||||||
|
|
||||||
|
std::atomic<uint32_t>& getCallFlagger()
|
||||||
|
{
|
||||||
|
static std::atomic<uint32_t> callFlagger(0);
|
||||||
|
return callFlagger;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::atomic<uint32_t>& getCallCounter()
|
||||||
|
{
|
||||||
|
static std::atomic<uint32_t> callCounter(0);
|
||||||
|
return callCounter;
|
||||||
|
}
|
||||||
|
|
||||||
|
void myWriteLogMessage(LogLevel, const char*)
|
||||||
|
{
|
||||||
|
getCallFlagger().fetch_or(1024u);
|
||||||
|
getCallCounter().fetch_add(1u);
|
||||||
|
}
|
||||||
|
|
||||||
|
void myWriteLogMessageEx(LogLevel, const char*, const char*, int, const char*, const char*)
|
||||||
|
{
|
||||||
|
getCallFlagger().fetch_or(2048u);
|
||||||
|
getCallCounter().fetch_add(1u);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(Core_Logger_Replace, WriteLogMessageReplaceRestore)
|
||||||
|
{
|
||||||
|
uint32_t step_0 = getCallCounter().load();
|
||||||
|
writeLogMessage(cv::utils::logging::LOG_LEVEL_DEBUG, "msg");
|
||||||
|
uint32_t step_1 = getCallCounter().load();
|
||||||
|
EXPECT_EQ(step_0, step_1);
|
||||||
|
replaceWriteLogMessage(nullptr);
|
||||||
|
uint32_t step_2 = getCallCounter().load();
|
||||||
|
EXPECT_EQ(step_1, step_2);
|
||||||
|
writeLogMessage(cv::utils::logging::LOG_LEVEL_DEBUG, "msg");
|
||||||
|
uint32_t step_3 = getCallCounter().load();
|
||||||
|
EXPECT_EQ(step_2, step_3);
|
||||||
|
replaceWriteLogMessage(myWriteLogMessage);
|
||||||
|
uint32_t step_4 = getCallCounter().load();
|
||||||
|
EXPECT_EQ(step_3, step_4);
|
||||||
|
writeLogMessage(cv::utils::logging::LOG_LEVEL_DEBUG, "msg");
|
||||||
|
uint32_t step_5 = getCallCounter().load();
|
||||||
|
EXPECT_EQ(step_4 + 1, step_5);
|
||||||
|
writeLogMessage(cv::utils::logging::LOG_LEVEL_DEBUG, "msg");
|
||||||
|
uint32_t step_6 = getCallCounter().load();
|
||||||
|
EXPECT_EQ(step_5 + 1, step_6);
|
||||||
|
replaceWriteLogMessage(nullptr);
|
||||||
|
uint32_t step_7 = getCallCounter().load();
|
||||||
|
EXPECT_EQ(step_6, step_7);
|
||||||
|
writeLogMessage(cv::utils::logging::LOG_LEVEL_DEBUG, "msg");
|
||||||
|
uint32_t step_8 = getCallCounter().load();
|
||||||
|
EXPECT_EQ(step_7, step_8);
|
||||||
|
uint32_t flags = getCallFlagger().load();
|
||||||
|
EXPECT_NE(flags & 1024u, 0u);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(Core_Logger_Replace, WriteLogMessageExReplaceRestore)
|
||||||
|
{
|
||||||
|
uint32_t step_0 = getCallCounter().load();
|
||||||
|
writeLogMessageEx(cv::utils::logging::LOG_LEVEL_DEBUG, "tag", "file", 0, "func", "msg");
|
||||||
|
uint32_t step_1 = getCallCounter().load();
|
||||||
|
EXPECT_EQ(step_0, step_1);
|
||||||
|
replaceWriteLogMessageEx(nullptr);
|
||||||
|
uint32_t step_2 = getCallCounter().load();
|
||||||
|
EXPECT_EQ(step_1, step_2);
|
||||||
|
writeLogMessageEx(cv::utils::logging::LOG_LEVEL_DEBUG, "tag", "file", 0, "func", "msg");
|
||||||
|
uint32_t step_3 = getCallCounter().load();
|
||||||
|
EXPECT_EQ(step_2, step_3);
|
||||||
|
replaceWriteLogMessageEx(myWriteLogMessageEx);
|
||||||
|
uint32_t step_4 = getCallCounter().load();
|
||||||
|
EXPECT_EQ(step_3, step_4);
|
||||||
|
writeLogMessageEx(cv::utils::logging::LOG_LEVEL_DEBUG, "tag", "file", 0, "func", "msg");
|
||||||
|
uint32_t step_5 = getCallCounter().load();
|
||||||
|
EXPECT_EQ(step_4 + 1, step_5);
|
||||||
|
writeLogMessageEx(cv::utils::logging::LOG_LEVEL_DEBUG, "tag", "file", 0, "func", "msg");
|
||||||
|
uint32_t step_6 = getCallCounter().load();
|
||||||
|
EXPECT_EQ(step_5 + 1, step_6);
|
||||||
|
replaceWriteLogMessageEx(nullptr);
|
||||||
|
uint32_t step_7 = getCallCounter().load();
|
||||||
|
EXPECT_EQ(step_6, step_7);
|
||||||
|
writeLogMessageEx(cv::utils::logging::LOG_LEVEL_DEBUG, "tag", "file", 0, "func", "msg");
|
||||||
|
uint32_t step_8 = getCallCounter().load();
|
||||||
|
EXPECT_EQ(step_7, step_8);
|
||||||
|
uint32_t flags = getCallFlagger().load();
|
||||||
|
EXPECT_NE(flags & 2048u, 0u);
|
||||||
|
}
|
||||||
|
|
||||||
|
}} // namespace
|
Loading…
Reference in New Issue
Block a user