SSP21-CPP
MockLogHandler.h
1 /*
2  * Copyright (c) 2019, Automatak LLC
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
6  * following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following
9  * disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following
12  * disclaimer in the documentation and/or other materials provided with the distribution.
13  *
14  * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote
15  * products derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
18  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
23  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25 #ifndef LOG4CPP_MOCKLOGHANDLER_H
26 #define LOG4CPP_MOCKLOGHANDLER_H
27 
28 #include "log4cpp/Logger.h"
29 #include "ser4cpp/util/Uncopyable.h"
30 
31 #include "catch.hpp"
32 
33 #include <deque>
34 #include <iostream>
35 
36 namespace log4cpp
37 {
39  {
40  struct Backend final : public ILogHandler
41  {
42  void log(ModuleId module, const char* id, LogLevel level, char const* location, char const* message) override
43  {
44  if (output_to_stdio)
45  {
46  std::cout << message << std::endl;
47  }
48 
49  lines.push_back(message);
50  }
51 
52  bool output_to_stdio = false;
53  std::deque<std::string> lines;
54  };
55 
56  public:
57  MockLogHandler(const std::string& id, LogLevels levels = LogLevels::everything()) :
58  backend(std::make_shared<Backend>()),
59  logger(backend, ModuleId(0), id, levels)
60  {}
61 
62  void print_output()
63  {
64  this->backend->output_to_stdio = true;
65  }
66 
67  virtual void log(ModuleId module, const char* id, LogLevel level, char const* location, char const* message) override
68  {
69  this->backend->log(module, id, level, location, message);
70  }
71 
72  template <typename... Args>
73  void expect(const std::string& expected, const Args& ... args)
74  {
75  REQUIRE_FALSE(backend->lines.empty());
76  REQUIRE(expected == backend->lines.front());
77  backend->lines.pop_front();
78  expect(args ...);
79  }
80 
81  void expect()
82  {
83  REQUIRE(backend->lines.empty());
84  }
85 
86  private:
87  std::shared_ptr<Backend> backend;
88 
89  MockLogHandler() = delete;
90 
91  public:
92  log4cpp::Logger logger;
93  };
94 
95 } //namespace log4cpp
96 
97 #endif //LOG4CPP_MOCKLOGHANDLER_H
log4cpp header-only library namespace
virtual void log(ModuleId module, const char *id, LogLevel level, char const *location, char const *message) override