#include <cstdint>
#include <iosfwd>
#include <string>
+#include <type_traits>
+#include <folly/Portability.h>
#include <folly/Range.h>
namespace folly {
* DBG1 is one level higher of verbosity, etc.
*/
enum class LogLevel : uint32_t {
- NONE = 0,
+ UNINITIALIZED = 0,
+ NONE = 1,
+ MIN_LEVEL = 1,
DEBUG = 900,
DBG0 = 1000,
WARN = 3000,
WARNING = 3000,
- // Unfortunately Windows headers #define ERROR
- // On Windows platforms we avoid defining ERROR. However we make it
- // available on other platforms, to make it easier to convert code from
- // other log libraries that also use ERROR as their log level name (e.g.,
- // glog).
+ // Unfortunately Windows headers #define ERROR, so we cannot use
+ // it as an enum value name. We only provide ERR instead.
ERR = 4000,
-#ifndef ERROR
- ERROR = 4000,
-#endif
CRITICAL = 5000,
+ // DFATAL log messages crash the program on debug builds.
+ DFATAL = 0x7ffffffe,
+ // FATAL log messages always abort the program.
+ // This level is equivalent to MAX_LEVEL.
+ FATAL = 0x7fffffff,
+
// The most significant bit is used by LogCategory to store a flag value,
// so the maximum value has that bit cleared.
//
* Support adding and subtracting integers from LogLevels, to create slightly
* adjusted log level values.
*/
-inline LogLevel operator+(LogLevel level, uint32_t value) {
- auto newValue = static_cast<uint32_t>(level) + value;
+inline constexpr LogLevel operator+(LogLevel level, uint32_t value) {
// Cap the result at LogLevel::MAX_LEVEL
- if (newValue > static_cast<uint32_t>(LogLevel::MAX_LEVEL)) {
- return LogLevel::MAX_LEVEL;
- }
- return static_cast<LogLevel>(newValue);
+ return ((static_cast<uint32_t>(level) + value) >
+ static_cast<uint32_t>(LogLevel::MAX_LEVEL))
+ ? LogLevel::MAX_LEVEL
+ : static_cast<LogLevel>(static_cast<uint32_t>(level) + value);
}
inline LogLevel& operator+=(LogLevel& level, uint32_t value) {
level = level + value;
return level;
}
-inline LogLevel operator-(LogLevel level, uint32_t value) {
+inline constexpr LogLevel operator-(LogLevel level, uint32_t value) {
return static_cast<LogLevel>(static_cast<uint32_t>(level) - value);
}
inline LogLevel& operator-=(LogLevel& level, uint32_t value) {
return level;
}
-/*
- * Comparisons between LogLevel values
- */
-inline bool operator<=(LogLevel a, LogLevel b) {
- return static_cast<uint32_t>(a) <= static_cast<uint32_t>(b);
-}
-inline bool operator<(LogLevel a, LogLevel b) {
- return static_cast<uint32_t>(a) < static_cast<uint32_t>(b);
-}
-inline bool operator>=(LogLevel a, LogLevel b) {
- return static_cast<uint32_t>(a) >= static_cast<uint32_t>(b);
-}
-inline bool operator>(LogLevel a, LogLevel b) {
- return static_cast<uint32_t>(a) > static_cast<uint32_t>(b);
-}
-
/**
* Construct a LogLevel from a string name.
*/
* Print a LogLevel in a human readable format.
*/
std::ostream& operator<<(std::ostream& os, LogLevel level);
+
+/**
+ * Returns true if and only if a LogLevel is fatal.
+ */
+inline constexpr bool isLogLevelFatal(LogLevel level) {
+ return folly::kIsDebug ? (level >= LogLevel::DFATAL)
+ : (level >= LogLevel::FATAL);
}
+} // namespace folly