| 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 | enum class LogSeverity : int { |
| 29 | kInfo = 0, |
| 30 | kWarning = 1, |
| 31 | kError = 2, |
| 32 | kFatal = 3, |
| 33 | }; |
| 34 | |
| 35 | // Returns an iterable of all standard `absl::LogSeverity` values, ordered from |
| 36 | // least to most severe. |
| 37 | constexpr std::array<absl::LogSeverity, 4> LogSeverities() { |
| 38 | return {{absl::LogSeverity::kInfo, absl::LogSeverity::kWarning, |
| 39 | absl::LogSeverity::kError, absl::LogSeverity::kFatal}}; |
| 40 | } |
| 41 | |
| 42 | // Returns the all-caps string representation (e.g. "INFO") of the specified |
| 43 | // severity level if it is one of the normal levels and "UNKNOWN" otherwise. |
| 44 | constexpr const char* LogSeverityName(absl::LogSeverity s) { |
| 45 | return s == absl::LogSeverity::kInfo |
| 46 | ? "INFO" |
| 47 | : s == absl::LogSeverity::kWarning |
| 48 | ? "WARNING" |
| 49 | : s == absl::LogSeverity::kError |
| 50 | ? "ERROR" |
| 51 | : s == absl::LogSeverity::kFatal ? "FATAL" : "UNKNOWN" ; |
| 52 | } |
| 53 | |
| 54 | // Values less than `kInfo` normalize to `kInfo`; values greater than `kFatal` |
| 55 | // normalize to `kError` (**NOT** `kFatal`). |
| 56 | constexpr absl::LogSeverity NormalizeLogSeverity(absl::LogSeverity s) { |
| 57 | return s < absl::LogSeverity::kInfo |
| 58 | ? absl::LogSeverity::kInfo |
| 59 | : s > absl::LogSeverity::kFatal ? absl::LogSeverity::kError : s; |
| 60 | } |
| 61 | constexpr absl::LogSeverity NormalizeLogSeverity(int s) { |
| 62 | return NormalizeLogSeverity(static_cast<absl::LogSeverity>(s)); |
| 63 | } |
| 64 | |
| 65 | // The exact representation of a streamed `absl::LogSeverity` is deliberately |
| 66 | // unspecified; do not rely on it. |
| 67 | std::ostream& operator<<(std::ostream& os, absl::LogSeverity s); |
| 68 | |
| 69 | } // namespace absl |
| 70 | |
| 71 | #endif // ABSL_BASE_INTERNAL_LOG_SEVERITY_H_ |
| 72 | |