add a new logging library
[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::NONE, stringToLogLevel("none"));
25   EXPECT_EQ(LogLevel::NONE, stringToLogLevel("NONE"));
26   EXPECT_EQ(LogLevel::NONE, stringToLogLevel("NoNe"));
27   EXPECT_EQ(LogLevel::NONE, stringToLogLevel("LogLevel::none"));
28
29   EXPECT_EQ(LogLevel::DEBUG, stringToLogLevel("debug"));
30   EXPECT_EQ(LogLevel::DEBUG, stringToLogLevel("dEBug"));
31   EXPECT_EQ(LogLevel::DEBUG, stringToLogLevel("loglevel::dEBug"));
32
33   EXPECT_EQ(LogLevel::INFO, stringToLogLevel("info"));
34   EXPECT_EQ(LogLevel::INFO, stringToLogLevel("INFO"));
35   EXPECT_EQ(LogLevel::INFO, stringToLogLevel("loglevel(INFO)"));
36
37   EXPECT_EQ(LogLevel::WARN, stringToLogLevel("warn"));
38   EXPECT_EQ(LogLevel::WARN, stringToLogLevel("WARN"));
39   EXPECT_EQ(LogLevel::WARN, stringToLogLevel("warning"));
40
41   EXPECT_EQ(LogLevel::ERR, stringToLogLevel("err"));
42   EXPECT_EQ(LogLevel::ERR, stringToLogLevel("eRr"));
43   EXPECT_EQ(LogLevel::ERR, stringToLogLevel("error"));
44   EXPECT_EQ(LogLevel::ERR, stringToLogLevel("ERROR"));
45
46   EXPECT_EQ(LogLevel::CRITICAL, stringToLogLevel("critical"));
47   EXPECT_EQ(LogLevel::CRITICAL, stringToLogLevel("CRITICAL"));
48
49   EXPECT_EQ(LogLevel::MAX_LEVEL, stringToLogLevel("max"));
50   EXPECT_EQ(LogLevel::MAX_LEVEL, stringToLogLevel("Max_Level"));
51   EXPECT_EQ(LogLevel::MAX_LEVEL, stringToLogLevel("LogLevel::MAX"));
52   EXPECT_EQ(LogLevel::MAX_LEVEL, stringToLogLevel("LogLevel::MAX_LEVEL"));
53
54   EXPECT_EQ(LogLevel::DBG0, stringToLogLevel("dbg0"));
55   EXPECT_EQ(LogLevel::DBG5, stringToLogLevel("dbg5"));
56   EXPECT_EQ(LogLevel::DBG5, stringToLogLevel("DBG5"));
57   EXPECT_EQ(LogLevel::DBG9, stringToLogLevel("DBG9"));
58   EXPECT_EQ(LogLevel::DEBUG + 1, stringToLogLevel("DBG99"));
59   EXPECT_EQ(LogLevel::DEBUG, stringToLogLevel("900"));
60   EXPECT_EQ(LogLevel::DEBUG, stringToLogLevel("LogLevel(900)"));
61
62   EXPECT_THROW(stringToLogLevel("foobar"), std::range_error);
63   EXPECT_THROW(stringToLogLevel("dbg"), std::range_error);
64   EXPECT_THROW(stringToLogLevel("dbgxyz"), std::range_error);
65   EXPECT_THROW(stringToLogLevel("dbg-1"), std::range_error);
66   EXPECT_THROW(stringToLogLevel("dbg12345"), std::range_error);
67   EXPECT_THROW(stringToLogLevel("900z"), std::range_error);
68 }
69
70 TEST(LogLevel, toString) {
71   EXPECT_EQ("LogLevel::NONE", logLevelToString(LogLevel::NONE));
72   EXPECT_EQ("LogLevel::INFO", logLevelToString(LogLevel::INFO));
73   EXPECT_EQ("LogLevel::WARN", logLevelToString(LogLevel::WARN));
74   EXPECT_EQ("LogLevel::WARN", logLevelToString(LogLevel::WARNING));
75   EXPECT_EQ("LogLevel::DEBUG", logLevelToString(LogLevel::DEBUG));
76   EXPECT_EQ("LogLevel::ERR", logLevelToString(LogLevel::ERR));
77   EXPECT_EQ("LogLevel::CRITICAL", logLevelToString(LogLevel::CRITICAL));
78   EXPECT_EQ("LogLevel::MAX_LEVEL", logLevelToString(LogLevel::MAX_LEVEL));
79
80   EXPECT_EQ("LogLevel::DBG0", logLevelToString(LogLevel::DBG0));
81   EXPECT_EQ("LogLevel::DBG2", logLevelToString(LogLevel::DBG2));
82   EXPECT_EQ("LogLevel::DBG5", logLevelToString(LogLevel::DBG5));
83   EXPECT_EQ("LogLevel::DBG9", logLevelToString(LogLevel::DBG9));
84   EXPECT_EQ("LogLevel::DBG97", logLevelToString(static_cast<LogLevel>(903)));
85   EXPECT_EQ("LogLevel::DBG64", logLevelToString(LogLevel::DBG4 - 60));
86
87   EXPECT_EQ("LogLevel(1234)", logLevelToString(static_cast<LogLevel>(1234)));
88 }
89
90 TEST(LogLevel, toStringAndBack) {
91   // Check that stringToLogLevel(logLevelToString()) is the identity function
92   auto checkLevel = [](LogLevel level) {
93     auto stringForm = logLevelToString(level);
94     auto outputLevel = stringToLogLevel(stringForm);
95     EXPECT_EQ(level, outputLevel)
96         << "error converting " << level << " (" << static_cast<uint32_t>(level)
97         << ") to string and back.  String is " << stringForm;
98   };
99
100   // Check all of the named levels
101   checkLevel(LogLevel::NONE);
102   checkLevel(LogLevel::DEBUG);
103   checkLevel(LogLevel::DBG0);
104   checkLevel(LogLevel::DBG1);
105   checkLevel(LogLevel::DBG2);
106   checkLevel(LogLevel::DBG3);
107   checkLevel(LogLevel::DBG4);
108   checkLevel(LogLevel::DBG5);
109   checkLevel(LogLevel::DBG6);
110   checkLevel(LogLevel::DBG7);
111   checkLevel(LogLevel::DBG8);
112   checkLevel(LogLevel::DBG9);
113   checkLevel(LogLevel::INFO);
114   checkLevel(LogLevel::WARN);
115   checkLevel(LogLevel::WARNING);
116   checkLevel(LogLevel::ERR);
117   checkLevel(LogLevel::CRITICAL);
118   checkLevel(LogLevel::MAX_LEVEL);
119
120   // Try with some random integer values
121   for (uint32_t numIters = 0; numIters < 10000; ++numIters) {
122     auto levelValue =
123         folly::Random::rand32(static_cast<uint32_t>(LogLevel::MAX_LEVEL));
124     checkLevel(static_cast<LogLevel>(levelValue));
125   }
126 }