spdlog/tests/test_sink.h

58 lines
1.0 KiB
C
Raw Normal View History

2018-05-22 14:59:27 -04:00
//
// Copyright(c) 2018 Gabi Melman.
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
//
#pragma once
#include "spdlog/details/null_mutex.h"
#include "spdlog/sinks/base_sink.h"
2018-05-26 19:53:16 -04:00
#include <chrono>
2018-05-22 14:59:27 -04:00
#include <mutex>
2018-05-26 19:53:16 -04:00
#include <thread>
2018-05-22 14:59:27 -04:00
namespace spdlog {
namespace sinks {
template<class Mutex>
class test_sink : public base_sink<Mutex>
{
public:
size_t msg_counter()
{
return msg_counter_;
}
2018-05-26 19:53:16 -04:00
size_t flush_counter()
2018-05-22 14:59:27 -04:00
{
2018-05-26 19:53:16 -04:00
return flush_counter_;
}
void set_delay(std::chrono::milliseconds delay)
{
delay_ = delay;
2018-05-22 14:59:27 -04:00
}
protected:
void sink_it_(const details::log_msg &) override
2018-05-22 14:59:27 -04:00
{
msg_counter_++;
2018-05-26 19:53:16 -04:00
std::this_thread::sleep_for(delay_);
2018-05-22 14:59:27 -04:00
}
void flush_() override
2018-05-22 14:59:27 -04:00
{
2018-05-26 19:53:16 -04:00
flush_counter_++;
2018-05-22 14:59:27 -04:00
}
size_t msg_counter_{0};
2018-05-26 19:53:16 -04:00
size_t flush_counter_{0};
std::chrono::milliseconds delay_{0};
2018-05-22 14:59:27 -04:00
};
using test_sink_mt = test_sink<std::mutex>;
using test_sink_st = test_sink<details::null_mutex>;
} // namespace sinks
} // namespace spdlog