2 * Copyright 2004-present Facebook, Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
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>
21 using namespace folly;
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());
29 Logger fooBar{&db, "foo.bar"};
30 Logger fooBar2{&db, ".foo..bar"};
31 EXPECT_EQ(fooBar.getCategory(), fooBar2.getCategory());
34 TEST(LoggerDB, getCategory) {
35 LoggerDB db{LoggerDB::TESTING};
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");
45 auto h1 = std::make_shared<TestLogHandler>();
46 auto h2 = std::make_shared<TestLogHandler>();
47 auto h3 = std::make_shared<TestLogHandler>();
60 EXPECT_EQ(0, h1->getFlushCount());
61 EXPECT_EQ(0, h2->getFlushCount());
62 EXPECT_EQ(0, h3->getFlushCount());
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());
71 db.flushAllHandlers();
72 EXPECT_EQ(2, h1->getFlushCount());
73 EXPECT_EQ(2, h2->getFlushCount());
74 EXPECT_EQ(2, h3->getFlushCount());