From 9a7b74954f9ffb7246d515e44239bf3054a70dfc Mon Sep 17 00:00:00 2001 From: Yueh-Hsuan Chiang Date: Thu, 27 Feb 2014 14:00:10 -0800 Subject: [PATCH] Refine the checks in InfoLogLevel test. Summary: InfoLogLevel test now checks the number of lines of the output log file instead of the number of bytes in the log file. This diff fixes the issue that the previous InfoLogLevel test in auto_roll_logger_test passed in make check but fails when valgrind is used. Test Plan: run with make check and valgrind. Reviewers: kailiu Reviewed By: kailiu CC: leveldb Differential Revision: https://reviews.facebook.net/D16407 --- util/auto_roll_logger_test.cc | 79 ++++++++++++++++------------------- 1 file changed, 36 insertions(+), 43 deletions(-) diff --git a/util/auto_roll_logger_test.cc b/util/auto_roll_logger_test.cc index 3cf9cb453..423ef416b 100755 --- a/util/auto_roll_logger_test.cc +++ b/util/auto_roll_logger_test.cc @@ -5,12 +5,15 @@ // #include #include +#include +#include +#include +#include #include "util/testharness.h" #include "util/auto_roll_logger.h" #include "rocksdb/db.h" #include #include -#include using namespace std; @@ -240,52 +243,42 @@ TEST(AutoRollLoggerTest, CreateLoggerFromOptions) { TEST(AutoRollLoggerTest, InfoLogLevel) { InitTestDb(); - // the lengths of DEBUG, INFO, WARN, ERROR, FATAL respectively - const int kInfoLogLevelNameLens[5] = {5, 4, 4, 5, 5}; size_t log_size = 8192; - std::unique_ptr logger_guard( - new AutoRollLogger(Env::Default(), kTestDir, "", log_size, 0)); - auto logger = logger_guard.get(); - - int message_length = kSampleMessage.length(); - int log_length = 0; - int total_logname_length = 0; - for (int log_level = InfoLogLevel::FATAL; log_level >= InfoLogLevel::DEBUG; - log_level--) { - logger->SetInfoLogLevel((InfoLogLevel)log_level); - total_logname_length += kInfoLogLevelNameLens[log_level]; - for (int log_type = InfoLogLevel::DEBUG; log_type <= InfoLogLevel::FATAL; - log_type++) { - // log messages with log level smaller than log_level will not be logged. - LogMessage((InfoLogLevel)log_type, logger, kSampleMessage.c_str()); + int log_lines = 0; + // an extra-scope to force the AutoRollLogger to flush the log file when it + // becomes out of scope. + { + AutoRollLogger logger(Env::Default(), kTestDir, "", log_size, 0); + for (int log_level = InfoLogLevel::FATAL; log_level >= InfoLogLevel::DEBUG; + log_level--) { + logger.SetInfoLogLevel((InfoLogLevel)log_level); + for (int log_type = InfoLogLevel::DEBUG; log_type <= InfoLogLevel::FATAL; + log_type++) { + // log messages with log level smaller than log_level will not be + // logged. + LogMessage((InfoLogLevel)log_type, &logger, kSampleMessage.c_str()); + } + log_lines += InfoLogLevel::FATAL - log_level + 1; + } + for (int log_level = InfoLogLevel::FATAL; log_level >= InfoLogLevel::DEBUG; + log_level--) { + logger.SetInfoLogLevel((InfoLogLevel)log_level); + + // again, messages with level smaller than log_level will not be logged. + Debug(&logger, "%s", kSampleMessage.c_str()); + Info(&logger, "%s", kSampleMessage.c_str()); + Warn(&logger, "%s", kSampleMessage.c_str()); + Error(&logger, "%s", kSampleMessage.c_str()); + Fatal(&logger, "%s", kSampleMessage.c_str()); + log_lines += InfoLogLevel::FATAL - log_level + 1; } - // 44 is the length of the message excluding the actual - // message and log name. - log_length += (message_length + 44) * (InfoLogLevel::FATAL - log_level + 1); - log_length += total_logname_length; - ASSERT_EQ(logger->GetLogFileSize(), log_length); - } - - // rerun the test but using different log functions. - total_logname_length = 0; - for (int log_level = InfoLogLevel::FATAL; log_level >= InfoLogLevel::DEBUG; - log_level--) { - logger->SetInfoLogLevel((InfoLogLevel)log_level); - total_logname_length += kInfoLogLevelNameLens[log_level]; - - // again, messages with level smaller than log_level will not be logged. - Debug(logger, "%s", kSampleMessage.c_str()); - Info(logger, "%s", kSampleMessage.c_str()); - Warn(logger, "%s", kSampleMessage.c_str()); - Error(logger, "%s", kSampleMessage.c_str()); - Fatal(logger, "%s", kSampleMessage.c_str()); - // 44 is the length of the message excluding the actual - // message and log name. - log_length += (message_length + 44) * (InfoLogLevel::FATAL - log_level + 1); - log_length += total_logname_length; - ASSERT_EQ(logger->GetLogFileSize(), log_length); } + std::ifstream inFile(AutoRollLoggerTest::kLogFile.c_str()); + int lines = std::count(std::istreambuf_iterator(inFile), + std::istreambuf_iterator(), '\n'); + ASSERT_EQ(log_lines, lines); + inFile.close(); } int OldLogFileCount(const string& dir) {