|
|
|||
File indexing completed on 2025-12-16 09:40:43
0001 // Copyright 2017 The Abseil Authors. 0002 // 0003 // Licensed under the Apache License, Version 2.0 (the "License"); 0004 // you may not use this file except in compliance with the License. 0005 // You may obtain a copy of the License at 0006 // 0007 // https://www.apache.org/licenses/LICENSE-2.0 0008 // 0009 // Unless required by applicable law or agreed to in writing, software 0010 // distributed under the License is distributed on an "AS IS" BASIS, 0011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 0012 // See the License for the specific language governing permissions and 0013 // limitations under the License. 0014 0015 #ifndef ABSL_BASE_LOG_SEVERITY_H_ 0016 #define ABSL_BASE_LOG_SEVERITY_H_ 0017 0018 #include <array> 0019 #include <ostream> 0020 0021 #include "absl/base/attributes.h" 0022 #include "absl/base/config.h" 0023 0024 namespace absl { 0025 ABSL_NAMESPACE_BEGIN 0026 0027 // absl::LogSeverity 0028 // 0029 // Four severity levels are defined. Logging APIs should terminate the program 0030 // when a message is logged at severity `kFatal`; the other levels have no 0031 // special semantics. 0032 // 0033 // Values other than the four defined levels (e.g. produced by `static_cast`) 0034 // are valid, but their semantics when passed to a function, macro, or flag 0035 // depend on the function, macro, or flag. The usual behavior is to normalize 0036 // such values to a defined severity level, however in some cases values other 0037 // than the defined levels are useful for comparison. 0038 // 0039 // Example: 0040 // 0041 // // Effectively disables all logging: 0042 // SetMinLogLevel(static_cast<absl::LogSeverity>(100)); 0043 // 0044 // Abseil flags may be defined with type `LogSeverity`. Dependency layering 0045 // constraints require that the `AbslParseFlag()` overload be declared and 0046 // defined in the flags library itself rather than here. The `AbslUnparseFlag()` 0047 // overload is defined there as well for consistency. 0048 // 0049 // absl::LogSeverity Flag String Representation 0050 // 0051 // An `absl::LogSeverity` has a string representation used for parsing 0052 // command-line flags based on the enumerator name (e.g. `kFatal`) or 0053 // its unprefixed name (without the `k`) in any case-insensitive form. (E.g. 0054 // "FATAL", "fatal" or "Fatal" are all valid.) Unparsing such flags produces an 0055 // unprefixed string representation in all caps (e.g. "FATAL") or an integer. 0056 // 0057 // Additionally, the parser accepts arbitrary integers (as if the type were 0058 // `int`). 0059 // 0060 // Examples: 0061 // 0062 // --my_log_level=kInfo 0063 // --my_log_level=INFO 0064 // --my_log_level=info 0065 // --my_log_level=0 0066 // 0067 // `DFATAL` and `kLogDebugFatal` are similarly accepted. 0068 // 0069 // Unparsing a flag produces the same result as `absl::LogSeverityName()` for 0070 // the standard levels and a base-ten integer otherwise. 0071 enum class LogSeverity : int { 0072 kInfo = 0, 0073 kWarning = 1, 0074 kError = 2, 0075 kFatal = 3, 0076 }; 0077 0078 // LogSeverities() 0079 // 0080 // Returns an iterable of all standard `absl::LogSeverity` values, ordered from 0081 // least to most severe. 0082 constexpr std::array<absl::LogSeverity, 4> LogSeverities() { 0083 return {{absl::LogSeverity::kInfo, absl::LogSeverity::kWarning, 0084 absl::LogSeverity::kError, absl::LogSeverity::kFatal}}; 0085 } 0086 0087 // `absl::kLogDebugFatal` equals `absl::LogSeverity::kFatal` in debug builds 0088 // (i.e. when `NDEBUG` is not defined) and `absl::LogSeverity::kError` 0089 // otherwise. Avoid ODR-using this variable as it has internal linkage and thus 0090 // distinct storage in different TUs. 0091 #ifdef NDEBUG 0092 static constexpr absl::LogSeverity kLogDebugFatal = absl::LogSeverity::kError; 0093 #else 0094 static constexpr absl::LogSeverity kLogDebugFatal = absl::LogSeverity::kFatal; 0095 #endif 0096 0097 // LogSeverityName() 0098 // 0099 // Returns the all-caps string representation (e.g. "INFO") of the specified 0100 // severity level if it is one of the standard levels and "UNKNOWN" otherwise. 0101 constexpr const char* LogSeverityName(absl::LogSeverity s) { 0102 switch (s) { 0103 case absl::LogSeverity::kInfo: return "INFO"; 0104 case absl::LogSeverity::kWarning: return "WARNING"; 0105 case absl::LogSeverity::kError: return "ERROR"; 0106 case absl::LogSeverity::kFatal: return "FATAL"; 0107 } 0108 return "UNKNOWN"; 0109 } 0110 0111 // NormalizeLogSeverity() 0112 // 0113 // Values less than `kInfo` normalize to `kInfo`; values greater than `kFatal` 0114 // normalize to `kError` (**NOT** `kFatal`). 0115 constexpr absl::LogSeverity NormalizeLogSeverity(absl::LogSeverity s) { 0116 absl::LogSeverity n = s; 0117 if (n < absl::LogSeverity::kInfo) n = absl::LogSeverity::kInfo; 0118 if (n > absl::LogSeverity::kFatal) n = absl::LogSeverity::kError; 0119 return n; 0120 } 0121 constexpr absl::LogSeverity NormalizeLogSeverity(int s) { 0122 return absl::NormalizeLogSeverity(static_cast<absl::LogSeverity>(s)); 0123 } 0124 0125 // operator<< 0126 // 0127 // The exact representation of a streamed `absl::LogSeverity` is deliberately 0128 // unspecified; do not rely on it. 0129 std::ostream& operator<<(std::ostream& os, absl::LogSeverity s); 0130 0131 // Enums representing a lower bound for LogSeverity. APIs that only operate on 0132 // messages of at least a certain level (for example, `SetMinLogLevel()`) use 0133 // this type to specify that level. absl::LogSeverityAtLeast::kInfinity is 0134 // a level above all threshold levels and therefore no log message will 0135 // ever meet this threshold. 0136 enum class LogSeverityAtLeast : int { 0137 kInfo = static_cast<int>(absl::LogSeverity::kInfo), 0138 kWarning = static_cast<int>(absl::LogSeverity::kWarning), 0139 kError = static_cast<int>(absl::LogSeverity::kError), 0140 kFatal = static_cast<int>(absl::LogSeverity::kFatal), 0141 kInfinity = 1000, 0142 }; 0143 0144 std::ostream& operator<<(std::ostream& os, absl::LogSeverityAtLeast s); 0145 0146 // Enums representing an upper bound for LogSeverity. APIs that only operate on 0147 // messages of at most a certain level (for example, buffer all messages at or 0148 // below a certain level) use this type to specify that level. 0149 // absl::LogSeverityAtMost::kNegativeInfinity is a level below all threshold 0150 // levels and therefore will exclude all log messages. 0151 enum class LogSeverityAtMost : int { 0152 kNegativeInfinity = -1000, 0153 kInfo = static_cast<int>(absl::LogSeverity::kInfo), 0154 kWarning = static_cast<int>(absl::LogSeverity::kWarning), 0155 kError = static_cast<int>(absl::LogSeverity::kError), 0156 kFatal = static_cast<int>(absl::LogSeverity::kFatal), 0157 }; 0158 0159 std::ostream& operator<<(std::ostream& os, absl::LogSeverityAtMost s); 0160 0161 #define COMPOP(op1, op2, T) \ 0162 constexpr bool operator op1(absl::T lhs, absl::LogSeverity rhs) { \ 0163 return static_cast<absl::LogSeverity>(lhs) op1 rhs; \ 0164 } \ 0165 constexpr bool operator op2(absl::LogSeverity lhs, absl::T rhs) { \ 0166 return lhs op2 static_cast<absl::LogSeverity>(rhs); \ 0167 } 0168 0169 // Comparisons between `LogSeverity` and `LogSeverityAtLeast`/ 0170 // `LogSeverityAtMost` are only supported in one direction. 0171 // Valid checks are: 0172 // LogSeverity >= LogSeverityAtLeast 0173 // LogSeverity < LogSeverityAtLeast 0174 // LogSeverity <= LogSeverityAtMost 0175 // LogSeverity > LogSeverityAtMost 0176 COMPOP(>, <, LogSeverityAtLeast) 0177 COMPOP(<=, >=, LogSeverityAtLeast) 0178 COMPOP(<, >, LogSeverityAtMost) 0179 COMPOP(>=, <=, LogSeverityAtMost) 0180 #undef COMPOP 0181 0182 ABSL_NAMESPACE_END 0183 } // namespace absl 0184 0185 #endif // ABSL_BASE_LOG_SEVERITY_H_
| [ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
|
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |
|