Skip to content

User-defined logger callback, C-style.#27154

Merged
asmorkalov merged 3 commits intoopencv:4.xfrom
kinchungwong:logging_callback_simple_c
Mar 30, 2025
Merged

User-defined logger callback, C-style.#27154
asmorkalov merged 3 commits intoopencv:4.xfrom
kinchungwong:logging_callback_simple_c

Conversation

@kinchungwong
Copy link
Copy Markdown
Contributor

@kinchungwong kinchungwong commented Mar 26, 2025

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

  • I agree to contribute to the project under Apache 2 License.
  • 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

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.
@kinchungwong kinchungwong force-pushed the logging_callback_simple_c branch from c788983 to 59a3fe5 Compare March 27, 2025 07:48
1. removes addressof (&) when taking address of a function.
2. added function and typedef docs
@kinchungwong kinchungwong marked this pull request as ready for review March 30, 2025 09:08
Copy link
Copy Markdown
Contributor

@opencv-alalek opencv-alalek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for contribution!

LGTM 👍

@asmorkalov asmorkalov added this to the 4.12.0 milestone Mar 30, 2025
@asmorkalov asmorkalov merged commit afc7c0a into opencv:4.x Mar 30, 2025
24 of 28 checks passed
@kinchungwong kinchungwong deleted the logging_callback_simple_c branch March 30, 2025 13:26
@patrikhuber
Copy link
Copy Markdown
Contributor

@kinchungwong Is there an example somewhere of how to use this functionality, like my wishful code in #27140 (comment)?

@mshabunin
Copy link
Copy Markdown
Contributor

@patrikhuber , I believe it works just like you've shown in the example, but the function is called replaceWriteLogMessageEx instead of setLogCallback. And callback function have slightly different arguments: (LogLevel logLevel, const char* tag, const char* file, int line, const char* func, const char* message).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants