1 // Copyright 2017 The Abseil Authors.
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
7 // https://www.apache.org/licenses/LICENSE-2.0
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.
15 #ifndef ABSL_BASE_INTERNAL_LOG_SEVERITY_H_
16 #define ABSL_BASE_INTERNAL_LOG_SEVERITY_H_
21 #include "absl/base/attributes.h"
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
28 enum class LogSeverity : int {
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}};
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
47 : s == absl::LogSeverity::kWarning
49 : s == absl::LogSeverity::kError
51 : s == absl::LogSeverity::kFatal ? "FATAL" : "UNKNOWN";
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;
61 constexpr absl::LogSeverity NormalizeLogSeverity(int s) {
62 return NormalizeLogSeverity(static_cast<absl::LogSeverity>(s));
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);
71 #endif // ABSL_BASE_INTERNAL_LOG_SEVERITY_H_