From e70759cc4c3b84b0a322bc3677186005e4adcbd2 Mon Sep 17 00:00:00 2001 From: hmilylmk Date: Wed, 17 Aug 2022 11:42:22 +0800 Subject: [PATCH] dsoftbus: add hilog module for solving print error native hilog can recongize format string like "xxxx %{public/private}d xxxx", we adapter hilog that deleting {public/private} substring, and use vprintf for log print. Signed-off-by: hmilylmk --- dsoftbus/depend/BUILD.gn | 11 ++++++- dsoftbus/depend/hilog/hilog.c | 38 +++++++++++++++++++++++ dsoftbus/depend/hilog/include/hilog/log.h | 24 +++++++++++--- 3 files changed, 67 insertions(+), 6 deletions(-) create mode 100644 dsoftbus/depend/hilog/hilog.c diff --git a/dsoftbus/depend/BUILD.gn b/dsoftbus/depend/BUILD.gn index ef1d939f..c0879e9d 100644 --- a/dsoftbus/depend/BUILD.gn +++ b/dsoftbus/depend/BUILD.gn @@ -10,11 +10,20 @@ ohos_shared_library("deviceauth_sdk") { } config("hilog_config") { - include_dirs = [ "hilog/include" ] + include_dirs = [ + "hilog/include", + "//third_party/bounds_checking_function/include", + ] } ohos_shared_library("libhilog") { public_configs = [ ":hilog_config" ] + sources = [ + "hilog/hilog.c", + ] + deps = [ + "//third_party/bounds_checking_function:libsec_shared", + ] } config("ipc_config") { diff --git a/dsoftbus/depend/hilog/hilog.c b/dsoftbus/depend/hilog/hilog.c new file mode 100644 index 00000000..9271d2a8 --- /dev/null +++ b/dsoftbus/depend/hilog/hilog.c @@ -0,0 +1,38 @@ +#include +#include +#include +#include +#include "securec.h" + +#ifndef LOG_PRINT_MAX_LEN +#define LOG_PRINT_MAX_LEN 256 +#endif + +char *adapterStrForPrintfFormat(const char *fmt) { + char *left, *right; + char *buffer = (char *)malloc(LOG_PRINT_MAX_LEN * sizeof(char)); + (void)memset_s(buffer, LOG_PRINT_MAX_LEN * sizeof(char), 0, LOG_PRINT_MAX_LEN * sizeof(char)); + strcpy_s(buffer, LOG_PRINT_MAX_LEN * sizeof(char), fmt); + while (strstr(buffer, "{")) { + left = strstr(buffer, "{"); + right = strstr(buffer, "}"); + right++; + while (*right != '\0') { + *left = *right; + left++; + right++; + } + *left = '\0'; + } + return buffer; +} + +void printfAdapter(const char *fmt, ...) { + char *buffer; + buffer = adapterStrForPrintfFormat(fmt); + va_list ap; + va_start(ap, fmt); + vprintf(buffer, ap); + va_end(ap); + free(buffer); +} diff --git a/dsoftbus/depend/hilog/include/hilog/log.h b/dsoftbus/depend/hilog/include/hilog/log.h index 76b005bc..715880c1 100644 --- a/dsoftbus/depend/hilog/include/hilog/log.h +++ b/dsoftbus/depend/hilog/include/hilog/log.h @@ -1,11 +1,25 @@ #ifndef _HILOG_H #define _HILOG_H -#include +#ifdef __cplusplus +#if __cplusplus +extern "C" { +#endif +#endif + + +char *adapterStrForPrintfFormat(const char *fmt); +void printfAdapter(const char *fmt, ...); -#define HILOG_DEBUG(type, fmt, ...) printf(fmt"\n", ##__VA_ARGS__) -#define HILOG_INFO(type, fmt, ...) printf(fmt"\n", ##__VA_ARGS__) -#define HILOG_WARN(type, fmt, ...) printf(fmt"\n", ##__VA_ARGS__) -#define HILOG_ERROR(type, fmt, ...) printf(fmt"\n", ##__VA_ARGS__) +#define HILOG_DEBUG(type, fmt, ...) printfAdapter(fmt"\n", ##__VA_ARGS__) +#define HILOG_INFO(type, fmt, ...) printfAdapter(fmt"\n", ##__VA_ARGS__) +#define HILOG_WARN(type, fmt, ...) printfAdapter(fmt"\n", ##__VA_ARGS__) +#define HILOG_ERROR(type, fmt, ...) printfAdapter(fmt"\n", ##__VA_ARGS__) + +#ifdef __cplusplus +#if __cplusplus +} +#endif +#endif #endif -- Gitee