proxygen
InitTest.cpp
Go to the documentation of this file.
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/logging/Init.h>
17 
19 #include <folly/logging/LoggerDB.h>
23 #include <folly/test/TestUtils.h>
24 
25 using folly::initLogging;
26 using folly::LoggerDB;
28 
29 namespace {
30 // A counter to help confirm that our getBaseLoggingConfigCalled() was invoked
31 // rather than the default implementation that folly exports as a weak symbol.
32 unsigned int getBaseLoggingConfigCalled;
33 } // namespace
34 
35 namespace folly {
36 
37 const char* getBaseLoggingConfig() {
38  ++getBaseLoggingConfigCalled;
39  return "folly=INFO; default:stream=stdout";
40 }
41 
42 } // namespace folly
43 
44 TEST(Init, checkConfig) {
45  // Before we call initLogging(), the LoggerDB will have the default
46  // configuration provided by initializeLoggerDB().
47  auto initialConfig = folly::LoggerDB::get().getConfig();
48  EXPECT_EQ(0, getBaseLoggingConfigCalled);
49  EXPECT_EQ(
50  parseLogConfig(".:=INFO:default; "
51  "default=stream:stream=stderr,async=false"),
52  LoggerDB::get().getConfig());
53 
54  // Call initLogging()
55  // Make sure it merges the supplied config argument with our custom
56  // base configuration.
57  initLogging(".=ERROR,folly.logging=DBG7");
58  EXPECT_EQ(1, getBaseLoggingConfigCalled);
59  EXPECT_EQ(
60  parseLogConfig(".:=ERROR:default,folly=INFO:,folly.logging=DBG7:; "
61  "default=stream:stream=stdout,async=false"),
62  LoggerDB::get().getConfig());
63 
64  // Test calling initLogging() with bad configuration strings, and
65  // configured such that it should throw an exception on error rather than
66  // exiting.
67  //
68  // Note that it is okay to call initLogging() multiple times (we already
69  // called it successfully once above), but this isn't really something to
70  // expect most callers to want to do.
72  initLogging(".=BOGUSLEVEL"),
74  R"(invalid log level "BOGUSLEVEL")");
76  initLogging(".=ERR:undefined_handler"),
77  std::invalid_argument,
78  R"(unknown log handler "undefined_handler")");
79 }
80 
81 // We use our custom main() to ensure that folly::initLogging() has
82 // not been called yet when we start running the tests.
83 int main(int argc, char** argv) {
84  ::testing::InitGoogleTest(&argc, argv);
85  gflags::ParseCommandLineFlags(&argc, &argv, /* remove_flags = */ true);
86 
87  return RUN_ALL_TESTS();
88 }
int RUN_ALL_TESTS() GTEST_MUST_USE_RESULT_
Definition: gtest.h:2232
#define EXPECT_THROW_RE(statement, exceptionType, pattern)
Definition: TestUtils.h:119
const char * getBaseLoggingConfig()
Definition: InitWeak.cpp:29
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
LogConfig getConfig() const
Definition: LoggerDB.cpp:158
—— Concurrent Priority Queue Implementation ——
Definition: AtomicBitSet.h:29
LogConfig parseLogConfig(StringPiece value)
char ** argv
int main(int argc, char **argv)
Definition: InitTest.cpp:83
GTEST_API_ void InitGoogleTest(int *argc, char **argv)
Definition: gtest.cc:5370
void initLogging(StringPiece configString)
Definition: Init.cpp:25
TEST(SequencedExecutor, CPUThreadPoolExecutor)
static LoggerDB & get()
Definition: LoggerDB.cpp:112