Fix the warning introduced by auto_roll_logger_test

Summary: Fix the warning [-Werror=format-security] and [-Werror=unused-result].

Test Plan:
enforced the Werror and run make

Task ID: 2101673

Blame Rev:

Reviewers: heyongqiang

Differential Revision: https://reviews.facebook.net/D8553
main
Kai Liu 12 years ago
parent f02db1c118
commit aaa0cbb97a
  1. 81
      util/auto_roll_logger_test.cc

@ -15,60 +15,11 @@ using namespace std;
namespace leveldb { namespace leveldb {
// annoymous namespace for test facilities.
namespace {
// This is a fake logger that keeps counting
// the size of logged messages.
class MockLogger: public Logger {
public:
MockLogger(): log_size_(0) { }
// In our simple cases, we only use `format` parameter.
void Logv(const char* format, va_list ap) {
log_size_ += strlen(format);
}
virtual size_t GetLogFileSize() const {
return log_size_;
}
private:
size_t log_size_;
};
// A fake Env class that can create MockLogger instance.
class MockEnv: public EnvWrapper {
public:
static Env* MakeMockEnv() {
Env* target = Env::Default();
return new MockEnv(target);
}
~MockEnv() {
}
virtual Status NewLogger(const std::string& fname, shared_ptr<Logger>* result) {
result->reset(new MockLogger());
return Status::OK();
}
virtual Status RenameFile(const std::string& src,
const std::string& target) {
// do nothing
return Status::OK();
}
private:
MockEnv(Env* target): EnvWrapper(target), target_(target) { }
Env* target_;
};
} // end anonymous namespace
class AutoRollLoggerTest { class AutoRollLoggerTest {
public: public:
static void InitTestDb() { static void InitTestDb() {
string deleteCmd = "rm -rf " + kTestDir; string deleteCmd = "rm -rf " + kTestDir;
system(deleteCmd.c_str()); ASSERT_TRUE(system(deleteCmd.c_str()) == 0);
Env::Default()->CreateDir(kTestDir); Env::Default()->CreateDir(kTestDir);
} }
@ -83,7 +34,6 @@ class AutoRollLoggerTest {
static const string kTestDir; static const string kTestDir;
static const string kLogFile; static const string kLogFile;
static Env* env; static Env* env;
static Env* mockEnv;
}; };
const string AutoRollLoggerTest::kSampleMessage( const string AutoRollLoggerTest::kSampleMessage(
@ -92,9 +42,16 @@ const string AutoRollLoggerTest::kTestDir(
test::TmpDir() + "/db_log_test"); test::TmpDir() + "/db_log_test");
const string AutoRollLoggerTest::kLogFile( const string AutoRollLoggerTest::kLogFile(
test::TmpDir() + "/db_log_test/LOG"); test::TmpDir() + "/db_log_test/LOG");
Env* AutoRollLoggerTest::mockEnv = MockEnv::MakeMockEnv();
Env* AutoRollLoggerTest::env = Env::Default(); Env* AutoRollLoggerTest::env = Env::Default();
// In this test we only want to Log some simple log message with
// no format. LogMessage() provides such a simple interface and
// avoids the [format-security] warning which occurs when you
// call Log(logger, log_message) directly.
void LogMessage(Logger* logger, const char* message) {
Log(logger, "%s", message);
}
void GetFileCreateTime(const std::string& fname, uint64_t* file_ctime) { void GetFileCreateTime(const std::string& fname, uint64_t* file_ctime) {
struct stat s; struct stat s;
if (stat(fname.c_str(), &s) != 0) { if (stat(fname.c_str(), &s) != 0) {
@ -108,19 +65,19 @@ void AutoRollLoggerTest::RollLogFileBySizeTest(AutoRollLogger* logger,
const string& log_message) { const string& log_message) {
// measure the size of each message, which is supposed // measure the size of each message, which is supposed
// to be equal or greater than log_message.size() // to be equal or greater than log_message.size()
Log(logger, log_message.c_str()); LogMessage(logger, log_message.c_str());
size_t message_size = logger->GetLogFileSize(); size_t message_size = logger->GetLogFileSize();
size_t current_log_size = message_size; size_t current_log_size = message_size;
// Test the cases when the log file will not be rolled. // Test the cases when the log file will not be rolled.
while (current_log_size + message_size < log_max_size) { while (current_log_size + message_size < log_max_size) {
Log(logger, log_message.c_str()); LogMessage(logger, log_message.c_str());
current_log_size += message_size; current_log_size += message_size;
ASSERT_EQ(current_log_size, logger->GetLogFileSize()); ASSERT_EQ(current_log_size, logger->GetLogFileSize());
} }
// Now the log file will be rolled // Now the log file will be rolled
Log(logger, log_message.c_str()); LogMessage(logger, log_message.c_str());
ASSERT_TRUE(0 == logger->GetLogFileSize()); ASSERT_TRUE(0 == logger->GetLogFileSize());
} }
@ -136,7 +93,7 @@ uint64_t AutoRollLoggerTest::RollLogFileByTimeTest(
// -- Write to the log for several times, which is supposed // -- Write to the log for several times, which is supposed
// to be finished before time. // to be finished before time.
for (int i = 0; i < 10; ++i) { for (int i = 0; i < 10; ++i) {
Log(logger, log_message.c_str()); LogMessage(logger, log_message.c_str());
ASSERT_OK(logger->GetStatus()); ASSERT_OK(logger->GetStatus());
// Make sure we always write to the same log file (by // Make sure we always write to the same log file (by
// checking the create time); // checking the create time);
@ -150,7 +107,7 @@ uint64_t AutoRollLoggerTest::RollLogFileByTimeTest(
// -- Make the log file expire // -- Make the log file expire
sleep(time); sleep(time);
Log(logger, log_message.c_str()); LogMessage(logger, log_message.c_str());
// At this time, the new log file should be created. // At this time, the new log file should be created.
GetFileCreateTime(kLogFile, &actual_create_time); GetFileCreateTime(kLogFile, &actual_create_time);
@ -162,10 +119,10 @@ uint64_t AutoRollLoggerTest::RollLogFileByTimeTest(
} }
TEST(AutoRollLoggerTest, RollLogFileBySize) { TEST(AutoRollLoggerTest, RollLogFileBySize) {
size_t log_max_size = 1024; size_t log_max_size = 1024 * 5;
AutoRollLogger* logger = new AutoRollLogger( AutoRollLogger* logger = new AutoRollLogger(
mockEnv, kTestDir, "", log_max_size, 0); Env::Default(), kTestDir, "", log_max_size, 0);
RollLogFileBySizeTest(logger, log_max_size, RollLogFileBySizeTest(logger, log_max_size,
kSampleMessage + ":RollLogFileBySize"); kSampleMessage + ":RollLogFileBySize");
@ -204,7 +161,7 @@ TEST(AutoRollLoggerTest,
AutoRollLogger* logger = new AutoRollLogger( AutoRollLogger* logger = new AutoRollLogger(
Env::Default(), kTestDir, "", log_size, 0); Env::Default(), kTestDir, "", log_size, 0);
Log(logger, kSampleMessage.c_str()); LogMessage(logger, kSampleMessage.c_str());
ASSERT_GT(logger->GetLogFileSize(), kZero); ASSERT_GT(logger->GetLogFileSize(), kZero);
delete logger; delete logger;
@ -239,7 +196,7 @@ TEST(AutoRollLoggerTest, CreateLoggerFromOptions) {
// Normal logger // Normal logger
ASSERT_OK(CreateLoggerFromOptions(kTestDir, "", env, options, &logger)); ASSERT_OK(CreateLoggerFromOptions(kTestDir, "", env, options, &logger));
ASSERT_TRUE(dynamic_cast<PosixLogger*>(logger.get()) != NULL); ASSERT_TRUE(dynamic_cast<PosixLogger*>(logger.get()));
// Only roll by size // Only roll by size
InitTestDb(); InitTestDb();
@ -247,7 +204,7 @@ TEST(AutoRollLoggerTest, CreateLoggerFromOptions) {
ASSERT_OK(CreateLoggerFromOptions(kTestDir, "", env, options, &logger)); ASSERT_OK(CreateLoggerFromOptions(kTestDir, "", env, options, &logger));
AutoRollLogger* auto_roll_logger = AutoRollLogger* auto_roll_logger =
dynamic_cast<AutoRollLogger*>(logger.get()); dynamic_cast<AutoRollLogger*>(logger.get());
ASSERT_TRUE(auto_roll_logger != NULL); ASSERT_TRUE(auto_roll_logger);
RollLogFileBySizeTest( RollLogFileBySizeTest(
auto_roll_logger, options.max_log_file_size, auto_roll_logger, options.max_log_file_size,
kSampleMessage + ":CreateLoggerFromOptions - size"); kSampleMessage + ":CreateLoggerFromOptions - size");

Loading…
Cancel
Save