You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

log_severity.h 7.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. {
  22. ABSL_NAMESPACE_BEGIN
  23. // absl::LogSeverity
  24. //
  25. // Four severity levels are defined. Logging APIs should terminate the program
  26. // when a message is logged at severity `kFatal`; the other levels have no
  27. // special semantics.
  28. //
  29. // Values other than the four defined levels (e.g. produced by `static_cast`)
  30. // are valid, but their semantics when passed to a function, macro, or flag
  31. // depend on the function, macro, or flag. The usual behavior is to normalize
  32. // such values to a defined severity level, however in some cases values other
  33. // than the defined levels are useful for comparison.
  34. //
  35. // Example:
  36. //
  37. // // Effectively disables all logging:
  38. // SetMinLogLevel(static_cast<absl::LogSeverity>(100));
  39. //
  40. // Abseil flags may be defined with type `LogSeverity`. Dependency layering
  41. // constraints require that the `AbslParseFlag()` overload be declared and
  42. // defined in the flags library itself rather than here. The `AbslUnparseFlag()`
  43. // overload is defined there as well for consistency.
  44. //
  45. // absl::LogSeverity Flag String Representation
  46. //
  47. // An `absl::LogSeverity` has a string representation used for parsing
  48. // command-line flags based on the enumerator name (e.g. `kFatal`) or
  49. // its unprefixed name (without the `k`) in any case-insensitive form. (E.g.
  50. // "FATAL", "fatal" or "Fatal" are all valid.) Unparsing such flags produces an
  51. // unprefixed string representation in all caps (e.g. "FATAL") or an integer.
  52. //
  53. // Additionally, the parser accepts arbitrary integers (as if the type were
  54. // `int`).
  55. //
  56. // Examples:
  57. //
  58. // --my_log_level=kInfo
  59. // --my_log_level=INFO
  60. // --my_log_level=info
  61. // --my_log_level=0
  62. //
  63. // Unparsing a flag produces the same result as `absl::LogSeverityName()` for
  64. // the standard levels and a base-ten integer otherwise.
  65. enum class LogSeverity : int
  66. {
  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. {
  78. return {{absl::LogSeverity::kInfo, absl::LogSeverity::kWarning, absl::LogSeverity::kError, absl::LogSeverity::kFatal}};
  79. }
  80. // LogSeverityName()
  81. //
  82. // Returns the all-caps string representation (e.g. "INFO") of the specified
  83. // severity level if it is one of the standard levels and "UNKNOWN" otherwise.
  84. constexpr const char* LogSeverityName(absl::LogSeverity s)
  85. {
  86. return s == absl::LogSeverity::kInfo ? "INFO" : s == absl::LogSeverity::kWarning ? "WARNING" :
  87. s == absl::LogSeverity::kError ? "ERROR" :
  88. s == absl::LogSeverity::kFatal ? "FATAL" :
  89. "UNKNOWN";
  90. }
  91. // NormalizeLogSeverity()
  92. //
  93. // Values less than `kInfo` normalize to `kInfo`; values greater than `kFatal`
  94. // normalize to `kError` (**NOT** `kFatal`).
  95. constexpr absl::LogSeverity NormalizeLogSeverity(absl::LogSeverity s)
  96. {
  97. return s < absl::LogSeverity::kInfo ? absl::LogSeverity::kInfo : s > absl::LogSeverity::kFatal ? absl::LogSeverity::kError :
  98. s;
  99. }
  100. constexpr absl::LogSeverity NormalizeLogSeverity(int s)
  101. {
  102. return absl::NormalizeLogSeverity(static_cast<absl::LogSeverity>(s));
  103. }
  104. // operator<<
  105. //
  106. // The exact representation of a streamed `absl::LogSeverity` is deliberately
  107. // unspecified; do not rely on it.
  108. std::ostream& operator<<(std::ostream& os, absl::LogSeverity s);
  109. // Enums representing a lower bound for LogSeverity. APIs that only operate on
  110. // messages of at least a certain level (for example, `SetMinLogLevel()`) use
  111. // this type to specify that level. absl::LogSeverityAtLeast::kInfinity is
  112. // a level above all threshold levels and therefore no log message will
  113. // ever meet this threshold.
  114. enum class LogSeverityAtLeast : int
  115. {
  116. kInfo = static_cast<int>(absl::LogSeverity::kInfo),
  117. kWarning = static_cast<int>(absl::LogSeverity::kWarning),
  118. kError = static_cast<int>(absl::LogSeverity::kError),
  119. kFatal = static_cast<int>(absl::LogSeverity::kFatal),
  120. kInfinity = 1000,
  121. };
  122. std::ostream& operator<<(std::ostream& os, absl::LogSeverityAtLeast s);
  123. // Enums representing an upper bound for LogSeverity. APIs that only operate on
  124. // messages of at most a certain level (for example, buffer all messages at or
  125. // below a certain level) use this type to specify that level.
  126. // absl::LogSeverityAtMost::kNegativeInfinity is a level below all threshold
  127. // levels and therefore will exclude all log messages.
  128. enum class LogSeverityAtMost : int
  129. {
  130. kNegativeInfinity = -1000,
  131. kInfo = static_cast<int>(absl::LogSeverity::kInfo),
  132. kWarning = static_cast<int>(absl::LogSeverity::kWarning),
  133. kError = static_cast<int>(absl::LogSeverity::kError),
  134. kFatal = static_cast<int>(absl::LogSeverity::kFatal),
  135. };
  136. std::ostream& operator<<(std::ostream& os, absl::LogSeverityAtMost s);
  137. #define COMPOP(op1, op2, T) \
  138. constexpr bool operator op1(absl::T lhs, absl::LogSeverity rhs) \
  139. { \
  140. return static_cast<absl::LogSeverity>(lhs) op1 rhs; \
  141. } \
  142. constexpr bool operator op2(absl::LogSeverity lhs, absl::T rhs) \
  143. { \
  144. return lhs op2 static_cast<absl::LogSeverity>(rhs); \
  145. }
  146. // Comparisons between `LogSeverity` and `LogSeverityAtLeast`/
  147. // `LogSeverityAtMost` are only supported in one direction.
  148. // Valid checks are:
  149. // LogSeverity >= LogSeverityAtLeast
  150. // LogSeverity < LogSeverityAtLeast
  151. // LogSeverity <= LogSeverityAtMost
  152. // LogSeverity > LogSeverityAtMost
  153. COMPOP(>, <, LogSeverityAtLeast)
  154. COMPOP(<=, >=, LogSeverityAtLeast)
  155. COMPOP(<, >, LogSeverityAtMost)
  156. COMPOP(>=, <=, LogSeverityAtMost)
  157. #undef COMPOP
  158. ABSL_NAMESPACE_END
  159. } // namespace absl
  160. #endif // ABSL_BASE_LOG_SEVERITY_H_