logging: add LoggerDB::flushAllHandlers()
[folly.git] / folly / experimental / logging / test / LoggerDBTest.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/experimental/logging/Logger.h>
17 #include <folly/experimental/logging/LoggerDB.h>
18 #include <folly/experimental/logging/test/TestLogHandler.h>
19 #include <folly/portability/GTest.h>
20
21 using namespace folly;
22
23 TEST(LoggerDB, lookupNameCanonicalization) {
24   LoggerDB db{LoggerDB::TESTING};
25   Logger foo{&db, "foo"};
26   Logger foo2{&db, "..foo.."};
27   EXPECT_EQ(foo.getCategory(), foo2.getCategory());
28
29   Logger fooBar{&db, "foo.bar"};
30   Logger fooBar2{&db, ".foo..bar"};
31   EXPECT_EQ(fooBar.getCategory(), fooBar2.getCategory());
32 }
33
34 TEST(LoggerDB, getCategory) {
35   LoggerDB db{LoggerDB::TESTING};
36 }
37
38 TEST(LoggerDB, flushAllHandlers) {
39   LoggerDB db{LoggerDB::TESTING};
40   auto* cat1 = db.getCategory("foo");
41   auto* cat2 = db.getCategory("foo.bar.test");
42   auto* cat3 = db.getCategory("hello.world");
43   auto* cat4 = db.getCategory("other.category");
44
45   auto h1 = std::make_shared<TestLogHandler>();
46   auto h2 = std::make_shared<TestLogHandler>();
47   auto h3 = std::make_shared<TestLogHandler>();
48
49   cat1->addHandler(h1);
50
51   cat2->addHandler(h2);
52   cat2->addHandler(h3);
53
54   cat3->addHandler(h1);
55   cat3->addHandler(h2);
56   cat3->addHandler(h3);
57
58   cat4->addHandler(h1);
59
60   EXPECT_EQ(0, h1->getFlushCount());
61   EXPECT_EQ(0, h2->getFlushCount());
62   EXPECT_EQ(0, h3->getFlushCount());
63
64   // Calling flushAllHandlers() should only flush each handler once,
65   // even when they are attached to multiple categories.
66   db.flushAllHandlers();
67   EXPECT_EQ(1, h1->getFlushCount());
68   EXPECT_EQ(1, h2->getFlushCount());
69   EXPECT_EQ(1, h3->getFlushCount());
70
71   db.flushAllHandlers();
72   EXPECT_EQ(2, h1->getFlushCount());
73   EXPECT_EQ(2, h2->getFlushCount());
74   EXPECT_EQ(2, h3->getFlushCount());
75 }
76
77 TEST(LoggerDB, processConfigString) {
78   LoggerDB db{LoggerDB::TESTING};
79   db.processConfigString("foo.bar=dbg5");
80   EXPECT_EQ(LogLevel::DBG5, db.getCategory("foo.bar")->getLevel());
81   EXPECT_EQ(LogLevel::DBG5, db.getCategory("foo.bar")->getEffectiveLevel());
82   EXPECT_EQ(LogLevel::MAX_LEVEL, db.getCategory("foo")->getLevel());
83   EXPECT_EQ(LogLevel::ERROR, db.getCategory("foo")->getEffectiveLevel());
84   EXPECT_EQ(LogLevel::ERROR, db.getCategory("")->getLevel());
85   EXPECT_EQ(LogLevel::ERROR, db.getCategory("")->getEffectiveLevel());
86
87   EXPECT_EQ(LogLevel::MAX_LEVEL, db.getCategory("foo.bar.test")->getLevel());
88   EXPECT_EQ(
89       LogLevel::DBG5, db.getCategory("foo.bar.test")->getEffectiveLevel());
90
91   db.processConfigString("sys=warn,foo.test=debug,foo.test.stuff=warn");
92   EXPECT_EQ(LogLevel::WARN, db.getCategory("sys")->getLevel());
93   EXPECT_EQ(LogLevel::WARN, db.getCategory("sys")->getEffectiveLevel());
94   EXPECT_EQ(LogLevel::DEBUG, db.getCategory("foo.test")->getLevel());
95   EXPECT_EQ(LogLevel::DEBUG, db.getCategory("foo.test")->getEffectiveLevel());
96   EXPECT_EQ(LogLevel::WARN, db.getCategory("foo.test.stuff")->getLevel());
97   EXPECT_EQ(
98       LogLevel::DEBUG, db.getCategory("foo.test.stuff")->getEffectiveLevel());
99 }