From f50c001a5c2432a6451c376c767099000eb28a98 Mon Sep 17 00:00:00 2001 From: liuyifei Date: Fri, 20 Dec 2024 02:19:54 +0000 Subject: [PATCH 1/3] =?UTF-8?q?kmsg=E9=9A=90=E7=A7=81=E5=BC=80=E5=85=B3?= =?UTF-8?q?=E4=BD=BF=E7=94=A8=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: liuyifei --- frameworks/libhilog/hilog_printf.cpp | 2 +- test/unittest/common/hilog_print_test.cpp | 50 +++++++++++++++++++++++ 2 files changed, 51 insertions(+), 1 deletion(-) diff --git a/frameworks/libhilog/hilog_printf.cpp b/frameworks/libhilog/hilog_printf.cpp index 1e92587..8731e6d 100644 --- a/frameworks/libhilog/hilog_printf.cpp +++ b/frameworks/libhilog/hilog_printf.cpp @@ -224,7 +224,7 @@ int HiLogPrintArgs(const LogType type, const LogLevel level, const unsigned int if (type == LOG_KMSG) { char tmpFmt[MAX_LOG_LEN] = {0}; // format va_list info to char* - if (vsnprintfp_s(tmpFmt, sizeof(tmpFmt), sizeof(tmpFmt) - 1, true, fmt, ap) == -1) { + if (vsnprintfp_s(tmpFmt, sizeof(tmpFmt), sizeof(tmpFmt) - 1, HiLogIsPrivacyOn(), fmt, ap) == -1) { tmpFmt[sizeof(tmpFmt) - 2] = '\n'; // 2 add \n to tail tmpFmt[sizeof(tmpFmt) - 1] = '\0'; } diff --git a/test/unittest/common/hilog_print_test.cpp b/test/unittest/common/hilog_print_test.cpp index 542b322..ed72aca 100644 --- a/test/unittest/common/hilog_print_test.cpp +++ b/test/unittest/common/hilog_print_test.cpp @@ -24,6 +24,8 @@ using namespace OHOS::HiviewDFX; namespace { const HiLogLabel LABEL = { LOG_CORE, 0xD002D00, "HILOGTEST_C" }; const int LOGINDEX = 42 + strlen("HILOGTEST_C"); +const HiLogLabel KMSG_LABEL = { LOG_KMSG, 0xD002D00, "HILOGTEST_C" }; +const std::string PRIV_STR = ""; std::string GetCmdResultFromPopen(const std::string& cmd) { @@ -48,6 +50,33 @@ std::string GetCmdResultFromPopen(const std::string& cmd) pclose(fp); return ret; } + +bool IsExistInCmdResult(const std::string &cmd, const std::string &str) +{ + if (cmd.empty()) { + return false; + } + FILE* fp = popen(cmd.c_str(), "r"); + if (fp == nullptr) { + return false; + } + bool ret = false; + char* buffer = nullptr; + size_t len = 0; + while (getline(&buffer, &len, fp) != -1) { + std::string line = buffer; + if (line.find(str) != string::npos) { + ret = true; + break; + } + } + if (buffer != nullptr) { + free(buffer); + buffer = nullptr; + } + pclose(fp); + return ret; +} } void HilogPrintTest::SetUpTestCase() @@ -205,4 +234,25 @@ const vector PrecisionVec = { EXPECT_EQ(log, PrecisionVec[i]); } } + +/** + * @tc.name: Dfx_HilogPrintTest_HilogKmsgPrivacyTest + * @tc.desc: HilogKmsgPrivacyTest. + * @tc.type: FUNC + */ +HWTEST_F(HilogPrintTest, HilogKmsgPrivacyTest, TestSize.Level1) +{ + GTEST_LOG_(INFO) << "HilogKmsgPrivacyTest: start."; + std::string msg = "HilogPrintTest:HilogKmsgPrivacyTest"; + HiLog::Info(KMSG_LABEL, "%s", msg.c_str()); + sleep(QUERY_INTERVAL); + EXPECT_TRUE(IsExistInCmdResult("hilog -t kmsg -x |grep HILOGTEST_C", PRIV_STR)); + + (void)GetCmdResultFromPopen("hilog -r"); + (void)GetCmdResultFromPopen("hilog -p off"); + HiLog::Info(KMSG_LABEL, "%s", msg.c_str()); + sleep(QUERY_INTERVAL); + EXPECT_TRUE(IsExistInCmdResult("hilog -t kmsg -x |grep HILOGTEST_C", msg)); + (void)GetCmdResultFromPopen("hilog -p on"); +} } // namespace \ No newline at end of file -- Gitee From e96d3b9d2a9a716151031c6db977bcd28f26f22d Mon Sep 17 00:00:00 2001 From: liuyifei Date: Mon, 23 Dec 2024 04:01:05 +0000 Subject: [PATCH 2/3] update frameworks/libhilog/hilog_printf.cpp. Signed-off-by: liuyifei --- frameworks/libhilog/hilog_printf.cpp | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/frameworks/libhilog/hilog_printf.cpp b/frameworks/libhilog/hilog_printf.cpp index 8731e6d..3e4d617 100644 --- a/frameworks/libhilog/hilog_printf.cpp +++ b/frameworks/libhilog/hilog_printf.cpp @@ -212,6 +212,15 @@ static int LogToKmsg(const LogLevel level, const char *tag, const char* info) #endif } +bool HiLogIsPrivacyOn() +{ + bool priv = true; +#if not (defined( __WINDOWS__ ) || defined( __MAC__ ) || defined( __LINUX__ )) + priv = (!IsDebugOn()) && IsPrivateSwitchOn(); +#endif + return priv; +} + int HiLogPrintArgs(const LogType type, const LogLevel level, const unsigned int domain, const char *tag, const char *fmt, va_list ap) { @@ -271,14 +280,7 @@ int HiLogPrintArgs(const LogType type, const LogLevel level, const unsigned int } } - /* format log string */ -#if not (defined( __WINDOWS__ ) || defined( __MAC__ ) || defined( __LINUX__ )) - bool debug = IsDebugOn(); - bool priv = (!debug) && IsPrivateSwitchOn(); -#else - bool priv = true; -#endif - +/* format log string */ #ifdef __clang__ /* code specific to clang compiler */ #pragma clang diagnostic push @@ -288,7 +290,7 @@ int HiLogPrintArgs(const LogType type, const LogLevel level, const unsigned int #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wformat-nonliteral" #endif - vsnprintfp_s(logBuf, MAX_LOG_LEN - traceBufLen, MAX_LOG_LEN - traceBufLen - 1, priv, fmt, ap); + vsnprintfp_s(logBuf, MAX_LOG_LEN - traceBufLen, MAX_LOG_LEN - traceBufLen - 1, HiLogIsPrivacyOn(), fmt, ap); LogCallback logCallbackFunc = g_logCallback; if (logCallbackFunc != nullptr) { logCallbackFunc(type, level, domain, tag, logBuf); @@ -334,7 +336,7 @@ int HiLogPrintArgs(const LogType type, const LogLevel level, const unsigned int #if not (defined( __WINDOWS__ ) || defined( __MAC__ ) || defined( __LINUX__ )) /* flow control */ - if (!debug && IsNeedProcFlowCtr(type)) { + if (!IsDebugOn() && IsNeedProcFlowCtr(type)) { ret = HiLogFlowCtrlProcess(tagLen + logLen - traceBufLen, ts_mono); if (ret < 0) { return ret; -- Gitee From 9671695383b9c0bee3139d7c3273d05eb9f51dc4 Mon Sep 17 00:00:00 2001 From: liuyifei Date: Mon, 23 Dec 2024 04:02:28 +0000 Subject: [PATCH 3/3] update test/unittest/common/hilog_print_test.cpp. Signed-off-by: liuyifei --- test/unittest/common/hilog_print_test.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/unittest/common/hilog_print_test.cpp b/test/unittest/common/hilog_print_test.cpp index ed72aca..bb9259e 100644 --- a/test/unittest/common/hilog_print_test.cpp +++ b/test/unittest/common/hilog_print_test.cpp @@ -24,6 +24,7 @@ using namespace OHOS::HiviewDFX; namespace { const HiLogLabel LABEL = { LOG_CORE, 0xD002D00, "HILOGTEST_C" }; const int LOGINDEX = 42 + strlen("HILOGTEST_C"); +constexpr uint32_t QUERY_INTERVAL = 1; // sleep 1s const HiLogLabel KMSG_LABEL = { LOG_KMSG, 0xD002D00, "HILOGTEST_C" }; const std::string PRIV_STR = ""; @@ -247,7 +248,7 @@ HWTEST_F(HilogPrintTest, HilogKmsgPrivacyTest, TestSize.Level1) HiLog::Info(KMSG_LABEL, "%s", msg.c_str()); sleep(QUERY_INTERVAL); EXPECT_TRUE(IsExistInCmdResult("hilog -t kmsg -x |grep HILOGTEST_C", PRIV_STR)); - + (void)GetCmdResultFromPopen("hilog -r"); (void)GetCmdResultFromPopen("hilog -p off"); HiLog::Info(KMSG_LABEL, "%s", msg.c_str()); -- Gitee