log_severity.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 Y_ABSL_BASE_LOG_SEVERITY_H_
  15. #define Y_ABSL_BASE_LOG_SEVERITY_H_
  16. #include <array>
  17. #include <ostream>
  18. #include "y_absl/base/attributes.h"
  19. #include "y_absl/base/config.h"
  20. namespace y_absl {
  21. Y_ABSL_NAMESPACE_BEGIN
  22. // y_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<y_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. // y_absl::LogSeverity Flag String Representation
  45. //
  46. // An `y_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. // Unparsing a flag produces the same result as `y_absl::LogSeverityName()` for
  63. // the standard levels and a base-ten integer otherwise.
  64. enum class LogSeverity : int {
  65. kInfo = 0,
  66. kWarning = 1,
  67. kError = 2,
  68. kFatal = 3,
  69. };
  70. // LogSeverities()
  71. //
  72. // Returns an iterable of all standard `y_absl::LogSeverity` values, ordered from
  73. // least to most severe.
  74. constexpr std::array<y_absl::LogSeverity, 4> LogSeverities() {
  75. return {{y_absl::LogSeverity::kInfo, y_absl::LogSeverity::kWarning,
  76. y_absl::LogSeverity::kError, y_absl::LogSeverity::kFatal}};
  77. }
  78. // LogSeverityName()
  79. //
  80. // Returns the all-caps string representation (e.g. "INFO") of the specified
  81. // severity level if it is one of the standard levels and "UNKNOWN" otherwise.
  82. constexpr const char* LogSeverityName(y_absl::LogSeverity s) {
  83. return s == y_absl::LogSeverity::kInfo
  84. ? "INFO"
  85. : s == y_absl::LogSeverity::kWarning
  86. ? "WARNING"
  87. : s == y_absl::LogSeverity::kError
  88. ? "ERROR"
  89. : s == y_absl::LogSeverity::kFatal ? "FATAL" : "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 y_absl::LogSeverity NormalizeLogSeverity(y_absl::LogSeverity s) {
  96. return s < y_absl::LogSeverity::kInfo
  97. ? y_absl::LogSeverity::kInfo
  98. : s > y_absl::LogSeverity::kFatal ? y_absl::LogSeverity::kError : s;
  99. }
  100. constexpr y_absl::LogSeverity NormalizeLogSeverity(int s) {
  101. return y_absl::NormalizeLogSeverity(static_cast<y_absl::LogSeverity>(s));
  102. }
  103. // operator<<
  104. //
  105. // The exact representation of a streamed `y_absl::LogSeverity` is deliberately
  106. // unspecified; do not rely on it.
  107. std::ostream& operator<<(std::ostream& os, y_absl::LogSeverity s);
  108. // Enums representing a lower bound for LogSeverity. APIs that only operate on
  109. // messages of at least a certain level (for example, `SetMinLogLevel()`) use
  110. // this type to specify that level. y_absl::LogSeverityAtLeast::kInfinity is
  111. // a level above all threshold levels and therefore no log message will
  112. // ever meet this threshold.
  113. enum class LogSeverityAtLeast : int {
  114. kInfo = static_cast<int>(y_absl::LogSeverity::kInfo),
  115. kWarning = static_cast<int>(y_absl::LogSeverity::kWarning),
  116. kError = static_cast<int>(y_absl::LogSeverity::kError),
  117. kFatal = static_cast<int>(y_absl::LogSeverity::kFatal),
  118. kInfinity = 1000,
  119. };
  120. std::ostream& operator<<(std::ostream& os, y_absl::LogSeverityAtLeast s);
  121. // Enums representing an upper bound for LogSeverity. APIs that only operate on
  122. // messages of at most a certain level (for example, buffer all messages at or
  123. // below a certain level) use this type to specify that level.
  124. // y_absl::LogSeverityAtMost::kNegativeInfinity is a level below all threshold
  125. // levels and therefore will exclude all log messages.
  126. enum class LogSeverityAtMost : int {
  127. kNegativeInfinity = -1000,
  128. kInfo = static_cast<int>(y_absl::LogSeverity::kInfo),
  129. kWarning = static_cast<int>(y_absl::LogSeverity::kWarning),
  130. kError = static_cast<int>(y_absl::LogSeverity::kError),
  131. kFatal = static_cast<int>(y_absl::LogSeverity::kFatal),
  132. };
  133. std::ostream& operator<<(std::ostream& os, y_absl::LogSeverityAtMost s);
  134. #define COMPOP(op1, op2, T) \
  135. constexpr bool operator op1(y_absl::T lhs, y_absl::LogSeverity rhs) { \
  136. return static_cast<y_absl::LogSeverity>(lhs) op1 rhs; \
  137. } \
  138. constexpr bool operator op2(y_absl::LogSeverity lhs, y_absl::T rhs) { \
  139. return lhs op2 static_cast<y_absl::LogSeverity>(rhs); \
  140. }
  141. // Comparisons between `LogSeverity` and `LogSeverityAtLeast`/
  142. // `LogSeverityAtMost` are only supported in one direction.
  143. // Valid checks are:
  144. // LogSeverity >= LogSeverityAtLeast
  145. // LogSeverity < LogSeverityAtLeast
  146. // LogSeverity <= LogSeverityAtMost
  147. // LogSeverity > LogSeverityAtMost
  148. COMPOP(>, <, LogSeverityAtLeast)
  149. COMPOP(<=, >=, LogSeverityAtLeast)
  150. COMPOP(<, >, LogSeverityAtMost)
  151. COMPOP(>=, <=, LogSeverityAtMost)
  152. #undef COMPOP
  153. Y_ABSL_NAMESPACE_END
  154. } // namespace y_absl
  155. #endif // Y_ABSL_BASE_LOG_SEVERITY_H_