148f512ceSopenharmony_ci/* 248f512ceSopenharmony_ci * Copyright (c) 2021-2022 Huawei Device Co., Ltd. 348f512ceSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 448f512ceSopenharmony_ci * you may not use this file except in compliance with the License. 548f512ceSopenharmony_ci * You may obtain a copy of the License at 648f512ceSopenharmony_ci * 748f512ceSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 848f512ceSopenharmony_ci * 948f512ceSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 1048f512ceSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 1148f512ceSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1248f512ceSopenharmony_ci * See the License for the specific language governing permissions and 1348f512ceSopenharmony_ci * limitations under the License. 1448f512ceSopenharmony_ci */ 1548f512ceSopenharmony_ci#include "debug_logger_test.h" 1648f512ceSopenharmony_ci 1748f512ceSopenharmony_ci#include <random> 1848f512ceSopenharmony_ci 1948f512ceSopenharmony_ci#include <debug_logger.h> 2048f512ceSopenharmony_ci#include <gtest/gtest_prod.h> 2148f512ceSopenharmony_ci 2248f512ceSopenharmony_ciusing namespace testing::ext; 2348f512ceSopenharmony_ciusing namespace std; 2448f512ceSopenharmony_ciusing namespace OHOS::HiviewDFX; 2548f512ceSopenharmony_cinamespace OHOS { 2648f512ceSopenharmony_cinamespace Developtools { 2748f512ceSopenharmony_cinamespace HiPerf { 2848f512ceSopenharmony_ciDebugLevel DebugLogger::debugLevel_ = DEFAULT_LOG_LEVEL; 2948f512ceSopenharmony_cibool DebugLogger::logDisabled_ = false; 3048f512ceSopenharmony_ci 3148f512ceSopenharmony_ciclass DebugLoggerTest : public testing::Test { 3248f512ceSopenharmony_cipublic: 3348f512ceSopenharmony_ci static void SetUpTestCase(void); 3448f512ceSopenharmony_ci static void TearDownTestCase(void); 3548f512ceSopenharmony_ci void SetUp(); 3648f512ceSopenharmony_ci void TearDown(); 3748f512ceSopenharmony_ci void LogLevelTest(DebugLevel testlevel, bool useStdout = false); 3848f512ceSopenharmony_ci const std::string TEST_LOG_MESSAGE = "<HELLO_TEST_LOG_MESSAGE>"; 3948f512ceSopenharmony_ci std::random_device rd_; 4048f512ceSopenharmony_ci}; 4148f512ceSopenharmony_ci 4248f512ceSopenharmony_civoid DebugLoggerTest::SetUpTestCase() 4348f512ceSopenharmony_ci{ 4448f512ceSopenharmony_ci DebugLogger::GetInstance()->Reset(); 4548f512ceSopenharmony_ci} 4648f512ceSopenharmony_ci 4748f512ceSopenharmony_civoid DebugLoggerTest::TearDownTestCase() 4848f512ceSopenharmony_ci{ 4948f512ceSopenharmony_ci DebugLogger::GetInstance()->Reset(); 5048f512ceSopenharmony_ci} 5148f512ceSopenharmony_ci 5248f512ceSopenharmony_civoid DebugLoggerTest::SetUp() {} 5348f512ceSopenharmony_ci 5448f512ceSopenharmony_civoid DebugLoggerTest::TearDown() {} 5548f512ceSopenharmony_ci 5648f512ceSopenharmony_civoid DebugLoggerTest::LogLevelTest(DebugLevel testlevel, bool useStdout) 5748f512ceSopenharmony_ci{ 5848f512ceSopenharmony_ci StdoutRecord stdoutRecord; 5948f512ceSopenharmony_ci std::string log; 6048f512ceSopenharmony_ci // backup 6148f512ceSopenharmony_ci DebugLogger::GetInstance()->exitOnFatal_ = false; 6248f512ceSopenharmony_ci DebugLevel oldLevel = DebugLogger::GetInstance()->GetLogLevel(); 6348f512ceSopenharmony_ci DebugLogger::GetInstance()->SetLogLevel(testlevel); 6448f512ceSopenharmony_ci 6548f512ceSopenharmony_ci const std::string logMessage = 6648f512ceSopenharmony_ci TEST_LOG_MESSAGE + std::to_string(rd_()) + std::to_string(testlevel); 6748f512ceSopenharmony_ci 6848f512ceSopenharmony_ci if (useStdout) { 6948f512ceSopenharmony_ci stdoutRecord.Start(); 7048f512ceSopenharmony_ci } 7148f512ceSopenharmony_ci 7248f512ceSopenharmony_ci HLOGF("%s", logMessage.c_str()); 7348f512ceSopenharmony_ci HLOGE("%s", logMessage.c_str()); 7448f512ceSopenharmony_ci HLOGW("%s", logMessage.c_str()); 7548f512ceSopenharmony_ci HLOGI("%s", logMessage.c_str()); 7648f512ceSopenharmony_ci HLOGD("%s", logMessage.c_str()); 7748f512ceSopenharmony_ci HLOGV("%s", logMessage.c_str()); 7848f512ceSopenharmony_ci HLOGM("%s", logMessage.c_str()); 7948f512ceSopenharmony_ci 8048f512ceSopenharmony_ci if (useStdout) { 8148f512ceSopenharmony_ci log = stdoutRecord.Stop(); 8248f512ceSopenharmony_ci } else { 8348f512ceSopenharmony_ci fflush(DebugLogger::GetInstance()->file_); 8448f512ceSopenharmony_ci log = ReadFileToString(DebugLogger::GetInstance()->logPath_); 8548f512ceSopenharmony_ci } 8648f512ceSopenharmony_ci 8748f512ceSopenharmony_ci // we have 7 level log 8848f512ceSopenharmony_ci // so the logout line is : (all log level - curr log level) + curr log level self 8948f512ceSopenharmony_ci if (testlevel > LEVEL_FATAL or DebugLogger::GetInstance()->enableHilog_ or 9048f512ceSopenharmony_ci DebugLogger::GetInstance()->logDisabled_) { 9148f512ceSopenharmony_ci EXPECT_EQ(SubStringCount(log, logMessage), 0u); 9248f512ceSopenharmony_ci } else { 9348f512ceSopenharmony_ci EXPECT_EQ(SubStringCount(log, logMessage), 9448f512ceSopenharmony_ci static_cast<size_t>(LEVEL_FATAL) - static_cast<size_t>(testlevel) + 1u); 9548f512ceSopenharmony_ci } 9648f512ceSopenharmony_ci if (HasFailure()) { 9748f512ceSopenharmony_ci HLOGD("LogLevelTest failed. testlevel is %d, logMessage is '%s'", testlevel, 9848f512ceSopenharmony_ci logMessage.c_str()); 9948f512ceSopenharmony_ci if (useStdout) { 10048f512ceSopenharmony_ci printf("%s", log.c_str()); 10148f512ceSopenharmony_ci } 10248f512ceSopenharmony_ci } 10348f512ceSopenharmony_ci // restore 10448f512ceSopenharmony_ci DebugLogger::GetInstance()->SetLogLevel(oldLevel); 10548f512ceSopenharmony_ci DebugLogger::GetInstance()->exitOnFatal_ = true; 10648f512ceSopenharmony_ci} 10748f512ceSopenharmony_ci 10848f512ceSopenharmony_ci/** 10948f512ceSopenharmony_ci * @tc.name: Log 11048f512ceSopenharmony_ci * @tc.desc: 11148f512ceSopenharmony_ci * @tc.type: FUNC 11248f512ceSopenharmony_ci */ 11348f512ceSopenharmony_ciHWTEST_F(DebugLoggerTest, Log, TestSize.Level1) 11448f512ceSopenharmony_ci{ 11548f512ceSopenharmony_ci DebugLogger::GetInstance()->SetMixLogOutput(true); 11648f512ceSopenharmony_ci StdoutRecord stdoutRecord; 11748f512ceSopenharmony_ci std::string log; 11848f512ceSopenharmony_ci 11948f512ceSopenharmony_ci stdoutRecord.Start(); 12048f512ceSopenharmony_ci HLOGD("123"); 12148f512ceSopenharmony_ci log = stdoutRecord.Stop(); 12248f512ceSopenharmony_ci ASSERT_TRUE(log.size() >= 4u); 12348f512ceSopenharmony_ci EXPECT_STREQ(log.substr(log.size() - 4u).c_str(), "123\n"); 12448f512ceSopenharmony_ci 12548f512ceSopenharmony_ci stdoutRecord.Start(); 12648f512ceSopenharmony_ci HLOGD("%% %% %%"); 12748f512ceSopenharmony_ci log = stdoutRecord.Stop(); 12848f512ceSopenharmony_ci EXPECT_STREQ(log.substr(log.size() - 6u).c_str(), "% % %\n"); 12948f512ceSopenharmony_ci 13048f512ceSopenharmony_ci DebugLogger::GetInstance()->SetMixLogOutput(false); 13148f512ceSopenharmony_ci 13248f512ceSopenharmony_ci stdoutRecord.Start(); 13348f512ceSopenharmony_ci printf("123\n"); 13448f512ceSopenharmony_ci log = stdoutRecord.Stop(); 13548f512ceSopenharmony_ci EXPECT_STREQ(log.c_str(), "123\n"); 13648f512ceSopenharmony_ci 13748f512ceSopenharmony_ci stdoutRecord.Start(); 13848f512ceSopenharmony_ci printf("%% %% %%\n"); 13948f512ceSopenharmony_ci log = stdoutRecord.Stop(); 14048f512ceSopenharmony_ci EXPECT_STREQ(log.c_str(), "% % %\n"); 14148f512ceSopenharmony_ci} 14248f512ceSopenharmony_ci 14348f512ceSopenharmony_ci/** 14448f512ceSopenharmony_ci * @tc.name: SetMixLogOutput 14548f512ceSopenharmony_ci * @tc.desc: 14648f512ceSopenharmony_ci * @tc.type: FUNC 14748f512ceSopenharmony_ci */ 14848f512ceSopenharmony_ciHWTEST_F(DebugLoggerTest, SetMixLogOutput, TestSize.Level1) 14948f512ceSopenharmony_ci{ 15048f512ceSopenharmony_ci DebugLogger::GetInstance()->SetMixLogOutput(true); 15148f512ceSopenharmony_ci for (int loglevel = LEVEL_MUCH; loglevel < LEVEL_MAX; loglevel++) { 15248f512ceSopenharmony_ci LogLevelTest(static_cast<DebugLevel>(loglevel), true); 15348f512ceSopenharmony_ci } 15448f512ceSopenharmony_ci 15548f512ceSopenharmony_ci DebugLogger::GetInstance()->SetMixLogOutput(false); 15648f512ceSopenharmony_ci for (int loglevel = LEVEL_MUCH; loglevel < LEVEL_MAX; loglevel++) { 15748f512ceSopenharmony_ci LogLevelTest(static_cast<DebugLevel>(loglevel), false); 15848f512ceSopenharmony_ci } 15948f512ceSopenharmony_ci} 16048f512ceSopenharmony_ci 16148f512ceSopenharmony_ci/** 16248f512ceSopenharmony_ci * @tc.name: SetLogLevel 16348f512ceSopenharmony_ci * @tc.desc: 16448f512ceSopenharmony_ci * @tc.type: FUNC 16548f512ceSopenharmony_ci */ 16648f512ceSopenharmony_ciHWTEST_F(DebugLoggerTest, SetLogLevel, TestSize.Level1) 16748f512ceSopenharmony_ci{ 16848f512ceSopenharmony_ci for (int loglevel = LEVEL_MUCH; loglevel < LEVEL_MAX; loglevel++) { 16948f512ceSopenharmony_ci LogLevelTest(static_cast<DebugLevel>(loglevel)); 17048f512ceSopenharmony_ci } 17148f512ceSopenharmony_ci} 17248f512ceSopenharmony_ci 17348f512ceSopenharmony_ci/** 17448f512ceSopenharmony_ci * @tc.name: GetInstance 17548f512ceSopenharmony_ci * @tc.desc: 17648f512ceSopenharmony_ci * @tc.type: FUNC 17748f512ceSopenharmony_ci */ 17848f512ceSopenharmony_ciHWTEST_F(DebugLoggerTest, GetInstance, TestSize.Level1) 17948f512ceSopenharmony_ci{ 18048f512ceSopenharmony_ci EXPECT_NE(DebugLogger::GetInstance(), nullptr); 18148f512ceSopenharmony_ci} 18248f512ceSopenharmony_ci 18348f512ceSopenharmony_ci/** 18448f512ceSopenharmony_ci * @tc.name: SetLogPath 18548f512ceSopenharmony_ci * @tc.desc: 18648f512ceSopenharmony_ci * @tc.type: FUNC 18748f512ceSopenharmony_ci */ 18848f512ceSopenharmony_ciHWTEST_F(DebugLoggerTest, SetLogPath, TestSize.Level1) 18948f512ceSopenharmony_ci{ 19048f512ceSopenharmony_ci const std::string logFile1 = "log1.txt"; 19148f512ceSopenharmony_ci const std::string logFile2 = "log2.txt"; 19248f512ceSopenharmony_ci const std::string logFile3 = "log3.txt"; 19348f512ceSopenharmony_ci 19448f512ceSopenharmony_ci EXPECT_EQ(access(DEFAULT_LOG_PATH.c_str(), F_OK), 0); 19548f512ceSopenharmony_ci 19648f512ceSopenharmony_ci DebugLogger::GetInstance()->SetLogPath(logFile1); 19748f512ceSopenharmony_ci EXPECT_NE(access(DEFAULT_LOG_PATH.c_str(), F_OK), 0); 19848f512ceSopenharmony_ci EXPECT_EQ(access(logFile1.c_str(), F_OK), 0); 19948f512ceSopenharmony_ci 20048f512ceSopenharmony_ci DebugLogger::GetInstance()->SetLogPath(logFile2); 20148f512ceSopenharmony_ci EXPECT_NE(access(logFile1.c_str(), F_OK), 0); 20248f512ceSopenharmony_ci EXPECT_EQ(access(logFile2.c_str(), F_OK), 0); 20348f512ceSopenharmony_ci 20448f512ceSopenharmony_ci DebugLogger::GetInstance()->SetLogPath(logFile3); 20548f512ceSopenharmony_ci EXPECT_NE(access(logFile2.c_str(), F_OK), 0); 20648f512ceSopenharmony_ci EXPECT_EQ(access(logFile3.c_str(), F_OK), 0); 20748f512ceSopenharmony_ci 20848f512ceSopenharmony_ci DebugLogger::GetInstance()->SetLogPath(DEFAULT_LOG_PATH); 20948f512ceSopenharmony_ci EXPECT_NE(access(logFile3.c_str(), F_OK), 0); 21048f512ceSopenharmony_ci EXPECT_EQ(access(DEFAULT_LOG_PATH.c_str(), F_OK), 0); 21148f512ceSopenharmony_ci} 21248f512ceSopenharmony_ci 21348f512ceSopenharmony_ci/** 21448f512ceSopenharmony_ci * @tc.name: SetLogPath 21548f512ceSopenharmony_ci * @tc.desc: 21648f512ceSopenharmony_ci * @tc.type: FUNC 21748f512ceSopenharmony_ci */ 21848f512ceSopenharmony_ciHWTEST_F(DebugLoggerTest, SetLogTags, TestSize.Level1) 21948f512ceSopenharmony_ci{ 22048f512ceSopenharmony_ci const std::string errorLogTag = "errtag"; 22148f512ceSopenharmony_ci const std::string errorLogTags = "errtag,errtag,errtag"; 22248f512ceSopenharmony_ci const std::string mixLogTags = "errtag,errtag,DebugTest,errtag"; 22348f512ceSopenharmony_ci const std::string LogTags = "DebugTest:T,DebugTest:V"; 22448f512ceSopenharmony_ci 22548f512ceSopenharmony_ci DebugLogger::GetInstance()->SetLogTags("DebugTest"); 22648f512ceSopenharmony_ci 22748f512ceSopenharmony_ci EXPECT_EQ(DebugLogger::GetInstance()->ShouldLog(LEVEL_MUCH, "DebugTest"), true); 22848f512ceSopenharmony_ci EXPECT_EQ(DebugLogger::GetInstance()->ShouldLog(LEVEL_DEBUG, "DebugTest"), true); 22948f512ceSopenharmony_ci 23048f512ceSopenharmony_ci EXPECT_EQ(DebugLogger::GetInstance()->ShouldLog(LEVEL_MUCH, errorLogTag), false); 23148f512ceSopenharmony_ci EXPECT_EQ(DebugLogger::GetInstance()->ShouldLog(LEVEL_DEBUG, errorLogTag), true); 23248f512ceSopenharmony_ci 23348f512ceSopenharmony_ci DebugLogger::GetInstance()->SetLogTags(errorLogTags); 23448f512ceSopenharmony_ci 23548f512ceSopenharmony_ci EXPECT_EQ(DebugLogger::GetInstance()->ShouldLog(LEVEL_MUCH, "DebugTest"), false); 23648f512ceSopenharmony_ci EXPECT_EQ(DebugLogger::GetInstance()->ShouldLog(LEVEL_DEBUG, "DebugTest"), true); 23748f512ceSopenharmony_ci 23848f512ceSopenharmony_ci EXPECT_EQ(DebugLogger::GetInstance()->ShouldLog(LEVEL_MUCH, errorLogTag), true); 23948f512ceSopenharmony_ci EXPECT_EQ(DebugLogger::GetInstance()->ShouldLog(LEVEL_DEBUG, errorLogTag), true); 24048f512ceSopenharmony_ci 24148f512ceSopenharmony_ci DebugLogger::GetInstance()->SetLogTags(mixLogTags); 24248f512ceSopenharmony_ci 24348f512ceSopenharmony_ci EXPECT_EQ(DebugLogger::GetInstance()->ShouldLog(LEVEL_MUCH, "DebugTest"), true); 24448f512ceSopenharmony_ci EXPECT_EQ(DebugLogger::GetInstance()->ShouldLog(LEVEL_DEBUG, "DebugTest"), true); 24548f512ceSopenharmony_ci 24648f512ceSopenharmony_ci DebugLogger::GetInstance()->SetLogTags(LogTags); 24748f512ceSopenharmony_ci 24848f512ceSopenharmony_ci EXPECT_EQ(DebugLogger::GetInstance()->ShouldLog(LEVEL_MUCH, "DebugTest"), false); 24948f512ceSopenharmony_ci EXPECT_EQ(DebugLogger::GetInstance()->ShouldLog(LEVEL_VERBOSE, "DebugTest"), true); 25048f512ceSopenharmony_ci 25148f512ceSopenharmony_ci // back to default 25248f512ceSopenharmony_ci DebugLogger::GetInstance()->SetLogTags(""); 25348f512ceSopenharmony_ci} 25448f512ceSopenharmony_ci 25548f512ceSopenharmony_ci/** 25648f512ceSopenharmony_ci * @tc.name: Disable 25748f512ceSopenharmony_ci * @tc.desc: 25848f512ceSopenharmony_ci * @tc.type: FUNC 25948f512ceSopenharmony_ci */ 26048f512ceSopenharmony_ciHWTEST_F(DebugLoggerTest, Disable, TestSize.Level1) 26148f512ceSopenharmony_ci{ 26248f512ceSopenharmony_ci HLOGD("log disabled"); 26348f512ceSopenharmony_ci DebugLogger::GetInstance()->Disable(true); 26448f512ceSopenharmony_ci ASSERT_EQ(DebugLogger::GetInstance()->logDisabled_, true); 26548f512ceSopenharmony_ci for (int loglevel = LEVEL_MUCH; loglevel < LEVEL_MAX; loglevel++) { 26648f512ceSopenharmony_ci LogLevelTest(static_cast<DebugLevel>(loglevel)); 26748f512ceSopenharmony_ci } 26848f512ceSopenharmony_ci ASSERT_EQ(DebugLogger::GetInstance()->logDisabled_, true); 26948f512ceSopenharmony_ci 27048f512ceSopenharmony_ci HLOGD("log enabled"); 27148f512ceSopenharmony_ci DebugLogger::GetInstance()->Disable(false); 27248f512ceSopenharmony_ci ASSERT_EQ(DebugLogger::GetInstance()->logDisabled_, false); 27348f512ceSopenharmony_ci for (int loglevel = LEVEL_MUCH; loglevel < LEVEL_MAX; loglevel++) { 27448f512ceSopenharmony_ci LogLevelTest(static_cast<DebugLevel>(loglevel)); 27548f512ceSopenharmony_ci } 27648f512ceSopenharmony_ci} 27748f512ceSopenharmony_ci 27848f512ceSopenharmony_ci/** 27948f512ceSopenharmony_ci * @tc.name: EnableHiLog 28048f512ceSopenharmony_ci * @tc.desc: 28148f512ceSopenharmony_ci * @tc.type: FUNC 28248f512ceSopenharmony_ci */ 28348f512ceSopenharmony_ciHWTEST_F(DebugLoggerTest, EnableHiLog, TestSize.Level1) 28448f512ceSopenharmony_ci{ 28548f512ceSopenharmony_ci#if is_ohos 28648f512ceSopenharmony_ci DebugLogger::GetInstance()->EnableHiLog(true); 28748f512ceSopenharmony_ci for (int loglevel = LEVEL_MUCH; loglevel < LEVEL_MAX; loglevel++) { 28848f512ceSopenharmony_ci LogLevelTest(static_cast<DebugLevel>(loglevel)); 28948f512ceSopenharmony_ci } 29048f512ceSopenharmony_ci DebugLogger::GetInstance()->EnableHiLog(false); 29148f512ceSopenharmony_ci#endif 29248f512ceSopenharmony_ci} 29348f512ceSopenharmony_ci} // namespace HiPerf 29448f512ceSopenharmony_ci} // namespace Developtools 29548f512ceSopenharmony_ci} // namespace OHOS 296