From f50c001a5c2432a6451c376c767099000eb28a98 Mon Sep 17 00:00:00 2001 From: liuyifei Date: Fri, 20 Dec 2024 02:19:54 +0000 Subject: [PATCH] =?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