logging: rename the `DEBUG` log level to `DBG`
authorAdam Simpkins <simpkins@fb.com>
Wed, 10 Jan 2018 04:08:35 +0000 (20:08 -0800)
committerFacebook Github Bot <facebook-github-bot@users.noreply.github.com>
Wed, 10 Jan 2018 04:26:36 +0000 (20:26 -0800)
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
folly/experimental/logging/LogLevel.h
folly/experimental/logging/test/ConfigUpdateTest.cpp
folly/experimental/logging/test/LogCategoryTest.cpp
folly/experimental/logging/test/LogLevelTest.cpp
folly/experimental/logging/test/LoggerTest.cpp
folly/experimental/logging/test/PrintfTest.cpp

index 8084460..2858957 100644 (file)
@@ -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<uint32_t>(level) <= static_cast<uint32_t>(LogLevel::DBG0) &&
-      static_cast<uint32_t>(level) > static_cast<uint32_t>(LogLevel::DEBUG)) {
+      static_cast<uint32_t>(level) > static_cast<uint32_t>(LogLevel::DBG)) {
     auto num =
         static_cast<uint32_t>(LogLevel::DBG0) - static_cast<uint32_t>(level);
     return folly::to<string>("DBG", num);
index 605da06..f066fb5 100644 (file)
@@ -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,
index c1415c4..d362253 100644 (file)
@@ -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());
 }
 
index 2509960..9b734c5 100644 (file)
@@ -79,7 +79,7 @@ TEST(LogCategory, effectiveLevel) {
 void testNumHandlers(size_t numHandlers) {
   SCOPED_TRACE(folly::to<string>("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"};
index dc9ec69..8a65266 100644 (file)
@@ -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);
index abd740b..8172abc 100644 (file)
@@ -31,7 +31,7 @@ class LoggerTest : public ::testing::Test {
 
     handler_ = make_shared<TestLogHandler>();
     category->addHandler(handler_);
-    category->setLevel(LogLevel::DEBUG, true);
+    category->setLevel(LogLevel::DBG, true);
   }
 
   static StringPiece pathBasename(StringPiece path) {
index 39b6819..1f7fbe1 100644 (file)
@@ -27,7 +27,7 @@ TEST(PrintfTest, printfStyleMacros) {
 
   auto handler = make_shared<TestLogHandler>();
   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"};