2014-03-22 20:11:17 +08:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
#include <mutex>
|
|
|
|
#include <memory>
|
|
|
|
|
|
|
|
#include "base_sink.h"
|
|
|
|
|
|
|
|
namespace c11log
|
|
|
|
{
|
|
|
|
namespace sinks
|
|
|
|
{
|
|
|
|
class console_sink: public base_sink
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
explicit console_sink(std::ostream& os):_ostream(os) {}
|
|
|
|
console_sink(const console_sink&) = delete;
|
|
|
|
console_sink& operator=(const console_sink&) = delete;
|
|
|
|
virtual ~console_sink() = default;
|
|
|
|
|
2014-03-29 05:46:45 +08:00
|
|
|
|
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) override
|
2014-03-22 20:11:17 +08:00
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock(_mutex);
|
2014-05-09 20:27:06 +08:00
|
|
|
_ostream << msg.formatted;
|
2014-03-22 20:11:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
std::ostream& _ostream;
|
|
|
|
std::mutex _mutex;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
inline std::shared_ptr<console_sink>& stdout_sink ()
|
|
|
|
{
|
|
|
|
static auto inst = std::make_shared<console_sink>(std::cout);
|
|
|
|
return inst;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline std::shared_ptr<console_sink>& stderr_sink ()
|
|
|
|
{
|
|
|
|
static auto inst = std::make_shared<console_sink>(std::cerr);
|
|
|
|
return inst;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|