1//
2// Copyright(c) 2018 Gabi Melman.
3// Distributed under the MIT License (http://opensource.org/licenses/MIT)
4//
5
6#pragma once
7
8#include "spdlog/details/null_mutex.h"
9#include "spdlog/sinks/base_sink.h"
10
11#include <chrono>
12#include <mutex>
13#include <thread>
14
15namespace spdlog {
16namespace sinks {
17
18template<class Mutex>
19class test_sink : public base_sink<Mutex>
20{
21public:
22    size_t msg_counter()
23    {
24        std::lock_guard<Mutex> lock(base_sink<Mutex>::mutex_);
25        return msg_counter_;
26    }
27
28    size_t flush_counter()
29    {
30        std::lock_guard<Mutex> lock(base_sink<Mutex>::mutex_);
31        return flush_counter_;
32    }
33
34    void set_delay(std::chrono::milliseconds delay)
35    {
36        delay_ = delay;
37    }
38
39protected:
40    void sink_it_(const details::log_msg &) override
41    {
42        msg_counter_++;
43        std::this_thread::sleep_for(delay_);
44    }
45
46    void flush_() override
47    {
48        flush_counter_++;
49    }
50    size_t msg_counter_{0};
51    size_t flush_counter_{0};
52    std::chrono::milliseconds delay_{std::chrono::milliseconds::zero()};
53};
54
55using test_sink_mt = test_sink<std::mutex>;
56using test_sink_st = test_sink<details::null_mutex>;
57
58} // namespace sinks
59} // namespace spdlog
60