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 //      http://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 //
16 // NOTE FOR GOOGLERS:
17 //
18 // IWYU pragma: private, include "base/log_severity.h"
19 
20 #ifndef S2_THIRD_PARTY_ABSL_BASE_INTERNAL_LOG_SEVERITY_H_
21 #define S2_THIRD_PARTY_ABSL_BASE_INTERNAL_LOG_SEVERITY_H_
22 
23 #include <array>
24 
25 #include "s2/third_party/absl/base/attributes.h"
26 
27 namespace absl {
28 
29 // Four severity levels are defined.  Logging APIs should terminate the program
30 // when a message is logged at severity `kFatal`; the other levels have no
31 // special semantics.
32 enum class LogSeverity : int {
33   kInfo = 0,
34   kWarning = 1,
35   kError = 2,
36   kFatal = 3,
37 };
38 
39 // Returns an iterable of all standard `absl::LogSeverity` values, ordered from
40 // least to most severe.
LogSeverities()41 constexpr std::array<absl::LogSeverity, 4> LogSeverities() {
42   return {{absl::LogSeverity::kInfo, absl::LogSeverity::kWarning,
43            absl::LogSeverity::kError, absl::LogSeverity::kFatal}};
44 }
45 
46 // `absl::kLogDebugFatal` equals `absl::LogSeverity::kFatal` in debug builds
47 // (i.e. when `NDEBUG` is not defined) and `absl::LogSeverity::kError`
48 // otherwise.  It is extern to prevent ODR violations when compilation units
49 // with different build settings are linked together.
50 ABSL_CONST_INIT extern const absl::LogSeverity kLogDebugFatal;
51 
52 // Returns the all-caps string representation (e.g. "INFO") of the specified
53 // severity level if it is one of the normal levels and "UNKNOWN" otherwise.
LogSeverityName(absl::LogSeverity s)54 constexpr const char* LogSeverityName(absl::LogSeverity s) {
55   return s == absl::LogSeverity::kInfo
56              ? "INFO"
57              : s == absl::LogSeverity::kWarning
58                    ? "WARNING"
59                    : s == absl::LogSeverity::kError
60                          ? "ERROR"
61                          : s == absl::LogSeverity::kFatal ? "FATAL" : "UNKNOWN";
62 }
63 
64 // Values less than `kInfo` normalize to `kInfo`; values greater than `kFatal`
65 // normalize to `kError` (**NOT** `kFatal`).
NormalizeLogSeverity(absl::LogSeverity s)66 constexpr absl::LogSeverity NormalizeLogSeverity(absl::LogSeverity s) {
67   return s < absl::LogSeverity::kInfo
68              ? absl::LogSeverity::kInfo
69              : s > absl::LogSeverity::kFatal ? absl::LogSeverity::kError : s;
70 }
NormalizeLogSeverity(int s)71 constexpr absl::LogSeverity NormalizeLogSeverity(int s) {
72   return NormalizeLogSeverity(static_cast<absl::LogSeverity>(s));
73 }
74 
75 }  // namespace absl
76 
77 #endif  // S2_THIRD_PARTY_ABSL_BASE_INTERNAL_LOG_SEVERITY_H_
78