mirror of https://github.com/facebook/rocksdb.git
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
This commit is contained in:
parent
ad0c3747cb
commit
9a7b74954f
|
@ -5,12 +5,15 @@
|
||||||
//
|
//
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <cmath>
|
#include <cmath>
|
||||||
|
#include <iostream>
|
||||||
|
#include <fstream>
|
||||||
|
#include <iterator>
|
||||||
|
#include <algorithm>
|
||||||
#include "util/testharness.h"
|
#include "util/testharness.h"
|
||||||
#include "util/auto_roll_logger.h"
|
#include "util/auto_roll_logger.h"
|
||||||
#include "rocksdb/db.h"
|
#include "rocksdb/db.h"
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <iostream>
|
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
|
@ -240,53 +243,43 @@ TEST(AutoRollLoggerTest, CreateLoggerFromOptions) {
|
||||||
|
|
||||||
TEST(AutoRollLoggerTest, InfoLogLevel) {
|
TEST(AutoRollLoggerTest, InfoLogLevel) {
|
||||||
InitTestDb();
|
InitTestDb();
|
||||||
// the lengths of DEBUG, INFO, WARN, ERROR, FATAL respectively
|
|
||||||
const int kInfoLogLevelNameLens[5] = {5, 4, 4, 5, 5};
|
|
||||||
|
|
||||||
size_t log_size = 8192;
|
size_t log_size = 8192;
|
||||||
std::unique_ptr<AutoRollLogger> logger_guard(
|
int log_lines = 0;
|
||||||
new AutoRollLogger(Env::Default(), kTestDir, "", log_size, 0));
|
// an extra-scope to force the AutoRollLogger to flush the log file when it
|
||||||
auto logger = logger_guard.get();
|
// becomes out of scope.
|
||||||
|
{
|
||||||
int message_length = kSampleMessage.length();
|
AutoRollLogger logger(Env::Default(), kTestDir, "", log_size, 0);
|
||||||
int log_length = 0;
|
|
||||||
int total_logname_length = 0;
|
|
||||||
for (int log_level = InfoLogLevel::FATAL; log_level >= InfoLogLevel::DEBUG;
|
for (int log_level = InfoLogLevel::FATAL; log_level >= InfoLogLevel::DEBUG;
|
||||||
log_level--) {
|
log_level--) {
|
||||||
logger->SetInfoLogLevel((InfoLogLevel)log_level);
|
logger.SetInfoLogLevel((InfoLogLevel)log_level);
|
||||||
total_logname_length += kInfoLogLevelNameLens[log_level];
|
|
||||||
for (int log_type = InfoLogLevel::DEBUG; log_type <= InfoLogLevel::FATAL;
|
for (int log_type = InfoLogLevel::DEBUG; log_type <= InfoLogLevel::FATAL;
|
||||||
log_type++) {
|
log_type++) {
|
||||||
// log messages with log level smaller than log_level will not be logged.
|
// log messages with log level smaller than log_level will not be
|
||||||
LogMessage((InfoLogLevel)log_type, logger, kSampleMessage.c_str());
|
// logged.
|
||||||
|
LogMessage((InfoLogLevel)log_type, &logger, kSampleMessage.c_str());
|
||||||
}
|
}
|
||||||
// 44 is the length of the message excluding the actual
|
log_lines += InfoLogLevel::FATAL - log_level + 1;
|
||||||
// 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;
|
for (int log_level = InfoLogLevel::FATAL; log_level >= InfoLogLevel::DEBUG;
|
||||||
log_level--) {
|
log_level--) {
|
||||||
logger->SetInfoLogLevel((InfoLogLevel)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.
|
// again, messages with level smaller than log_level will not be logged.
|
||||||
Debug(logger, "%s", kSampleMessage.c_str());
|
Debug(&logger, "%s", kSampleMessage.c_str());
|
||||||
Info(logger, "%s", kSampleMessage.c_str());
|
Info(&logger, "%s", kSampleMessage.c_str());
|
||||||
Warn(logger, "%s", kSampleMessage.c_str());
|
Warn(&logger, "%s", kSampleMessage.c_str());
|
||||||
Error(logger, "%s", kSampleMessage.c_str());
|
Error(&logger, "%s", kSampleMessage.c_str());
|
||||||
Fatal(logger, "%s", kSampleMessage.c_str());
|
Fatal(&logger, "%s", kSampleMessage.c_str());
|
||||||
// 44 is the length of the message excluding the actual
|
log_lines += InfoLogLevel::FATAL - log_level + 1;
|
||||||
// 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<char>(inFile),
|
||||||
|
std::istreambuf_iterator<char>(), '\n');
|
||||||
|
ASSERT_EQ(log_lines, lines);
|
||||||
|
inFile.close();
|
||||||
|
}
|
||||||
|
|
||||||
int OldLogFileCount(const string& dir) {
|
int OldLogFileCount(const string& dir) {
|
||||||
std::vector<std::string> files;
|
std::vector<std::string> files;
|
||||||
|
|
Loading…
Reference in New Issue