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.
22 #include <folly/Range.h>
29 * Higher levels are more important than lower ones.
31 * However, the numbers in the DBG* level names are reversed, and can be
32 * thought of as debug verbosity levels. Increasing DBG* numbers mean
33 * increasing level of verbosity. DBG0 is the least verbose debug level,
34 * DBG1 is one level higher of verbosity, etc.
36 enum class LogLevel : uint32_t {
55 // Unfortunately Windows headers #define ERROR
56 // On Windows platforms we avoid defining ERROR. However we make it
57 // available on other platforms, to make it easier to convert code from
58 // other log libraries that also use ERROR as their log level name (e.g.,
67 // The most significant bit is used by LogCategory to store a flag value,
68 // so the maximum value has that bit cleared.
70 // (We call this MAX_LEVEL instead of MAX just since MAX() is commonly
71 // defined as a preprocessor macro by some C headers.)
72 MAX_LEVEL = 0x7fffffff,
76 * Support adding and subtracting integers from LogLevels, to create slightly
77 * adjusted log level values.
79 inline LogLevel operator+(LogLevel level, uint32_t value) {
80 auto newValue = static_cast<uint32_t>(level) + value;
81 // Cap the result at LogLevel::MAX_LEVEL
82 if (newValue > static_cast<uint32_t>(LogLevel::MAX_LEVEL)) {
83 return LogLevel::MAX_LEVEL;
85 return static_cast<LogLevel>(newValue);
87 inline LogLevel& operator+=(LogLevel& level, uint32_t value) {
88 level = level + value;
91 inline LogLevel operator-(LogLevel level, uint32_t value) {
92 return static_cast<LogLevel>(static_cast<uint32_t>(level) - value);
94 inline LogLevel& operator-=(LogLevel& level, uint32_t value) {
95 level = level - value;
100 * Comparisons between LogLevel values
102 inline bool operator<=(LogLevel a, LogLevel b) {
103 return static_cast<uint32_t>(a) <= static_cast<uint32_t>(b);
105 inline bool operator<(LogLevel a, LogLevel b) {
106 return static_cast<uint32_t>(a) < static_cast<uint32_t>(b);
108 inline bool operator>=(LogLevel a, LogLevel b) {
109 return static_cast<uint32_t>(a) >= static_cast<uint32_t>(b);
111 inline bool operator>(LogLevel a, LogLevel b) {
112 return static_cast<uint32_t>(a) > static_cast<uint32_t>(b);
116 * Construct a LogLevel from a string name.
118 LogLevel stringToLogLevel(folly::StringPiece name);
121 * Get a human-readable string representing the LogLevel.
123 std::string logLevelToString(LogLevel level);
126 * Print a LogLevel in a human readable format.
128 std::ostream& operator<<(std::ostream& os, LogLevel level);