sinks now parameterized by mutex T

This commit is contained in:
gabi 2014-10-10 02:44:12 +03:00
parent f0622c8fb8
commit 38468e64d8
3 changed files with 51 additions and 47 deletions

View File

@ -1,47 +0,0 @@
#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;
protected:
virtual void _sink_it(const details::log_msg& msg) override
{
std::lock_guard<std::mutex> lock(_mutex);
_ostream << msg.formatted;
}
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;
}
}
}

View File

@ -0,0 +1,18 @@
#pragma once
#include "../details/log_msg.h"
namespace c11log
{
namespace sinks
{
class isink
{
public:
virtual void log(const details::log_msg& msg) = 0;
virtual void enable(bool enabled) = 0;
virtual bool is_enabled() = 0;
};
}
}

View File

@ -0,0 +1,33 @@
#pragma once
#include <iostream>
#include <mutex>
#include "ostream_sink.h"
#include "../details/null_mutex.h"
namespace c11log
{
namespace sinks
{
template <class Mutex>
class stdout_sink : public ostream_sink<Mutex>
{
public:
stdout_sink() : ostream_sink<Mutex>(std::cout) {}
};
typedef stdout_sink<std::mutex> stdout_sink_mt;
typedef stdout_sink<details::null_mutex> stdout_sink_st;
template <class Mutex>
class stderr_sink : public ostream_sink<Mutex>
{
public:
stderr_sink() : ostream_sink<Mutex>(std::cerr) {}
};
typedef stderr_sink<std::mutex> stderr_sink_mt;
typedef stderr_sink<details::null_mutex> stderr_sink_st;
}
}