add a new logging library
[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   NONE = 0,
38
39   DEBUG = 900,
40   DBG0 = 1000,
41   DBG1 = 999,
42   DBG2 = 998,
43   DBG3 = 997,
44   DBG4 = 996,
45   DBG5 = 995,
46   DBG6 = 994,
47   DBG7 = 993,
48   DBG8 = 992,
49   DBG9 = 991,
50
51   INFO = 2000,
52   WARN = 3000,
53   WARNING = 3000,
54
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.,
59   // glog).
60   ERR = 4000,
61 #ifndef ERROR
62   ERROR = 4000,
63 #endif
64
65   CRITICAL = 5000,
66
67   // The most significant bit is used by LogCategory to store a flag value,
68   // so the maximum value has that bit cleared.
69   //
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,
73 };
74
75 /*
76  * Support adding and subtracting integers from LogLevels, to create slightly
77  * adjusted log level values.
78  */
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;
84   }
85   return static_cast<LogLevel>(newValue);
86 }
87 inline LogLevel& operator+=(LogLevel& level, uint32_t value) {
88   level = level + value;
89   return level;
90 }
91 inline LogLevel operator-(LogLevel level, uint32_t value) {
92   return static_cast<LogLevel>(static_cast<uint32_t>(level) - value);
93 }
94 inline LogLevel& operator-=(LogLevel& level, uint32_t value) {
95   level = level - value;
96   return level;
97 }
98
99 /*
100  * Comparisons between LogLevel values
101  */
102 inline bool operator<=(LogLevel a, LogLevel b) {
103   return static_cast<uint32_t>(a) <= static_cast<uint32_t>(b);
104 }
105 inline bool operator<(LogLevel a, LogLevel b) {
106   return static_cast<uint32_t>(a) < static_cast<uint32_t>(b);
107 }
108 inline bool operator>=(LogLevel a, LogLevel b) {
109   return static_cast<uint32_t>(a) >= static_cast<uint32_t>(b);
110 }
111 inline bool operator>(LogLevel a, LogLevel b) {
112   return static_cast<uint32_t>(a) > static_cast<uint32_t>(b);
113 }
114
115 /**
116  * Construct a LogLevel from a string name.
117  */
118 LogLevel stringToLogLevel(folly::StringPiece name);
119
120 /**
121  * Get a human-readable string representing the LogLevel.
122  */
123 std::string logLevelToString(LogLevel level);
124
125 /**
126  * Print a LogLevel in a human readable format.
127  */
128 std::ostream& operator<<(std::ostream& os, LogLevel level);
129 }