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* and INFO* level names are reversed, and can
34 * be thought of as debug verbosity levels. Increasing DBG* numbers mean
35 * increasing level of verbosity. DBG0 is the least verbose debug level, DBG1
36 * is one level higher of verbosity, etc.
38 enum class LogLevel : uint32_t {
43 // "DBG" is the lowest (aka most verbose) debug log level.
44 // This level is intended to be primarily used in log category settings.
45 // In your code it is usually better to use one of the finer-grained DBGn
46 // levels. In your log category settings you can then set the log category
47 // level to a specific DBGn level, or to to main DBG level to enable all DBGn
50 // This is named "DBG" rather than "DEBUG" since some open source projects
51 // define "DEBUG" as a preprocessor macro.
54 // Fine-grained debug log levels.
67 // Fine-grained info log levels.
82 // Unfortunately Windows headers #define ERROR, so we cannot use
83 // it as an enum value name. We only provide ERR instead.
88 // DFATAL log messages crash the program on debug builds.
90 // FATAL log messages always abort the program.
91 // This level is equivalent to MAX_LEVEL.
94 // The most significant bit is used by LogCategory to store a flag value,
95 // so the maximum value has that bit cleared.
97 // (We call this MAX_LEVEL instead of MAX just since MAX() is commonly
98 // defined as a preprocessor macro by some C headers.)
99 MAX_LEVEL = 0x7fffffff,
103 * Support adding and subtracting integers from LogLevels, to create slightly
104 * adjusted log level values.
106 inline constexpr LogLevel operator+(LogLevel level, uint32_t value) {
107 // Cap the result at LogLevel::MAX_LEVEL
108 return ((static_cast<uint32_t>(level) + value) >
109 static_cast<uint32_t>(LogLevel::MAX_LEVEL))
110 ? LogLevel::MAX_LEVEL
111 : static_cast<LogLevel>(static_cast<uint32_t>(level) + value);
113 inline LogLevel& operator+=(LogLevel& level, uint32_t value) {
114 level = level + value;
117 inline constexpr LogLevel operator-(LogLevel level, uint32_t value) {
118 return static_cast<LogLevel>(static_cast<uint32_t>(level) - value);
120 inline LogLevel& operator-=(LogLevel& level, uint32_t value) {
121 level = level - value;
126 * Construct a LogLevel from a string name.
128 LogLevel stringToLogLevel(folly::StringPiece name);
131 * Get a human-readable string representing the LogLevel.
133 std::string logLevelToString(LogLevel level);
136 * Print a LogLevel in a human readable format.
138 std::ostream& operator<<(std::ostream& os, LogLevel level);
141 * Returns true if and only if a LogLevel is fatal.
143 inline constexpr bool isLogLevelFatal(LogLevel level) {
144 return folly::kIsDebug ? (level >= LogLevel::DFATAL)
145 : (level >= LogLevel::FATAL);