logging: add a LogHandler::getConfig() method
[folly.git] / folly / experimental / logging / test / TestLogHandler.h
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 #pragma once
17
18 #include <map>
19 #include <string>
20 #include <utility>
21 #include <vector>
22
23 #include <folly/experimental/logging/LogHandler.h>
24 #include <folly/experimental/logging/LogHandlerConfig.h>
25 #include <folly/experimental/logging/LogMessage.h>
26
27 namespace folly {
28
29 /**
30  * A LogHandler that simply keeps a vector of all LogMessages it receives.
31  *
32  * This class is not thread-safe.  It is intended to be used in single-threaded
33  * tests.
34  */
35 class TestLogHandler : public LogHandler {
36  public:
37   TestLogHandler() : config_{"test"} {}
38   explicit TestLogHandler(LogHandlerConfig config)
39       : config_{std::move(config)} {}
40
41   std::vector<std::pair<LogMessage, const LogCategory*>>& getMessages() {
42     return messages_;
43   }
44
45   void handleMessage(
46       const LogMessage& message,
47       const LogCategory* handlerCategory) override {
48     messages_.emplace_back(message, handlerCategory);
49   }
50
51   void flush() override {
52     ++flushCount_;
53   }
54
55   uint64_t getFlushCount() const {
56     return flushCount_;
57   }
58
59   LogHandlerConfig getConfig() const override {
60     return config_;
61   }
62
63  private:
64   std::vector<std::pair<LogMessage, const LogCategory*>> messages_;
65   uint64_t flushCount_{0};
66   std::map<std::string, std::string> options_;
67   LogHandlerConfig config_;
68 };
69 } // namespace folly