diff --git a/frameworks/libhilog/hilog_printf.cpp b/frameworks/libhilog/hilog_printf.cpp index 1e92587d263630ecfb315d260ff5c1fccd74d7af..3e4d617eaaa04e73eeeccdb7e1dee61134820614 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) { @@ -224,7 +233,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'; } @@ -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; diff --git a/test/unittest/common/hilog_print_test.cpp b/test/unittest/common/hilog_print_test.cpp index 542b322bdc1ea367c64783f124c6b5cdfc5b5b1a..bb9259e6f835a9eaceae748fd56cb5023425fbfd 100644 --- a/test/unittest/common/hilog_print_test.cpp +++ b/test/unittest/common/hilog_print_test.cpp @@ -24,6 +24,9 @@ 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 = ""; std::string GetCmdResultFromPopen(const std::string& cmd) { @@ -48,6 +51,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 +235,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