logging: add numbered INFO* log level values
[folly.git] / folly / experimental / logging / test / LogLevelTest.cpp
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 #include <folly/Conv.h>
17 #include <folly/Random.h>
18 #include <folly/experimental/logging/LogLevel.h>
19 #include <folly/portability/GTest.h>
20
21 using namespace folly;
22
23 TEST(LogLevel, fromString) {
24   EXPECT_EQ(LogLevel::UNINITIALIZED, stringToLogLevel("uninitialized"));
25   EXPECT_EQ(LogLevel::UNINITIALIZED, stringToLogLevel("UnInitialized"));
26   EXPECT_EQ(
27       LogLevel::UNINITIALIZED, stringToLogLevel("LogLevel::UNINITIALIZED"));
28
29   EXPECT_EQ(LogLevel::NONE, stringToLogLevel("none"));
30   EXPECT_EQ(LogLevel::NONE, stringToLogLevel("NONE"));
31   EXPECT_EQ(LogLevel::NONE, stringToLogLevel("NoNe"));
32   EXPECT_EQ(LogLevel::NONE, stringToLogLevel("LogLevel::none"));
33
34   EXPECT_EQ(LogLevel::DBG, stringToLogLevel("debug"));
35   EXPECT_EQ(LogLevel::DBG, stringToLogLevel("dEBug"));
36   EXPECT_EQ(LogLevel::DBG, stringToLogLevel("Dbg"));
37   EXPECT_EQ(LogLevel::DBG, stringToLogLevel("loglevel::dEBug"));
38   EXPECT_EQ(LogLevel::DBG, stringToLogLevel("loglevel::DBG"));
39
40   EXPECT_EQ(LogLevel::INFO, stringToLogLevel("info"));
41   EXPECT_EQ(LogLevel::INFO, stringToLogLevel("INFO"));
42   EXPECT_EQ(LogLevel::INFO, stringToLogLevel("loglevel(INFO)"));
43
44   EXPECT_EQ(LogLevel::WARN, stringToLogLevel("warn"));
45   EXPECT_EQ(LogLevel::WARN, stringToLogLevel("WARN"));
46   EXPECT_EQ(LogLevel::WARN, stringToLogLevel("warning"));
47
48   EXPECT_EQ(LogLevel::ERR, stringToLogLevel("err"));
49   EXPECT_EQ(LogLevel::ERR, stringToLogLevel("eRr"));
50   EXPECT_EQ(LogLevel::ERR, stringToLogLevel("error"));
51   EXPECT_EQ(LogLevel::ERR, stringToLogLevel("ERR"));
52   EXPECT_EQ(LogLevel::ERR, stringToLogLevel("ERROR"));
53
54   EXPECT_EQ(LogLevel::CRITICAL, stringToLogLevel("critical"));
55   EXPECT_EQ(LogLevel::CRITICAL, stringToLogLevel("CRITICAL"));
56
57   EXPECT_EQ(LogLevel::DFATAL, stringToLogLevel("dfatal"));
58   EXPECT_EQ(LogLevel::DFATAL, stringToLogLevel("DFatal"));
59   EXPECT_EQ(LogLevel::DFATAL, stringToLogLevel("DFATAL"));
60
61   EXPECT_EQ(LogLevel::FATAL, stringToLogLevel("fatal"));
62   EXPECT_EQ(LogLevel::FATAL, stringToLogLevel("FaTaL"));
63   EXPECT_EQ(LogLevel::FATAL, stringToLogLevel("FATAL"));
64
65   EXPECT_EQ(LogLevel::MAX_LEVEL, stringToLogLevel("max"));
66   EXPECT_EQ(LogLevel::MAX_LEVEL, stringToLogLevel("Max_Level"));
67   EXPECT_EQ(LogLevel::MAX_LEVEL, stringToLogLevel("LogLevel::MAX"));
68   EXPECT_EQ(LogLevel::MAX_LEVEL, stringToLogLevel("LogLevel::MAX_LEVEL"));
69
70   EXPECT_EQ(LogLevel::DBG0, stringToLogLevel("dbg0"));
71   EXPECT_EQ(LogLevel::DBG5, stringToLogLevel("dbg5"));
72   EXPECT_EQ(LogLevel::DBG5, stringToLogLevel("DBG5"));
73   EXPECT_EQ(LogLevel::DBG9, stringToLogLevel("DBG9"));
74   EXPECT_EQ(LogLevel::DBG + 1, stringToLogLevel("DBG998"));
75   EXPECT_EQ(LogLevel::DBG, stringToLogLevel("DBG999"));
76   EXPECT_EQ(LogLevel::DBG, stringToLogLevel("1000"));
77   EXPECT_EQ(LogLevel::DBG, stringToLogLevel("LogLevel(1000)"));
78
79   EXPECT_EQ(LogLevel::INFO0, stringToLogLevel("info0"));
80   EXPECT_EQ(LogLevel::INFO5, stringToLogLevel("INFO5"));
81   EXPECT_EQ(LogLevel::INFO5, stringToLogLevel("INFO5"));
82   EXPECT_EQ(LogLevel::INFO9, stringToLogLevel("info9"));
83   EXPECT_EQ(LogLevel::INFO + 1, stringToLogLevel("Info998"));
84   EXPECT_EQ(LogLevel::INFO, stringToLogLevel("INFO999"));
85   EXPECT_EQ(LogLevel::INFO, stringToLogLevel("2000"));
86   EXPECT_EQ(LogLevel::INFO6, stringToLogLevel("LogLevel(2993)"));
87
88   EXPECT_THROW(stringToLogLevel("foobar"), std::range_error);
89   EXPECT_THROW(stringToLogLevel("dbgx"), std::range_error);
90   EXPECT_THROW(stringToLogLevel("dbgxyz"), std::range_error);
91   EXPECT_THROW(stringToLogLevel("dbg-1"), std::range_error);
92   EXPECT_THROW(stringToLogLevel("dbg12345"), std::range_error);
93   EXPECT_THROW(stringToLogLevel("900z"), std::range_error);
94 }
95
96 TEST(LogLevel, toString) {
97   EXPECT_EQ("UNINITIALIZED", logLevelToString(LogLevel::UNINITIALIZED));
98   EXPECT_EQ("NONE", logLevelToString(LogLevel::NONE));
99   EXPECT_EQ("INFO", logLevelToString(LogLevel::INFO));
100   EXPECT_EQ("WARN", logLevelToString(LogLevel::WARN));
101   EXPECT_EQ("WARN", logLevelToString(LogLevel::WARNING));
102   EXPECT_EQ("DEBUG", logLevelToString(LogLevel::DBG));
103   EXPECT_EQ("ERR", logLevelToString(LogLevel::ERR));
104   EXPECT_EQ("CRITICAL", logLevelToString(LogLevel::CRITICAL));
105   EXPECT_EQ("DFATAL", logLevelToString(LogLevel::DFATAL));
106   EXPECT_EQ("FATAL", logLevelToString(LogLevel::FATAL));
107   EXPECT_EQ("FATAL", logLevelToString(LogLevel::MAX_LEVEL));
108
109   EXPECT_EQ("DBG0", logLevelToString(LogLevel::DBG0));
110   EXPECT_EQ("DBG2", logLevelToString(LogLevel::DBG2));
111   EXPECT_EQ("DBG5", logLevelToString(LogLevel::DBG5));
112   EXPECT_EQ("DBG9", logLevelToString(LogLevel::DBG9));
113   EXPECT_EQ("DBG96", logLevelToString(static_cast<LogLevel>(1903)));
114   EXPECT_EQ("DBG64", logLevelToString(LogLevel::DBG4 - 60));
115
116   EXPECT_EQ("INFO0", logLevelToString(LogLevel::INFO0));
117   EXPECT_EQ("INFO2", logLevelToString(LogLevel::INFO2));
118   EXPECT_EQ("INFO5", logLevelToString(LogLevel::INFO5));
119   EXPECT_EQ("INFO9", logLevelToString(LogLevel::INFO9));
120   EXPECT_EQ("INFO86", logLevelToString(static_cast<LogLevel>(2913)));
121   EXPECT_EQ("INFO57", logLevelToString(LogLevel::INFO7 - 50));
122
123   EXPECT_EQ("LogLevel(123)", logLevelToString(static_cast<LogLevel>(123)));
124 }
125
126 TEST(LogLevel, toStringAndBack) {
127   // Check that stringToLogLevel(logLevelToString()) is the identity function
128   auto checkLevel = [](LogLevel level) {
129     auto stringForm = logLevelToString(level);
130     auto outputLevel = stringToLogLevel(stringForm);
131     EXPECT_EQ(level, outputLevel)
132         << "error converting " << level << " (" << static_cast<uint32_t>(level)
133         << ") to string and back.  String is " << stringForm;
134   };
135
136   // Check all of the named levels
137   checkLevel(LogLevel::UNINITIALIZED);
138   checkLevel(LogLevel::NONE);
139   checkLevel(LogLevel::DBG);
140   checkLevel(LogLevel::DBG0);
141   checkLevel(LogLevel::DBG1);
142   checkLevel(LogLevel::DBG2);
143   checkLevel(LogLevel::DBG3);
144   checkLevel(LogLevel::DBG4);
145   checkLevel(LogLevel::DBG5);
146   checkLevel(LogLevel::DBG6);
147   checkLevel(LogLevel::DBG7);
148   checkLevel(LogLevel::DBG8);
149   checkLevel(LogLevel::DBG9);
150   checkLevel(LogLevel::INFO);
151   checkLevel(LogLevel::WARN);
152   checkLevel(LogLevel::WARNING);
153   checkLevel(LogLevel::ERR);
154   checkLevel(LogLevel::CRITICAL);
155   checkLevel(LogLevel::DFATAL);
156   checkLevel(LogLevel::FATAL);
157
158   // Try with some random integer values
159   for (uint32_t numIters = 0; numIters < 10000; ++numIters) {
160     auto levelValue =
161         folly::Random::rand32(static_cast<uint32_t>(LogLevel::MAX_LEVEL));
162     checkLevel(static_cast<LogLevel>(levelValue));
163   }
164 }