Skip to content

OpenVINO™ is an open source toolkit for optimizing and deploying AI inference

[C API] Add log-callback setup (#32082)

Tomasz Jankowski5e7df3caefda
diff --git a/src/bindings/c/include/openvino/c/openvino.h b/src/bindings/c/include/openvino/c/openvino.h
index 0eb40cc6409..177af83d585 100644
--- a/src/bindings/c/include/openvino/c/openvino.h
+++ b/src/bindings/c/include/openvino/c/openvino.h
@@ -37,3 +37,4 @@
#include "openvino/c/ov_remote_context.h"
#include "openvino/c/ov_shape.h"
#include "openvino/c/ov_tensor.h"
+#include "openvino/c/ov_util.h"
diff --git a/src/bindings/c/include/openvino/c/ov_util.h b/src/bindings/c/include/openvino/c/ov_util.h
new file mode 100644
index 00000000000..b6efa64d456
--- /dev/null
+++ b/src/bindings/c/include/openvino/c/ov_util.h
@@ -0,0 +1,27 @@
+// Copyright (C) 2018-2025 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#pragma once
+
+#include "openvino/c/ov_common.h"
+
+/**
+ * @brief Callback function type for logging messages.
+ * @param message The log message as a null-terminated C string.
+ */
+typedef void (*ov_util_log_callback_func)(const char* message);
+
+/**
+ * @brief Sets user log message handling callback.
+ * @param [in] func The function pointer to user-defined message logging callback.
+ * Null pointer is accepted (no logging).
+ */
+OPENVINO_C_API(void)
+ov_util_set_log_callback(ov_util_log_callback_func func);
+
+/**
+ * @brief Resets log message handling callback to its default (standard output).
+ */
+OPENVINO_C_API(void)
+ov_util_reset_log_callback();
diff --git a/src/bindings/c/src/ov_util.cpp b/src/bindings/c/src/ov_util.cpp
new file mode 100644
index 00000000000..90f4fd1753e
--- /dev/null
+++ b/src/bindings/c/src/ov_util.cpp
@@ -0,0 +1,23 @@
+// Copyright (C) 2018-2025 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#include "openvino/c/ov_util.h"
+
+#include "openvino/core/log.hpp"
+
+void ov_util_set_log_callback(ov_util_log_callback_func f) {
+ if (f) {
+ static std::function<void(std::string_view)> callback;
+ callback = [f](std::string_view msg) {
+ f(msg.data());
+ };
+ ov::util::set_log_callback(callback);
+ } else {
+ ov::util::set_log_callback({});
+ }
+}
+
+void ov_util_reset_log_callback() {
+ ov::util::reset_log_callback();
+}
diff --git a/src/bindings/c/tests/ov_util_test.cpp b/src/bindings/c/tests/ov_util_test.cpp
new file mode 100644
index 00000000000..04aab861318
--- /dev/null
+++ b/src/bindings/c/tests/ov_util_test.cpp
@@ -0,0 +1,53 @@
+// Copyright (C) 2018-2025 Intel Corporation
+// SPDX-License-Identifier: Apache-2.0
+//
+
+#include <gtest/gtest.h>
+
+#include <cstring>
+
+#include "openvino/c/openvino.h"
+#include "openvino/util/log.hpp"
+
+namespace {
+void test_log_msg(const char* msg) {
+ ov::util::LogHelper{ov::util::LOG_TYPE::_LOG_TYPE_INFO, "1664804", 101}.stream() << msg;
+}
+
+std::string got_message;
+void callback_func(const char* msg) {
+ got_message = msg;
+}
+bool compare_msg(const char* msg) {
+ const auto msg_size = std::strlen(msg);
+ if (got_message.size() < msg_size) {
+ return false;
+ }
+ return std::strncmp(msg, got_message.data() + got_message.size() - msg_size, msg_size) == 0;
+}
+} // namespace
+
+TEST(ov_util, set_log_callback) {
+ got_message.clear();
+ test_log_msg("default");
+ EXPECT_TRUE(got_message.empty());
+ ov_util_set_log_callback(&callback_func);
+ const char test_msg[] = "callback";
+ test_log_msg(test_msg);
+ EXPECT_TRUE(compare_msg(test_msg));
+}
+
+TEST(ov_util, reset_log_callback) {
+ got_message.clear();
+ ov_util_set_log_callback(&callback_func);
+ ov_util_reset_log_callback();
+ test_log_msg("default");
+ EXPECT_TRUE(got_message.empty());
+}
+
+TEST(ov_util, no_log_callback) {
+ got_message.clear();
+ ov_util_set_log_callback(NULL);
+ EXPECT_NO_THROW(test_log_msg("default"));
+ EXPECT_TRUE(got_message.empty());
+}