logging: fix compiler compatibility for one more constexpr function
[folly.git] / folly / experimental / logging / LogLevel.h
index 05c1603efcc72955134d0de177dd61d1161651e5..a3e0e34abf71c6cd8dbba0da8af076e9b916576d 100644 (file)
@@ -18,7 +18,9 @@
 #include <cstdint>
 #include <iosfwd>
 #include <string>
+#include <type_traits>
 
+#include <folly/Portability.h>
 #include <folly/Range.h>
 
 namespace folly {
@@ -60,12 +62,15 @@ enum class LogLevel : uint32_t {
   // other log libraries that also use ERROR as their log level name (e.g.,
   // glog).
   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.
   //
@@ -78,19 +83,18 @@ enum class LogLevel : uint32_t {
  * 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) {
@@ -112,4 +116,12 @@ std::string logLevelToString(LogLevel level);
  * 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);
+}
 }