From 24d6b776bc7f5608d7e553f361eb79f2dcf6d7f7 Mon Sep 17 00:00:00 2001 From: Adam Simpkins Date: Tue, 9 Jan 2018 20:08:35 -0800 Subject: [PATCH] logging: rename the `DEBUG` log level to `DBG` Summary: I ran into some open source projects that define `DEBUG` as a preprocessor macro on the compiler command line. (They effectively defined it as the opposite of `NDEBUG`.) Doing some Google searches revealed that there are a number of projects that appear to use this as a macro. Therefore this diff renames the `DEBUG` log level enum value to `DBG` to avoid potentially conflicting with projects that do use `DEBUG` as a macro name. I did keep the behavior that `logLevelToString()` returns "DEBUG" for an input of `LogLevel::DBG`. Reviewed By: yfeldblum Differential Revision: D6690465 fbshipit-source-id: 35bb1698afb45eb670e60c192f21390cbf09331d --- folly/experimental/logging/LogLevel.cpp | 10 +++++----- folly/experimental/logging/LogLevel.h | 12 ++++++++++- .../logging/test/ConfigUpdateTest.cpp | 6 +++--- .../logging/test/LogCategoryTest.cpp | 2 +- .../logging/test/LogLevelTest.cpp | 20 ++++++++++--------- .../experimental/logging/test/LoggerTest.cpp | 2 +- .../experimental/logging/test/PrintfTest.cpp | 2 +- 7 files changed, 33 insertions(+), 21 deletions(-) diff --git a/folly/experimental/logging/LogLevel.cpp b/folly/experimental/logging/LogLevel.cpp index 80844608..2858957f 100644 --- a/folly/experimental/logging/LogLevel.cpp +++ b/folly/experimental/logging/LogLevel.cpp @@ -34,7 +34,7 @@ LogLevel stringToLogLevel(StringPiece name) { // If the string is of the form "LogLevel::foo" or "LogLevel(foo)" // strip it down just to "foo". This makes sure we can process both - // the "LogLevel::DEBUG" and "LogLevel(1234)" formats produced by + // the "LogLevel::WARN" and "LogLevel(1234)" formats produced by // logLevelToString(). constexpr StringPiece lowercasePrefix{"loglevel::"}; constexpr StringPiece wrapperPrefix{"loglevel("}; @@ -49,8 +49,8 @@ LogLevel stringToLogLevel(StringPiece name) { return LogLevel::UNINITIALIZED; } else if (lowerName == "none") { return LogLevel::NONE; - } else if (lowerName == "debug") { - return LogLevel::DEBUG; + } else if (lowerName == "debug" || lowerName == "dbg") { + return LogLevel::DBG; } else if (lowerName == "info") { return LogLevel::INFO; } else if (lowerName == "warn" || lowerName == "warning") { @@ -90,7 +90,7 @@ string logLevelToString(LogLevel level) { return "UNINITIALIZED"; } else if (level == LogLevel::NONE) { return "NONE"; - } else if (level == LogLevel::DEBUG) { + } else if (level == LogLevel::DBG) { return "DEBUG"; } else if (level == LogLevel::INFO) { return "INFO"; @@ -107,7 +107,7 @@ string logLevelToString(LogLevel level) { } if (static_cast(level) <= static_cast(LogLevel::DBG0) && - static_cast(level) > static_cast(LogLevel::DEBUG)) { + static_cast(level) > static_cast(LogLevel::DBG)) { auto num = static_cast(LogLevel::DBG0) - static_cast(level); return folly::to("DBG", num); diff --git a/folly/experimental/logging/LogLevel.h b/folly/experimental/logging/LogLevel.h index 605da062..f066fb50 100644 --- a/folly/experimental/logging/LogLevel.h +++ b/folly/experimental/logging/LogLevel.h @@ -40,7 +40,17 @@ enum class LogLevel : uint32_t { NONE = 1, MIN_LEVEL = 1, - DEBUG = 900, + // "DBG" is the lowest (aka most verbose) debug log level. + // This level is intended to be primarily used in log category settings. + // In your code it is usually better to use one of the finer-grained DBGn + // levels. In your log category settings you can then set the log category + // level to a specific DBGn level, or to to main DBG level to enable all DBGn + // messages. + // + // This is named "DBG" rather than "DEBUG" since some open source projects + // define "DEBUG" as a preprocessor macro. + DBG = 900, + DBG0 = 1000, DBG1 = 999, DBG2 = 998, diff --git a/folly/experimental/logging/test/ConfigUpdateTest.cpp b/folly/experimental/logging/test/ConfigUpdateTest.cpp index c1415c42..d3622530 100644 --- a/folly/experimental/logging/test/ConfigUpdateTest.cpp +++ b/folly/experimental/logging/test/ConfigUpdateTest.cpp @@ -101,11 +101,11 @@ TEST(ConfigUpdate, updateLogLevels) { parseLogConfig("sys=warn,foo.test=debug,foo.test.stuff=warn")); EXPECT_EQ(LogLevel::WARN, db.getCategory("sys")->getLevel()); EXPECT_EQ(LogLevel::WARN, db.getCategory("sys")->getEffectiveLevel()); - EXPECT_EQ(LogLevel::DEBUG, db.getCategory("foo.test")->getLevel()); - EXPECT_EQ(LogLevel::DEBUG, db.getCategory("foo.test")->getEffectiveLevel()); + EXPECT_EQ(LogLevel::DBG, db.getCategory("foo.test")->getLevel()); + EXPECT_EQ(LogLevel::DBG, db.getCategory("foo.test")->getEffectiveLevel()); EXPECT_EQ(LogLevel::WARN, db.getCategory("foo.test.stuff")->getLevel()); EXPECT_EQ( - LogLevel::DEBUG, db.getCategory("foo.test.stuff")->getEffectiveLevel()); + LogLevel::DBG, db.getCategory("foo.test.stuff")->getEffectiveLevel()); EXPECT_EQ(LogLevel::DBG5, db.getCategory("foo.bar")->getEffectiveLevel()); } diff --git a/folly/experimental/logging/test/LogCategoryTest.cpp b/folly/experimental/logging/test/LogCategoryTest.cpp index 2509960b..9b734c5b 100644 --- a/folly/experimental/logging/test/LogCategoryTest.cpp +++ b/folly/experimental/logging/test/LogCategoryTest.cpp @@ -79,7 +79,7 @@ TEST(LogCategory, effectiveLevel) { void testNumHandlers(size_t numHandlers) { SCOPED_TRACE(folly::to("num_handlers= ", numHandlers)); LoggerDB db{LoggerDB::TESTING}; - db.setLevel("", LogLevel::DEBUG); + db.setLevel("", LogLevel::DBG); // Create the requested number of handlers for the foo.bar category Logger foobar{&db, "foo.bar"}; diff --git a/folly/experimental/logging/test/LogLevelTest.cpp b/folly/experimental/logging/test/LogLevelTest.cpp index dc9ec69d..8a652665 100644 --- a/folly/experimental/logging/test/LogLevelTest.cpp +++ b/folly/experimental/logging/test/LogLevelTest.cpp @@ -31,9 +31,11 @@ TEST(LogLevel, fromString) { EXPECT_EQ(LogLevel::NONE, stringToLogLevel("NoNe")); EXPECT_EQ(LogLevel::NONE, stringToLogLevel("LogLevel::none")); - EXPECT_EQ(LogLevel::DEBUG, stringToLogLevel("debug")); - EXPECT_EQ(LogLevel::DEBUG, stringToLogLevel("dEBug")); - EXPECT_EQ(LogLevel::DEBUG, stringToLogLevel("loglevel::dEBug")); + EXPECT_EQ(LogLevel::DBG, stringToLogLevel("debug")); + EXPECT_EQ(LogLevel::DBG, stringToLogLevel("dEBug")); + EXPECT_EQ(LogLevel::DBG, stringToLogLevel("Dbg")); + EXPECT_EQ(LogLevel::DBG, stringToLogLevel("loglevel::dEBug")); + EXPECT_EQ(LogLevel::DBG, stringToLogLevel("loglevel::DBG")); EXPECT_EQ(LogLevel::INFO, stringToLogLevel("info")); EXPECT_EQ(LogLevel::INFO, stringToLogLevel("INFO")); @@ -69,12 +71,12 @@ TEST(LogLevel, fromString) { EXPECT_EQ(LogLevel::DBG5, stringToLogLevel("dbg5")); EXPECT_EQ(LogLevel::DBG5, stringToLogLevel("DBG5")); EXPECT_EQ(LogLevel::DBG9, stringToLogLevel("DBG9")); - EXPECT_EQ(LogLevel::DEBUG + 1, stringToLogLevel("DBG99")); - EXPECT_EQ(LogLevel::DEBUG, stringToLogLevel("900")); - EXPECT_EQ(LogLevel::DEBUG, stringToLogLevel("LogLevel(900)")); + EXPECT_EQ(LogLevel::DBG + 1, stringToLogLevel("DBG99")); + EXPECT_EQ(LogLevel::DBG, stringToLogLevel("900")); + EXPECT_EQ(LogLevel::DBG, stringToLogLevel("LogLevel(900)")); EXPECT_THROW(stringToLogLevel("foobar"), std::range_error); - EXPECT_THROW(stringToLogLevel("dbg"), std::range_error); + EXPECT_THROW(stringToLogLevel("dbgx"), std::range_error); EXPECT_THROW(stringToLogLevel("dbgxyz"), std::range_error); EXPECT_THROW(stringToLogLevel("dbg-1"), std::range_error); EXPECT_THROW(stringToLogLevel("dbg12345"), std::range_error); @@ -87,7 +89,7 @@ TEST(LogLevel, toString) { EXPECT_EQ("INFO", logLevelToString(LogLevel::INFO)); EXPECT_EQ("WARN", logLevelToString(LogLevel::WARN)); EXPECT_EQ("WARN", logLevelToString(LogLevel::WARNING)); - EXPECT_EQ("DEBUG", logLevelToString(LogLevel::DEBUG)); + EXPECT_EQ("DEBUG", logLevelToString(LogLevel::DBG)); EXPECT_EQ("ERR", logLevelToString(LogLevel::ERR)); EXPECT_EQ("CRITICAL", logLevelToString(LogLevel::CRITICAL)); EXPECT_EQ("DFATAL", logLevelToString(LogLevel::DFATAL)); @@ -117,7 +119,7 @@ TEST(LogLevel, toStringAndBack) { // Check all of the named levels checkLevel(LogLevel::UNINITIALIZED); checkLevel(LogLevel::NONE); - checkLevel(LogLevel::DEBUG); + checkLevel(LogLevel::DBG); checkLevel(LogLevel::DBG0); checkLevel(LogLevel::DBG1); checkLevel(LogLevel::DBG2); diff --git a/folly/experimental/logging/test/LoggerTest.cpp b/folly/experimental/logging/test/LoggerTest.cpp index abd740b6..8172abcb 100644 --- a/folly/experimental/logging/test/LoggerTest.cpp +++ b/folly/experimental/logging/test/LoggerTest.cpp @@ -31,7 +31,7 @@ class LoggerTest : public ::testing::Test { handler_ = make_shared(); category->addHandler(handler_); - category->setLevel(LogLevel::DEBUG, true); + category->setLevel(LogLevel::DBG, true); } static StringPiece pathBasename(StringPiece path) { diff --git a/folly/experimental/logging/test/PrintfTest.cpp b/folly/experimental/logging/test/PrintfTest.cpp index 39b6819a..1f7fbe1e 100644 --- a/folly/experimental/logging/test/PrintfTest.cpp +++ b/folly/experimental/logging/test/PrintfTest.cpp @@ -27,7 +27,7 @@ TEST(PrintfTest, printfStyleMacros) { auto handler = make_shared(); category->addHandler(handler); - category->setLevel(LogLevel::DEBUG, true); + category->setLevel(LogLevel::DBG, true); Logger foo{&db, "test.foo.bar"}; Logger foobar{&db, "test.foo.bar"}; -- 2.34.1