spdlog/include/c11log/sinks/base_sink.h

53 lines
909 B
C
Raw Normal View History

2014-03-22 08:11:17 -04:00
#pragma once
#include<string>
#include<mutex>
2014-03-22 08:11:17 -04:00
#include<atomic>
#include "isink.h"
2014-03-22 08:11:17 -04:00
#include "../formatter.h"
2014-10-09 20:36:50 -04:00
#include "../common.h"
2014-03-28 12:03:24 -04:00
#include "../details/log_msg.h"
2014-03-22 08:11:17 -04:00
2014-03-22 08:11:17 -04:00
namespace c11log
{
namespace sinks
{
template<class Mutex>
class base_sink:public isink
2014-03-22 08:11:17 -04:00
{
public:
base_sink():_mutex(), _enabled(true) {}
2014-03-22 08:11:17 -04:00
virtual ~base_sink() = default;
base_sink(const base_sink&) = delete;
base_sink& operator=(const base_sink&) = delete;
2014-10-09 20:36:50 -04:00
void log(const details::log_msg& msg) override
2014-03-22 08:11:17 -04:00
{
2014-03-28 09:05:09 -04:00
if (_enabled)
2014-03-22 08:11:17 -04:00
{
std::lock_guard<Mutex> lock(_mutex);
2014-03-22 08:11:17 -04:00
_sink_it(msg);
}
};
2014-10-09 20:36:50 -04:00
void enable(bool enabled) override
2014-03-28 09:05:09 -04:00
{
_enabled = enabled;
}
2014-10-09 20:36:50 -04:00
bool is_enabled() override
2014-03-22 08:11:17 -04:00
{
2014-03-28 09:05:09 -04:00
return _enabled.load();
2014-03-22 08:11:17 -04:00
}
protected:
2014-03-28 12:03:24 -04:00
virtual void _sink_it(const details::log_msg& msg) = 0;
Mutex _mutex;
2014-03-28 09:05:09 -04:00
std::atomic<bool> _enabled;
2014-03-22 08:11:17 -04:00
};
}
}