spdlog/include/c11log/sinks/base_sink.h

62 lines
975 B
C
Raw Normal View History

2014-03-22 20:11:17 +08:00
#pragma once
#include<string>
#include<atomic>
#include "../formatter.h"
#include "../common_types.h"
2014-03-29 00:03:24 +08:00
#include "../details/log_msg.h"
2014-03-22 20:11:17 +08:00
namespace c11log
{
namespace sinks
{
class base_sink
{
public:
2014-03-28 21:05:09 +08:00
base_sink(): _enabled(true) {}
2014-03-22 20:11:17 +08:00
virtual ~base_sink() = default;
base_sink(const base_sink&) = delete;
base_sink& operator=(const base_sink&) = delete;
2014-03-29 00:03:24 +08:00
void log(const details::log_msg& msg)
2014-03-22 20:11:17 +08:00
{
2014-03-28 21:05:09 +08:00
if (_enabled)
2014-03-22 20:11:17 +08:00
{
_sink_it(msg);
}
};
2014-03-28 21:05:09 +08:00
void enable(bool enabled)
{
_enabled = enabled;
}
bool is_enabled()
2014-03-22 20:11:17 +08:00
{
2014-03-28 21:05:09 +08:00
return _enabled.load();
2014-03-22 20:11:17 +08:00
}
protected:
2014-03-29 00:03:24 +08:00
virtual void _sink_it(const details::log_msg& msg) = 0;
2014-03-28 21:05:09 +08:00
std::atomic<bool> _enabled;
2014-03-22 20:11:17 +08:00
};
class null_sink:public base_sink
{
2014-03-29 05:46:45 +08:00
public:
static std::shared_ptr<null_sink>& get()
{
static auto inst = std::make_shared<null_sink>();
return inst;
}
2014-03-22 20:11:17 +08:00
protected:
2014-03-29 00:03:24 +08:00
void _sink_it(const details::log_msg&) override
2014-03-29 05:46:45 +08:00
{}
2014-03-22 20:11:17 +08:00
};
2014-03-29 05:46:45 +08:00
2014-03-22 20:11:17 +08:00
}
}