From 7fe9ffc48bdd76a46b92ebda3e068cea6400bc58 Mon Sep 17 00:00:00 2001 From: Eric Rahm Date: Mon, 1 Jun 2015 14:31:01 -0700 Subject: [PATCH] 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 | 53 ++++++++++++++++++++++++++++++++++++++------ 1 file changed, 46 insertions(+), 7 deletions(-) diff --git a/xpcom/glue/Logging.h b/xpcom/glue/Logging.h index 192a33b95942..0e9c35f9bcca 100644 --- a/xpcom/glue/Logging.h +++ b/xpcom/glue/Logging.h @@ -9,20 +9,59 @@ #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) +namespace mozilla { -#define MOZ_LOG PR_LOG +// 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, +}; -// 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)) +namespace detail { + +inline bool log_test(const PRLogModuleInfo* module, LogLevel level) { + MOZ_ASSERT(level != LogLevel::Disabled); + return module && module->level >= static_cast(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