0c751b15bd33af5f1390aa279c7a227fdde294da
[folly.git] / folly / experimental / logging / LogLevel.h
1 /*
2  * Copyright 2004-present Facebook, Inc.
3  *
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
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 #pragma once
17
18 #include <cstdint>
19 #include <iosfwd>
20 #include <string>
21 #include <type_traits>
22
23 #include <folly/Portability.h>
24 #include <folly/Range.h>
25
26 namespace folly {
27
28 /**
29  * Log level values.
30  *
31  * Higher levels are more important than lower ones.
32  *
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.
37  */
38 enum class LogLevel : uint32_t {
39   UNINITIALIZED = 0,
40   NONE = 1,
41   MIN_LEVEL = 1,
42
43   DEBUG = 900,
44   DBG0 = 1000,
45   DBG1 = 999,
46   DBG2 = 998,
47   DBG3 = 997,
48   DBG4 = 996,
49   DBG5 = 995,
50   DBG6 = 994,
51   DBG7 = 993,
52   DBG8 = 992,
53   DBG9 = 991,
54
55   INFO = 2000,
56   WARN = 3000,
57   WARNING = 3000,
58
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.,
63   // glog).
64   ERR = 4000,
65 #ifndef ERROR
66   ERROR = 4000,
67 #endif
68
69   CRITICAL = 5000,
70
71   // DFATAL log messages crash the program on debug builds.
72   DFATAL = 0x7ffffffe,
73   // FATAL log messages always abort the program.
74   // This level is equivalent to MAX_LEVEL.
75   FATAL = 0x7fffffff,
76
77   // The most significant bit is used by LogCategory to store a flag value,
78   // so the maximum value has that bit cleared.
79   //
80   // (We call this MAX_LEVEL instead of MAX just since MAX() is commonly
81   // defined as a preprocessor macro by some C headers.)
82   MAX_LEVEL = 0x7fffffff,
83 };
84
85 /*
86  * Support adding and subtracting integers from LogLevels, to create slightly
87  * adjusted log level values.
88  */
89 inline constexpr LogLevel operator+(LogLevel level, uint32_t value) {
90   auto newValue = static_cast<uint32_t>(level) + value;
91   // Cap the result at LogLevel::MAX_LEVEL
92   if (newValue > static_cast<uint32_t>(LogLevel::MAX_LEVEL)) {
93     return LogLevel::MAX_LEVEL;
94   }
95   return static_cast<LogLevel>(newValue);
96 }
97 inline LogLevel& operator+=(LogLevel& level, uint32_t value) {
98   level = level + value;
99   return level;
100 }
101 inline constexpr LogLevel operator-(LogLevel level, uint32_t value) {
102   return static_cast<LogLevel>(static_cast<uint32_t>(level) - value);
103 }
104 inline LogLevel& operator-=(LogLevel& level, uint32_t value) {
105   level = level - value;
106   return level;
107 }
108
109 /**
110  * Construct a LogLevel from a string name.
111  */
112 LogLevel stringToLogLevel(folly::StringPiece name);
113
114 /**
115  * Get a human-readable string representing the LogLevel.
116  */
117 std::string logLevelToString(LogLevel level);
118
119 /**
120  * Print a LogLevel in a human readable format.
121  */
122 std::ostream& operator<<(std::ostream& os, LogLevel level);
123
124 /**
125  * Returns true if and only if a LogLevel is fatal.
126  */
127 inline constexpr bool isLogLevelFatal(LogLevel level) {
128   if (folly::kIsDebug) {
129     return level >= LogLevel::DFATAL;
130   } else {
131     return level >= LogLevel::FATAL;
132   }
133 }
134 }