logging: improve the AsyncFileWriter flush test()
[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 <utility>
19 #include <vector>
20
21 #include <folly/experimental/logging/LogHandler.h>
22 #include <folly/experimental/logging/LogMessage.h>
23
24 namespace folly {
25
26 /**
27  * A LogHandler that simply keeps a vector of all LogMessages it receives.
28  *
29  * This class is not thread-safe.  It is intended to be used in single-threaded
30  * tests.
31  */
32 class TestLogHandler : public LogHandler {
33  public:
34   std::vector<std::pair<LogMessage, const LogCategory*>>& getMessages() {
35     return messages_;
36   }
37
38   void handleMessage(
39       const LogMessage& message,
40       const LogCategory* handlerCategory) override {
41     messages_.emplace_back(message, handlerCategory);
42   }
43
44   void flush() override {
45     ++flushCount_;
46   }
47
48   uint64_t getFlushCount() const {
49     return flushCount_;
50   }
51
52  private:
53   std::vector<std::pair<LogMessage, const LogCategory*>> messages_;
54   uint64_t flushCount_{0};
55 };
56 }