log_severity.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. // Copyright 2017 The Abseil Authors.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // https://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #ifndef ABSL_BASE_LOG_SEVERITY_H_
  15. #define ABSL_BASE_LOG_SEVERITY_H_
  16. #include <array>
  17. #include <ostream>
  18. #include "absl/base/attributes.h"
  19. #include "absl/base/config.h"
  20. namespace absl {
  21. ABSL_NAMESPACE_BEGIN
  22. // absl::LogSeverity
  23. //
  24. // Four severity levels are defined. Logging APIs should terminate the program
  25. // when a message is logged at severity `kFatal`; the other levels have no
  26. // special semantics.
  27. //
  28. // Values other than the four defined levels (e.g. produced by `static_cast`)
  29. // are valid, but their semantics when passed to a function, macro, or flag
  30. // depend on the function, macro, or flag. The usual behavior is to normalize
  31. // such values to a defined severity level, however in some cases values other
  32. // than the defined levels are useful for comparison.
  33. //
  34. // Example:
  35. //
  36. // // Effectively disables all logging:
  37. // SetMinLogLevel(static_cast<absl::LogSeverity>(100));
  38. //
  39. // Abseil flags may be defined with type `LogSeverity`. Dependency layering
  40. // constraints require that the `AbslParseFlag()` overload be declared and
  41. // defined in the flags library itself rather than here. The `AbslUnparseFlag()`
  42. // overload is defined there as well for consistency.
  43. //
  44. // absl::LogSeverity Flag String Representation
  45. //
  46. // An `absl::LogSeverity` has a string representation used for parsing
  47. // command-line flags based on the enumerator name (e.g. `kFatal`) or
  48. // its unprefixed name (without the `k`) in any case-insensitive form. (E.g.
  49. // "FATAL", "fatal" or "Fatal" are all valid.) Unparsing such flags produces an
  50. // unprefixed string representation in all caps (e.g. "FATAL") or an integer.
  51. //
  52. // Additionally, the parser accepts arbitrary integers (as if the type were
  53. // `int`).
  54. //
  55. // Examples:
  56. //
  57. // --my_log_level=kInfo
  58. // --my_log_level=INFO
  59. // --my_log_level=info
  60. // --my_log_level=0
  61. //
  62. // `DFATAL` and `kLogDebugFatal` are similarly accepted.
  63. //
  64. // Unparsing a flag produces the same result as `absl::LogSeverityName()` for
  65. // the standard levels and a base-ten integer otherwise.
  66. enum class LogSeverity : int {
  67. kInfo = 0,
  68. kWarning = 1,
  69. kError = 2,
  70. kFatal = 3,
  71. };
  72. // LogSeverities()
  73. //
  74. // Returns an iterable of all standard `absl::LogSeverity` values, ordered from
  75. // least to most severe.
  76. constexpr std::array<absl::LogSeverity, 4> LogSeverities() {
  77. return {{absl::LogSeverity::kInfo, absl::LogSeverity::kWarning,
  78. absl::LogSeverity::kError, absl::LogSeverity::kFatal}};
  79. }
  80. // `absl::kLogDebugFatal` equals `absl::LogSeverity::kFatal` in debug builds
  81. // (i.e. when `NDEBUG` is not defined) and `absl::LogSeverity::kError`
  82. // otherwise. Avoid ODR-using this variable as it has internal linkage and thus
  83. // distinct storage in different TUs.
  84. #ifdef NDEBUG
  85. static constexpr absl::LogSeverity kLogDebugFatal = absl::LogSeverity::kError;
  86. #else
  87. static constexpr absl::LogSeverity kLogDebugFatal = absl::LogSeverity::kFatal;
  88. #endif
  89. // LogSeverityName()
  90. //
  91. // Returns the all-caps string representation (e.g. "INFO") of the specified
  92. // severity level if it is one of the standard levels and "UNKNOWN" otherwise.
  93. constexpr const char* LogSeverityName(absl::LogSeverity s) {
  94. switch (s) {
  95. case absl::LogSeverity::kInfo: return "INFO";
  96. case absl::LogSeverity::kWarning: return "WARNING";
  97. case absl::LogSeverity::kError: return "ERROR";
  98. case absl::LogSeverity::kFatal: return "FATAL";
  99. }
  100. return "UNKNOWN";
  101. }
  102. // NormalizeLogSeverity()
  103. //
  104. // Values less than `kInfo` normalize to `kInfo`; values greater than `kFatal`
  105. // normalize to `kError` (**NOT** `kFatal`).
  106. constexpr absl::LogSeverity NormalizeLogSeverity(absl::LogSeverity s) {
  107. absl::LogSeverity n = s;
  108. if (n < absl::LogSeverity::kInfo) n = absl::LogSeverity::kInfo;
  109. if (n > absl::LogSeverity::kFatal) n = absl::LogSeverity::kError;
  110. return n;
  111. }
  112. constexpr absl::LogSeverity NormalizeLogSeverity(int s) {
  113. return absl::NormalizeLogSeverity(static_cast<absl::LogSeverity>(s));
  114. }
  115. // operator<<
  116. //
  117. // The exact representation of a streamed `absl::LogSeverity` is deliberately
  118. // unspecified; do not rely on it.
  119. std::ostream& operator<<(std::ostream& os, absl::LogSeverity s);
  120. // Enums representing a lower bound for LogSeverity. APIs that only operate on
  121. // messages of at least a certain level (for example, `SetMinLogLevel()`) use
  122. // this type to specify that level. absl::LogSeverityAtLeast::kInfinity is
  123. // a level above all threshold levels and therefore no log message will
  124. // ever meet this threshold.
  125. enum class LogSeverityAtLeast : int {
  126. kInfo = static_cast<int>(absl::LogSeverity::kInfo),
  127. kWarning = static_cast<int>(absl::LogSeverity::kWarning),
  128. kError = static_cast<int>(absl::LogSeverity::kError),
  129. kFatal = static_cast<int>(absl::LogSeverity::kFatal),
  130. kInfinity = 1000,
  131. };
  132. std::ostream& operator<<(std::ostream& os, absl::LogSeverityAtLeast s);
  133. // Enums representing an upper bound for LogSeverity. APIs that only operate on
  134. // messages of at most a certain level (for example, buffer all messages at or
  135. // below a certain level) use this type to specify that level.
  136. // absl::LogSeverityAtMost::kNegativeInfinity is a level below all threshold
  137. // levels and therefore will exclude all log messages.
  138. enum class LogSeverityAtMost : int {
  139. kNegativeInfinity = -1000,
  140. kInfo = static_cast<int>(absl::LogSeverity::kInfo),
  141. kWarning = static_cast<int>(absl::LogSeverity::kWarning),
  142. kError = static_cast<int>(absl::LogSeverity::kError),
  143. kFatal = static_cast<int>(absl::LogSeverity::kFatal),
  144. };
  145. std::ostream& operator<<(std::ostream& os, absl::LogSeverityAtMost s);
  146. #define COMPOP(op1, op2, T) \
  147. constexpr bool operator op1(absl::T lhs, absl::LogSeverity rhs) { \
  148. return static_cast<absl::LogSeverity>(lhs) op1 rhs; \
  149. } \
  150. constexpr bool operator op2(absl::LogSeverity lhs, absl::T rhs) { \
  151. return lhs op2 static_cast<absl::LogSeverity>(rhs); \
  152. }
  153. // Comparisons between `LogSeverity` and `LogSeverityAtLeast`/
  154. // `LogSeverityAtMost` are only supported in one direction.
  155. // Valid checks are:
  156. // LogSeverity >= LogSeverityAtLeast
  157. // LogSeverity < LogSeverityAtLeast
  158. // LogSeverity <= LogSeverityAtMost
  159. // LogSeverity > LogSeverityAtMost
  160. COMPOP(>, <, LogSeverityAtLeast)
  161. COMPOP(<=, >=, LogSeverityAtLeast)
  162. COMPOP(<, >, LogSeverityAtMost)
  163. COMPOP(>=, <=, LogSeverityAtMost)
  164. #undef COMPOP
  165. ABSL_NAMESPACE_END
  166. } // namespace absl
  167. #endif // ABSL_BASE_LOG_SEVERITY_H_