Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame^] | 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 | |
| 15 | #ifndef ABSL_BASE_INTERNAL_LOG_SEVERITY_H_ |
| 16 | #define ABSL_BASE_INTERNAL_LOG_SEVERITY_H_ |
| 17 | |
| 18 | #include <array> |
| 19 | #include <ostream> |
| 20 | |
| 21 | #include "absl/base/attributes.h" |
| 22 | |
| 23 | namespace absl { |
| 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 | // Abseil flags may be defined with type `LogSeverity`. Dependency layering |
| 30 | // constraints require that the `AbslParseFlag` overload be declared and defined |
| 31 | // in the flags module rather than here. The `AbslUnparseFlag` overload is |
| 32 | // defined there too for consistency. |
| 33 | // |
| 34 | // The parser accepts arbitrary integers (as if the type were `int`). It also |
| 35 | // accepts each named enumerator, without regard for case, with or without the |
| 36 | // leading 'k'. For example: "kInfo", "INFO", and "info" all parse to the value |
| 37 | // `absl::LogSeverity::kInfo`. |
| 38 | // |
| 39 | // Unparsing a flag produces the same result as `absl::LogSeverityName()` for |
| 40 | // the standard levels and a base-ten integer otherwise. |
| 41 | enum class LogSeverity : int { |
| 42 | kInfo = 0, |
| 43 | kWarning = 1, |
| 44 | kError = 2, |
| 45 | kFatal = 3, |
| 46 | }; |
| 47 | |
| 48 | // Returns an iterable of all standard `absl::LogSeverity` values, ordered from |
| 49 | // least to most severe. |
| 50 | constexpr std::array<absl::LogSeverity, 4> LogSeverities() { |
| 51 | return {{absl::LogSeverity::kInfo, absl::LogSeverity::kWarning, |
| 52 | absl::LogSeverity::kError, absl::LogSeverity::kFatal}}; |
| 53 | } |
| 54 | |
| 55 | // Returns the all-caps string representation (e.g. "INFO") of the specified |
| 56 | // severity level if it is one of the standard levels and "UNKNOWN" otherwise. |
| 57 | constexpr const char* LogSeverityName(absl::LogSeverity s) { |
| 58 | return s == absl::LogSeverity::kInfo |
| 59 | ? "INFO" |
| 60 | : s == absl::LogSeverity::kWarning |
| 61 | ? "WARNING" |
| 62 | : s == absl::LogSeverity::kError |
| 63 | ? "ERROR" |
| 64 | : s == absl::LogSeverity::kFatal ? "FATAL" : "UNKNOWN"; |
| 65 | } |
| 66 | |
| 67 | // Values less than `kInfo` normalize to `kInfo`; values greater than `kFatal` |
| 68 | // normalize to `kError` (**NOT** `kFatal`). |
| 69 | constexpr absl::LogSeverity NormalizeLogSeverity(absl::LogSeverity s) { |
| 70 | return s < absl::LogSeverity::kInfo |
| 71 | ? absl::LogSeverity::kInfo |
| 72 | : s > absl::LogSeverity::kFatal ? absl::LogSeverity::kError : s; |
| 73 | } |
| 74 | constexpr absl::LogSeverity NormalizeLogSeverity(int s) { |
| 75 | return absl::NormalizeLogSeverity(static_cast<absl::LogSeverity>(s)); |
| 76 | } |
| 77 | |
| 78 | // The exact representation of a streamed `absl::LogSeverity` is deliberately |
| 79 | // unspecified; do not rely on it. |
| 80 | std::ostream& operator<<(std::ostream& os, absl::LogSeverity s); |
| 81 | |
| 82 | } // namespace absl |
| 83 | |
| 84 | #endif // ABSL_BASE_INTERNAL_LOG_SEVERITY_H_ |