2 * Copyright 2004-present Facebook, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
21 #include <type_traits>
23 #include <folly/Portability.h>
24 #include <folly/Range.h>
31 * Higher levels are more important than lower ones.
33 * However, the numbers in the DBG* level names are reversed, and can be
34 * thought of as debug verbosity levels. Increasing DBG* numbers mean
35 * increasing level of verbosity. DBG0 is the least verbose debug level,
36 * DBG1 is one level higher of verbosity, etc.
38 enum class LogLevel : uint32_t {
59 // Unfortunately Windows headers #define ERROR
60 // On Windows platforms we avoid defining ERROR. However we make it
61 // available on other platforms, to make it easier to convert code from
62 // other log libraries that also use ERROR as their log level name (e.g.,
68 // DFATAL log messages crash the program on debug builds.
70 // FATAL log messages always abort the program.
71 // This level is equivalent to MAX_LEVEL.
74 // The most significant bit is used by LogCategory to store a flag value,
75 // so the maximum value has that bit cleared.
77 // (We call this MAX_LEVEL instead of MAX just since MAX() is commonly
78 // defined as a preprocessor macro by some C headers.)
79 MAX_LEVEL = 0x7fffffff,
83 * Support adding and subtracting integers from LogLevels, to create slightly
84 * adjusted log level values.
86 inline constexpr LogLevel operator+(LogLevel level, uint32_t value) {
87 // Cap the result at LogLevel::MAX_LEVEL
88 return ((static_cast<uint32_t>(level) + value) >
89 static_cast<uint32_t>(LogLevel::MAX_LEVEL))
91 : static_cast<LogLevel>(static_cast<uint32_t>(level) + value);
93 inline LogLevel& operator+=(LogLevel& level, uint32_t value) {
94 level = level + value;
97 inline constexpr LogLevel operator-(LogLevel level, uint32_t value) {
98 return static_cast<LogLevel>(static_cast<uint32_t>(level) - value);
100 inline LogLevel& operator-=(LogLevel& level, uint32_t value) {
101 level = level - value;
106 * Construct a LogLevel from a string name.
108 LogLevel stringToLogLevel(folly::StringPiece name);
111 * Get a human-readable string representing the LogLevel.
113 std::string logLevelToString(LogLevel level);
116 * Print a LogLevel in a human readable format.
118 std::ostream& operator<<(std::ostream& os, LogLevel level);
121 * Returns true if and only if a LogLevel is fatal.
123 inline constexpr bool isLogLevelFatal(LogLevel level) {
124 return folly::kIsDebug ? (level >= LogLevel::DFATAL)
125 : (level >= LogLevel::FATAL);