diff --git a/frameworks/hilog_ndk/hilog_ndk.c b/frameworks/hilog_ndk/hilog_ndk.c
index 79466f37b8474b471a4bc221788b174296f90409..792cca33b1bb36f77ef5ba3dd7f83ead87d7224c 100644
--- a/frameworks/hilog_ndk/hilog_ndk.c
+++ b/frameworks/hilog_ndk/hilog_ndk.c
@@ -32,3 +32,8 @@ bool OH_LOG_IsLoggable(unsigned int domain, const char *tag, LogLevel level)
{
return HiLogIsLoggable(domain, tag, level);
}
+
+void OH_LOG_SetCallback(LogCallback callback)
+{
+ return LOG_SetCallback(callback);
+}
diff --git a/frameworks/libhilog/hilog_printf.cpp b/frameworks/libhilog/hilog_printf.cpp
index 506c38e237a6590390ef1386e5abec0953d9027c..15c853ee2a61c6f4b9789733397efac1a4c67ee2 100644
--- a/frameworks/libhilog/hilog_printf.cpp
+++ b/frameworks/libhilog/hilog_printf.cpp
@@ -52,6 +52,7 @@
using namespace std;
using namespace OHOS::HiviewDFX;
static RegisterFunc g_registerFunc = nullptr;
+static LogCallback g_logCallback = nullptr;
static atomic_int g_hiLogGetIdCallCount = 0;
// protected by static lock guard
static char g_hiLogLastFatalMessage[MAX_LOG_LEN] = { 0 }; // MAX_lOG_LEN : 1024
@@ -85,6 +86,11 @@ void HiLogUnregisterGetIdFun(RegisterFunc registerFunc)
return;
}
+void LOG_SetCallback(LogCallback callback)
+{
+ g_logCallback = callback;
+}
+
static uint16_t GetFinalLevel(unsigned int domain, const std::string& tag)
{
// Priority: TagLevel > DomainLevel > GlobalLevel
@@ -251,6 +257,9 @@ int HiLogPrintArgs(const LogType type, const LogLevel level, const unsigned int
#pragma GCC diagnostic ignored "-Wformat-nonliteral"
#endif
vsnprintfp_s(logBuf, MAX_LOG_LEN - traceBufLen, MAX_LOG_LEN - traceBufLen - 1, priv, fmt, ap);
+ if (g_logCallback != nullptr) {
+ g_logCallback(type, level, domain, tag, logBuf);
+ }
#ifdef __clang__
#pragma clang diagnostic pop
#elif __GNUC__
diff --git a/interfaces/native/innerkits/include/hilog/log_c.h b/interfaces/native/innerkits/include/hilog/log_c.h
index 0632f2d62ac1aca0415fce35ea6f6534367e9d3e..9284e8afb37c1314e42b341f05c4a6624bc42df4 100644
--- a/interfaces/native/innerkits/include/hilog/log_c.h
+++ b/interfaces/native/innerkits/include/hilog/log_c.h
@@ -121,6 +121,34 @@ int HiLogPrint(LogType type, LogLevel level, unsigned int domain, const char *ta
*/
bool HiLogIsLoggable(unsigned int domain, const char *tag, LogLevel level);
+/**
+ * @brief Defines the function pointer type for the user-defined log processing function.
+ *
+ * @param type Indicates the log type. The type for third-party applications is defined by {@link LOG_APP}.
+ * @param level Indicates the log level, which can be LOG_DEBUG, LOG_INFO, LOG_WARN,
+ * LOG_ERROR, and LOG_FATAL.
+ * @param domain Indicates the service domain of logs. Its value is a hexadecimal integer ranging from 0x0 to 0xFFFF.
+ * @param tag Indicates the log tag, which is a string used to identify the class, file, or service behavior.
+ * @param msg Indicates the log message itself, which is a formatted log string.
+ * @since 11
+ */
+typedef void (*LogCallback)(const LogType type, const LogLevel level, const unsigned int domain, const char *tag,
+ const char *msg);
+
+/**
+ * @brief Set the user-defined log processing function.
+ *
+ * After calling this function, the callback function implemented by the user can receive all hilogs of the
+ * current process.
+ * Note that it will not change the default behavior of hilog logs of the current process, no matter whether this
+ * interface is called or not. \n
+ *
+ * @param callback Indicates the callback function implemented by the user. If you do not need to process hilog logs,
+ * you can transfer a null pointer.
+ * @since 11
+ */
+void LOG_SetCallback(LogCallback callback);
+
#ifdef __cplusplus
}
#endif
diff --git a/interfaces/native/innerkits/libhilog.map b/interfaces/native/innerkits/libhilog.map
index 41b46645ad08e6dc37776be3f86118b2e12fc1b5..0312d454d1835a24dbaf0c14130cb6a10e3ce455 100644
--- a/interfaces/native/innerkits/libhilog.map
+++ b/interfaces/native/innerkits/libhilog.map
@@ -10,6 +10,7 @@
HiLogUnregisterGetIdFun;
HilogWriteLogMessage;
GetLastFatalMessage;
+ LOG_SetCallback;
};
extern "C++" {
"OHOS::HiviewDFX::HiLog::Info(OHOS::HiviewDFX::HiLogLabel const&, char const*, ...)";
diff --git a/interfaces/native/kits/include/hilog/log.h b/interfaces/native/kits/include/hilog/log.h
index 26049d945ad80c33ee9d3f3d124b3951e4957d2c..7452137c280d212fc271cca2f31466f0dc11bd6f 100644
--- a/interfaces/native/kits/include/hilog/log.h
+++ b/interfaces/native/kits/include/hilog/log.h
@@ -246,6 +246,34 @@ bool OH_LOG_IsLoggable(unsigned int domain, const char *tag, LogLevel level);
*/
#define OH_LOG_FATAL(type, ...) ((void)OH_LOG_Print((type), LOG_FATAL, LOG_DOMAIN, LOG_TAG, __VA_ARGS__))
+/**
+ * @brief Defines the function pointer type for the user-defined log processing function.
+ *
+ * @param type Indicates the log type. The type for third-party applications is defined by {@link LOG_APP}.
+ * @param level Indicates the log level, which can be LOG_DEBUG, LOG_INFO, LOG_WARN,
+ * LOG_ERROR, and LOG_FATAL.
+ * @param domain Indicates the service domain of logs. Its value is a hexadecimal integer ranging from 0x0 to 0xFFFF.
+ * @param tag Indicates the log tag, which is a string used to identify the class, file, or service behavior.
+ * @param msg Indicates the log message itself, which is a formatted log string.
+ * @since 11
+ */
+typedef void (*LogCallback)(const LogType type, const LogLevel level, const unsigned int domain, const char *tag,
+ const char *msg);
+
+/**
+ * @brief Set the user-defined log processing function.
+ *
+ * After calling this function, the callback function implemented by the user can receive all hilogs of the
+ * current process.
+ * Note that it will not change the default behavior of hilog logs of the current process, no matter whether this
+ * interface is called or not. \n
+ *
+ * @param callback Indicates the callback function implemented by the user. If you do not need to process hilog logs,
+ * you can transfer a null pointer.
+ * @since 11
+ */
+void OH_LOG_SetCallback(LogCallback callback);
+
#ifdef __cplusplus
}
#endif
diff --git a/interfaces/native/kits/libhilog.ndk.json b/interfaces/native/kits/libhilog.ndk.json
index f121a608ef1db5c01429864a05ba6e5dc1bb62e5..2ca6bed2c0d93fcd9fbb3046ff370f17c696ba7e 100644
--- a/interfaces/native/kits/libhilog.ndk.json
+++ b/interfaces/native/kits/libhilog.ndk.json
@@ -4,5 +4,8 @@
},
{
"name": "OH_LOG_IsLoggable"
+ },
+ {
+ "name": "OH_LOG_SetCallback"
}
]