test_sink.h 1.85 KB
Newer Older
gabime's avatar
gabime committed
1 2 3 4 5 6 7 8 9
//
// 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"
10
#include "spdlog/fmt/fmt.h"
gabime's avatar
gabime committed
11
#include <chrono>
gabime's avatar
gabime committed
12
#include <mutex>
gabime's avatar
gabime committed
13
#include <thread>
gabime's avatar
gabime committed
14 15 16 17 18 19 20

namespace spdlog {
namespace sinks {

template<class Mutex>
class test_sink : public base_sink<Mutex>
{
gabime's avatar
gabime committed
21 22
    const size_t lines_to_save = 100;

gabime's avatar
gabime committed
23 24 25
public:
    size_t msg_counter()
    {
26
        std::lock_guard<Mutex> lock(base_sink<Mutex>::mutex_);
gabime's avatar
gabime committed
27 28 29
        return msg_counter_;
    }

gabime's avatar
gabime committed
30
    size_t flush_counter()
gabime's avatar
gabime committed
31
    {
32
        std::lock_guard<Mutex> lock(base_sink<Mutex>::mutex_);
gabime's avatar
gabime committed
33 34 35 36 37
        return flush_counter_;
    }

    void set_delay(std::chrono::milliseconds delay)
    {
gabime's avatar
gabime committed
38
        std::lock_guard<Mutex> lock(base_sink<Mutex>::mutex_);
gabime's avatar
gabime committed
39
        delay_ = delay;
gabime's avatar
gabime committed
40 41
    }

gabime's avatar
gabime committed
42 43 44 45 46 47
    // return last output without the eol
    std::vector<std::string> lines()
    {
        std::lock_guard<Mutex> lock(base_sink<Mutex>::mutex_);
        return lines_;
    }
48

gabime's avatar
gabime committed
49
protected:
50
    void sink_it_(const details::log_msg &msg) override
gabime's avatar
gabime committed
51
    {
52
        memory_buf_t formatted;
gabime's avatar
gabime committed
53 54 55 56 57 58 59 60 61
        base_sink<Mutex>::formatter_->format(msg, formatted);
        // save the line without the eol
        auto eol_len = strlen(details::os::default_eol);
        if (lines_.size() < lines_to_save)
        {
            lines_.emplace_back(formatted.begin(), formatted.end() - eol_len);
        }
        msg_counter_++;
        std::this_thread::sleep_for(delay_);
gabime's avatar
gabime committed
62 63
    }

64
    void flush_() override
gabime's avatar
gabime committed
65
    {
gabime's avatar
gabime committed
66
        flush_counter_++;
gabime's avatar
gabime committed
67
    }
gabime's avatar
gabime committed
68

gabime's avatar
gabime committed
69
    size_t msg_counter_{0};
gabime's avatar
gabime committed
70
    size_t flush_counter_{0};
gabime's avatar
gabime committed
71
    std::chrono::milliseconds delay_{std::chrono::milliseconds::zero()};
gabime's avatar
gabime committed
72
    std::vector<std::string> lines_;
gabime's avatar
gabime committed
73 74 75 76 77 78 79
};

using test_sink_mt = test_sink<std::mutex>;
using test_sink_st = test_sink<details::null_mutex>;

} // namespace sinks
} // namespace spdlog