Bug 1165515 - Part 13-1: Add log level enum class. r=froydnj
☠☠ backed out by 3c8ed81098dd ☠ ☠
authorEric Rahm <erahm@mozilla.com>
Mon, 01 Jun 2015 22:17:31 -0700
changeset 277469 b668b617bef20d4cfeec8ec105c5c6200b7496ef
parent 277468 d0916e1283a23b42d2ed9a2e97bcfd23a6d625b9
child 277470 7c3b45a47811b55f4e973d996dd149c5d575721b
push id4932
push userjlund@mozilla.com
push dateMon, 10 Aug 2015 18:23:06 +0000
treeherdermozilla-beta@6dd5a4f5f745 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersfroydnj
bugs1165515
milestone41.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1165515 - Part 13-1: Add log level enum class. r=froydnj This adds the mozilla::LogLevel enum class. Additionaly a log_test function is added to use rather than a macro, this allows us to enforce only mozilla::LogLevel is passed into the function.
xpcom/glue/Logging.h
--- a/xpcom/glue/Logging.h
+++ b/xpcom/glue/Logging.h
@@ -4,25 +4,64 @@
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #ifndef mozilla_logging_h
 #define mozilla_logging_h
 
 #include "prlog.h"
 
+#include "mozilla/Assertions.h"
+
 // This file is a placeholder for a replacement to the NSPR logging framework
 // that is defined in prlog.h. Currently it is just a pass through, but as
 // work progresses more functionality will be swapped out in favor of
 // mozilla logging implementations.
 
-#define PR_LOG_INFO PR_LOG_DEBUG
-#define PR_LOG_VERBOSE (PR_LOG_DEBUG + 1)
-
-#define MOZ_LOG PR_LOG
+namespace mozilla {
 
-// Tests if a module has enabled the given log level.
-// NB: _module can be null.
-#define MOZ_LOG_TEST(_module, _level) \
-  ((_module) && (_module)->level >= (_level))
+// While not a 100% mapping to PR_LOG's numeric values, mozilla::LogLevel does
+// maintain a direct mapping for the Disabled, Debug and Verbose levels.
+//
+// Mappings of LogLevel to PR_LOG's numeric values:
+//
+//   +---------+------------------+-----------------+
+//   | Numeric | NSPR Logging     | Mozilla Logging |
+//   +---------+------------------+-----------------+
+//   |       0 | PR_LOG_NONE      | Disabled        |
+//   |       1 | PR_LOG_ALWAYS    | Error           |
+//   |       2 | PR_LOG_ERROR     | Warning         |
+//   |       3 | PR_LOG_WARNING   | Info            |
+//   |       4 | PR_LOG_DEBUG     | Debug           |
+//   |       5 | PR_LOG_DEBUG + 1 | Verbose         |
+//   +---------+------------------+-----------------+
+//
+enum class LogLevel {
+  Disabled = 0,
+  Error,
+  Warning,
+  Info,
+  Debug,
+  Verbose,
+};
+
+namespace detail {
+
+inline bool log_test(const PRLogModuleInfo* module, LogLevel level) {
+  MOZ_ASSERT(level != LogLevel::Disabled);
+  return module && module->level >= static_cast<int>(level);
+}
+
+}
+
+}
+
+#define MOZ_LOG_TEST(_module,_level) mozilla::detail::log_test(_module, _level)
+
+#define MOZ_LOG(_module,_level,_args)     \
+  PR_BEGIN_MACRO             \
+    if (MOZ_LOG_TEST(_module,_level)) { \
+      PR_LogPrint _args;         \
+    }                     \
+  PR_END_MACRO
 
 #endif // mozilla_logging_h