Skip to content

Commit

Permalink
Bug 1165515 - Part 13-1: Add log level enum class. r=froydnj
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
EricRahm committed Jun 3, 2015
1 parent 9773e1b commit 9e0477f
Showing 1 changed file with 46 additions and 7 deletions.
53 changes: 46 additions & 7 deletions xpcom/glue/Logging.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

// 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 PR_LOG
#define MOZ_LOG_TEST(_module,_level) mozilla::detail::log_test(_module, _level)

// 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))
#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

0 comments on commit 9e0477f

Please sign in to comment.