c5f1920321c68a05f363390f3573e875e0f67dc8
[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
22 #include <folly/Range.h>
23
24 namespace folly {
25
26 /**
27  * Log level values.
28  *
29  * Higher levels are more important than lower ones.
30  *
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.
35  */
36 enum class LogLevel : uint32_t {
37   UNINITIALIZED = 0,
38   NONE = 1,
39   MIN_LEVEL = 1,
40
41   DEBUG = 900,
42   DBG0 = 1000,
43   DBG1 = 999,
44   DBG2 = 998,
45   DBG3 = 997,
46   DBG4 = 996,
47   DBG5 = 995,
48   DBG6 = 994,
49   DBG7 = 993,
50   DBG8 = 992,
51   DBG9 = 991,
52
53   INFO = 2000,
54   WARN = 3000,
55   WARNING = 3000,
56
57   // Unfortunately Windows headers #define ERROR
58   // On Windows platforms we avoid defining ERROR.  However we make it
59   // available on other platforms, to make it easier to convert code from
60   // other log libraries that also use ERROR as their log level name (e.g.,
61   // glog).
62   ERR = 4000,
63 #ifndef ERROR
64   ERROR = 4000,
65 #endif
66
67   CRITICAL = 5000,
68
69   // The most significant bit is used by LogCategory to store a flag value,
70   // so the maximum value has that bit cleared.
71   //
72   // (We call this MAX_LEVEL instead of MAX just since MAX() is commonly
73   // defined as a preprocessor macro by some C headers.)
74   MAX_LEVEL = 0x7fffffff,
75 };
76
77 /*
78  * Support adding and subtracting integers from LogLevels, to create slightly
79  * adjusted log level values.
80  */
81 inline LogLevel operator+(LogLevel level, uint32_t value) {
82   auto newValue = static_cast<uint32_t>(level) + value;
83   // Cap the result at LogLevel::MAX_LEVEL
84   if (newValue > static_cast<uint32_t>(LogLevel::MAX_LEVEL)) {
85     return LogLevel::MAX_LEVEL;
86   }
87   return static_cast<LogLevel>(newValue);
88 }
89 inline LogLevel& operator+=(LogLevel& level, uint32_t value) {
90   level = level + value;
91   return level;
92 }
93 inline LogLevel operator-(LogLevel level, uint32_t value) {
94   return static_cast<LogLevel>(static_cast<uint32_t>(level) - value);
95 }
96 inline LogLevel& operator-=(LogLevel& level, uint32_t value) {
97   level = level - value;
98   return level;
99 }
100
101 /*
102  * Comparisons between LogLevel values
103  */
104 inline bool operator<=(LogLevel a, LogLevel b) {
105   return static_cast<uint32_t>(a) <= static_cast<uint32_t>(b);
106 }
107 inline bool operator<(LogLevel a, LogLevel b) {
108   return static_cast<uint32_t>(a) < static_cast<uint32_t>(b);
109 }
110 inline bool operator>=(LogLevel a, LogLevel b) {
111   return static_cast<uint32_t>(a) >= static_cast<uint32_t>(b);
112 }
113 inline bool operator>(LogLevel a, LogLevel b) {
114   return static_cast<uint32_t>(a) > static_cast<uint32_t>(b);
115 }
116
117 /**
118  * Construct a LogLevel from a string name.
119  */
120 LogLevel stringToLogLevel(folly::StringPiece name);
121
122 /**
123  * Get a human-readable string representing the LogLevel.
124  */
125 std::string logLevelToString(LogLevel level);
126
127 /**
128  * Print a LogLevel in a human readable format.
129  */
130 std::ostream& operator<<(std::ostream& os, LogLevel level);
131 }