2014-01-25 17:09:04 +08:00
|
|
|
#pragma once
|
2014-01-25 21:52:10 +08:00
|
|
|
|
2014-01-25 17:09:04 +08:00
|
|
|
#include<string>
|
|
|
|
#include<memory>
|
|
|
|
#include<mutex>
|
|
|
|
|
|
|
|
#include "../formatters/formatters.h"
|
|
|
|
#include "../level.h"
|
|
|
|
|
|
|
|
namespace c11log {
|
|
|
|
namespace sinks {
|
|
|
|
class base_sink {
|
|
|
|
public:
|
2014-01-25 21:52:10 +08:00
|
|
|
base_sink() = default;
|
2014-01-25 17:09:04 +08:00
|
|
|
base_sink(level::level_enum l):_level(l)
|
|
|
|
{};
|
2014-01-25 21:52:10 +08:00
|
|
|
virtual ~base_sink() = default;
|
|
|
|
|
2014-01-25 17:09:04 +08:00
|
|
|
base_sink(const base_sink&) = delete;
|
|
|
|
base_sink& operator=(const base_sink&) = delete;
|
|
|
|
|
|
|
|
void log(const std::string &msg, level::level_enum level)
|
|
|
|
{
|
|
|
|
if (level >= _level) {
|
2014-01-25 21:52:10 +08:00
|
|
|
std::lock_guard<std::mutex> lock(mutex_);
|
2014-01-25 17:09:04 +08:00
|
|
|
if (level >= _level)
|
2014-01-25 21:52:10 +08:00
|
|
|
sink_it_(msg);
|
2014-01-25 17:09:04 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
void set_level(level::level_enum level)
|
|
|
|
{
|
2014-01-25 21:52:10 +08:00
|
|
|
std::lock_guard<std::mutex> lock(mutex_);
|
2014-01-25 17:09:04 +08:00
|
|
|
_level = level;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2014-01-25 21:52:10 +08:00
|
|
|
virtual void sink_it_(const std::string& msg) = 0;
|
2014-01-25 17:09:04 +08:00
|
|
|
level::level_enum _level = level::INFO;
|
2014-01-25 21:52:10 +08:00
|
|
|
std::mutex mutex_;
|
2014-01-25 17:09:04 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
class null_sink:public base_sink {
|
|
|
|
protected:
|
2014-01-25 21:52:10 +08:00
|
|
|
void sink_it_(const std::string& msg) override
|
2014-01-25 17:09:04 +08:00
|
|
|
{}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|