spdlog/include/c11log/sinks/stdout_sinks.h

41 lines
991 B
C
Raw Normal View History

2014-01-25 21:52:10 +08:00
#pragma once
2014-01-25 17:09:04 +08:00
#include <iostream>
2014-03-05 06:31:13 +08:00
#include <mutex>
#include <memory>
2014-01-25 17:09:04 +08:00
#include "base_sink.h"
2014-02-22 04:51:54 +08:00
namespace c11log {
namespace sinks {
class ostream_sink: public base_sink {
2014-01-25 21:52:10 +08:00
public:
2014-03-05 06:31:13 +08:00
explicit ostream_sink(std::ostream& os):_ostream(os) {}
ostream_sink(const ostream_sink&) = delete;
ostream_sink& operator=(const ostream_sink&) = delete;
2014-01-25 21:52:10 +08:00
virtual ~ostream_sink() = default;
2014-01-25 17:09:04 +08:00
2014-01-25 21:52:10 +08:00
protected:
2014-02-22 16:34:42 +08:00
virtual void _sink_it(const std::string& msg) override {
2014-03-05 06:31:13 +08:00
std::lock_guard<std::mutex> lock(_mutex);
2014-02-22 04:51:54 +08:00
_ostream << msg;
}
2014-01-25 17:09:04 +08:00
2014-02-22 04:51:54 +08:00
std::ostream& _ostream;
2014-03-05 06:31:13 +08:00
std::mutex _mutex;
2014-01-25 21:52:10 +08:00
};
2014-01-25 17:09:04 +08:00
2014-03-05 06:31:13 +08:00
inline std::shared_ptr<ostream_sink> cout_sink() {
static const ostream_sink& instance{std::cout};
return std::shared_ptr<ostream_sink>(&instance, [=](ostream_sink*) {});
}
2014-01-25 17:09:04 +08:00
2014-03-05 06:31:13 +08:00
inline std::shared_ptr<ostream_sink> cerr_sink() {
static const ostream_sink& instance = ostream_sink(std::cerr);
return std::shared_ptr<ostream_sink>(&instance, [=](ostream_sink*) {});
}
2014-02-22 04:51:54 +08:00
2014-03-05 06:31:13 +08:00
}
2014-01-25 21:52:10 +08:00
}