proxygen
LogLevelTest.cpp File Reference

Go to the source code of this file.

Functions

 TEST (LogLevel, fromString)
 
 TEST (LogLevel, toString)
 
 TEST (LogLevel, toStringAndBack)
 

Function Documentation

TEST ( LogLevel  ,
fromString   
)

Definition at line 23 of file LogLevelTest.cpp.

References folly::CRITICAL, folly::DBG, folly::DBG0, folly::DBG5, folly::DBG9, folly::DFATAL, folly::ERR, EXPECT_EQ, EXPECT_THROW, folly::FATAL, folly::INFO, folly::INFO0, folly::INFO5, folly::INFO6, folly::INFO9, folly::MAX_LEVEL, folly::NONE, folly::stringToLogLevel(), folly::UNINITIALIZED, and folly::WARN.

23  {
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 }
#define EXPECT_THROW(statement, expected_exception)
Definition: gtest.h:1843
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
LogLevel stringToLogLevel(StringPiece name)
Definition: LogLevel.cpp:42
TEST ( LogLevel  ,
toString   
)

Definition at line 96 of file LogLevelTest.cpp.

References folly::CRITICAL, folly::DBG, folly::DBG0, folly::DBG2, folly::DBG4, folly::DBG5, folly::DBG9, folly::DFATAL, folly::ERR, EXPECT_EQ, folly::FATAL, folly::INFO, folly::INFO0, folly::INFO2, folly::INFO5, folly::INFO7, folly::INFO9, folly::logLevelToString(), folly::MAX_LEVEL, folly::NONE, folly::UNINITIALIZED, folly::WARN, and folly::WARNING.

96  {
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 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
string logLevelToString(LogLevel level)
Definition: LogLevel.cpp:109
TEST ( LogLevel  ,
toStringAndBack   
)

Definition at line 126 of file LogLevelTest.cpp.

References folly::CRITICAL, folly::DBG, folly::DBG0, folly::DBG1, folly::DBG2, folly::DBG3, folly::DBG4, folly::DBG5, folly::DBG6, folly::DBG7, folly::DBG8, folly::DBG9, folly::DFATAL, folly::ERR, EXPECT_EQ, folly::FATAL, folly::INFO, folly::logLevelToString(), folly::MAX_LEVEL, folly::NONE, folly::Random::rand32(), folly::stringToLogLevel(), uint32_t, folly::UNINITIALIZED, folly::WARN, and folly::WARNING.

126  {
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 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
LogLevel stringToLogLevel(StringPiece name)
Definition: LogLevel.cpp:42
string logLevelToString(LogLevel level)
Definition: LogLevel.cpp:109
LogLevel
Definition: LogLevel.h:38
static uint32_t rand32()
Definition: Random.h:213