blob: 045f17f81db946ca777ca7a9403b8c7f49579e88 [file] [log] [blame]
Austin Schuh36244a12019-09-21 17:52:38 -07001// 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
Austin Schuhb4691e92020-12-31 12:37:18 -080015#ifndef ABSL_BASE_LOG_SEVERITY_H_
16#define ABSL_BASE_LOG_SEVERITY_H_
Austin Schuh36244a12019-09-21 17:52:38 -070017
18#include <array>
19#include <ostream>
20
21#include "absl/base/attributes.h"
Austin Schuhb4691e92020-12-31 12:37:18 -080022#include "absl/base/config.h"
Austin Schuh36244a12019-09-21 17:52:38 -070023
24namespace absl {
Austin Schuhb4691e92020-12-31 12:37:18 -080025ABSL_NAMESPACE_BEGIN
Austin Schuh36244a12019-09-21 17:52:38 -070026
Austin Schuhb4691e92020-12-31 12:37:18 -080027// absl::LogSeverity
28//
29// Four severity levels are defined. Logging APIs should terminate the program
Austin Schuh36244a12019-09-21 17:52:38 -070030// when a message is logged at severity `kFatal`; the other levels have no
31// special semantics.
32//
Austin Schuhb4691e92020-12-31 12:37:18 -080033// Values other than the four defined levels (e.g. produced by `static_cast`)
34// are valid, but their semantics when passed to a function, macro, or flag
35// depend on the function, macro, or flag. The usual behavior is to normalize
36// such values to a defined severity level, however in some cases values other
37// than the defined levels are useful for comparison.
Austin Schuh36244a12019-09-21 17:52:38 -070038//
Austin Schuhb4691e92020-12-31 12:37:18 -080039// Exmaple:
40//
41// // Effectively disables all logging:
42// SetMinLogLevel(static_cast<absl::LogSeverity>(100));
43//
44// Abseil flags may be defined with type `LogSeverity`. Dependency layering
45// constraints require that the `AbslParseFlag()` overload be declared and
46// defined in the flags library itself rather than here. The `AbslUnparseFlag()`
47// overload is defined there as well for consistency.
48//
49// absl::LogSeverity Flag String Representation
50//
51// An `absl::LogSeverity` has a string representation used for parsing
52// command-line flags based on the enumerator name (e.g. `kFatal`) or
53// its unprefixed name (without the `k`) in any case-insensitive form. (E.g.
54// "FATAL", "fatal" or "Fatal" are all valid.) Unparsing such flags produces an
55// unprefixed string representation in all caps (e.g. "FATAL") or an integer.
56//
57// Additionally, the parser accepts arbitrary integers (as if the type were
58// `int`).
59//
60// Examples:
61//
62// --my_log_level=kInfo
63// --my_log_level=INFO
64// --my_log_level=info
65// --my_log_level=0
Austin Schuh36244a12019-09-21 17:52:38 -070066//
67// Unparsing a flag produces the same result as `absl::LogSeverityName()` for
68// the standard levels and a base-ten integer otherwise.
69enum class LogSeverity : int {
70 kInfo = 0,
71 kWarning = 1,
72 kError = 2,
73 kFatal = 3,
74};
75
Austin Schuhb4691e92020-12-31 12:37:18 -080076// LogSeverities()
77//
Austin Schuh36244a12019-09-21 17:52:38 -070078// Returns an iterable of all standard `absl::LogSeverity` values, ordered from
79// least to most severe.
80constexpr std::array<absl::LogSeverity, 4> LogSeverities() {
81 return {{absl::LogSeverity::kInfo, absl::LogSeverity::kWarning,
82 absl::LogSeverity::kError, absl::LogSeverity::kFatal}};
83}
84
Austin Schuhb4691e92020-12-31 12:37:18 -080085// LogSeverityName()
86//
Austin Schuh36244a12019-09-21 17:52:38 -070087// Returns the all-caps string representation (e.g. "INFO") of the specified
88// severity level if it is one of the standard levels and "UNKNOWN" otherwise.
89constexpr const char* LogSeverityName(absl::LogSeverity s) {
90 return s == absl::LogSeverity::kInfo
91 ? "INFO"
92 : s == absl::LogSeverity::kWarning
93 ? "WARNING"
94 : s == absl::LogSeverity::kError
95 ? "ERROR"
96 : s == absl::LogSeverity::kFatal ? "FATAL" : "UNKNOWN";
97}
98
Austin Schuhb4691e92020-12-31 12:37:18 -080099// NormalizeLogSeverity()
100//
Austin Schuh36244a12019-09-21 17:52:38 -0700101// Values less than `kInfo` normalize to `kInfo`; values greater than `kFatal`
102// normalize to `kError` (**NOT** `kFatal`).
103constexpr absl::LogSeverity NormalizeLogSeverity(absl::LogSeverity s) {
104 return s < absl::LogSeverity::kInfo
105 ? absl::LogSeverity::kInfo
106 : s > absl::LogSeverity::kFatal ? absl::LogSeverity::kError : s;
107}
108constexpr absl::LogSeverity NormalizeLogSeverity(int s) {
109 return absl::NormalizeLogSeverity(static_cast<absl::LogSeverity>(s));
110}
111
Austin Schuhb4691e92020-12-31 12:37:18 -0800112// operator<<
113//
Austin Schuh36244a12019-09-21 17:52:38 -0700114// The exact representation of a streamed `absl::LogSeverity` is deliberately
115// unspecified; do not rely on it.
116std::ostream& operator<<(std::ostream& os, absl::LogSeverity s);
117
Austin Schuhb4691e92020-12-31 12:37:18 -0800118ABSL_NAMESPACE_END
Austin Schuh36244a12019-09-21 17:52:38 -0700119} // namespace absl
120
Austin Schuhb4691e92020-12-31 12:37:18 -0800121#endif // ABSL_BASE_LOG_SEVERITY_H_