LogLevel.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. * Copyright 2017-present Facebook, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #pragma once
  17. #include <cstdint>
  18. #include <iosfwd>
  19. #include <string>
  20. #include <type_traits>
  21. #include <folly/Portability.h>
  22. #include <folly/Range.h>
  23. namespace folly {
  24. /**
  25. * Log level values.
  26. *
  27. * Higher levels are more important than lower ones.
  28. *
  29. * However, the numbers in the DBG* and INFO* level names are reversed, and can
  30. * be thought of as debug verbosity levels. Increasing DBG* numbers mean
  31. * increasing level of verbosity. DBG0 is the least verbose debug level, DBG1
  32. * is one level higher of verbosity, etc.
  33. */
  34. enum class LogLevel : uint32_t {
  35. UNINITIALIZED = 0,
  36. NONE = 1,
  37. MIN_LEVEL = 1,
  38. // "DBG" is the lowest (aka most verbose) debug log level.
  39. // This level is intended to be primarily used in log category settings.
  40. // In your code it is usually better to use one of the finer-grained DBGn
  41. // levels. In your log category settings you can then set the log category
  42. // level to a specific DBGn level, or to to main DBG level to enable all DBGn
  43. // messages.
  44. //
  45. // This is named "DBG" rather than "DEBUG" since some open source projects
  46. // define "DEBUG" as a preprocessor macro.
  47. DBG = 1000,
  48. // Fine-grained debug log levels.
  49. DBG0 = 1999,
  50. DBG1 = 1998,
  51. DBG2 = 1997,
  52. DBG3 = 1996,
  53. DBG4 = 1995,
  54. DBG5 = 1994,
  55. DBG6 = 1993,
  56. DBG7 = 1992,
  57. DBG8 = 1991,
  58. DBG9 = 1990,
  59. INFO = 2000,
  60. // Fine-grained info log levels.
  61. INFO0 = 2999,
  62. INFO1 = 2998,
  63. INFO2 = 2997,
  64. INFO3 = 2996,
  65. INFO4 = 2995,
  66. INFO5 = 2994,
  67. INFO6 = 2993,
  68. INFO7 = 2992,
  69. INFO8 = 2991,
  70. INFO9 = 2990,
  71. WARN = 3000,
  72. WARNING = 3000,
  73. // Unfortunately Windows headers #define ERROR, so we cannot use
  74. // it as an enum value name. We only provide ERR instead.
  75. ERR = 4000,
  76. CRITICAL = 5000,
  77. // DFATAL log messages crash the program on debug builds.
  78. DFATAL = 0x7ffffffe,
  79. // FATAL log messages always abort the program.
  80. // This level is equivalent to MAX_LEVEL.
  81. FATAL = 0x7fffffff,
  82. // The most significant bit is used by LogCategory to store a flag value,
  83. // so the maximum value has that bit cleared.
  84. //
  85. // (We call this MAX_LEVEL instead of MAX just since MAX() is commonly
  86. // defined as a preprocessor macro by some C headers.)
  87. MAX_LEVEL = 0x7fffffff,
  88. };
  89. constexpr LogLevel kDefaultLogLevel = LogLevel::INFO;
  90. /*
  91. * Support adding and subtracting integers from LogLevels, to create slightly
  92. * adjusted log level values.
  93. */
  94. inline constexpr LogLevel operator+(LogLevel level, uint32_t value) {
  95. // Cap the result at LogLevel::MAX_LEVEL
  96. return ((static_cast<uint32_t>(level) + value) >
  97. static_cast<uint32_t>(LogLevel::MAX_LEVEL))
  98. ? LogLevel::MAX_LEVEL
  99. : static_cast<LogLevel>(static_cast<uint32_t>(level) + value);
  100. }
  101. inline LogLevel& operator+=(LogLevel& level, uint32_t value) {
  102. level = level + value;
  103. return level;
  104. }
  105. inline constexpr LogLevel operator-(LogLevel level, uint32_t value) {
  106. return static_cast<LogLevel>(static_cast<uint32_t>(level) - value);
  107. }
  108. inline LogLevel& operator-=(LogLevel& level, uint32_t value) {
  109. level = level - value;
  110. return level;
  111. }
  112. /**
  113. * Construct a LogLevel from a string name.
  114. */
  115. LogLevel stringToLogLevel(folly::StringPiece name);
  116. /**
  117. * Get a human-readable string representing the LogLevel.
  118. */
  119. std::string logLevelToString(LogLevel level);
  120. /**
  121. * Print a LogLevel in a human readable format.
  122. */
  123. std::ostream& operator<<(std::ostream& os, LogLevel level);
  124. /**
  125. * Returns true if and only if a LogLevel is fatal.
  126. */
  127. inline constexpr bool isLogLevelFatal(LogLevel level) {
  128. return folly::kIsDebug ? (level >= LogLevel::DFATAL)
  129. : (level >= LogLevel::FATAL);
  130. }
  131. } // namespace folly