mirror of https://github.com/gabime/spdlog.git
parent
3e88d785c0
commit
df56bb775a
Binary file not shown.
@ -1,55 +1,98 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include <mutex>
|
|
||||||
#include <queue>
|
|
||||||
#include <thread>
|
#include <thread>
|
||||||
#include <iostream>
|
#include <chrono>
|
||||||
|
#include <mutex>
|
||||||
|
#include <atomic>
|
||||||
|
|
||||||
#include "../logger.h"
|
|
||||||
#include "base_sink.h"
|
#include "base_sink.h"
|
||||||
|
#include "../logger.h"
|
||||||
|
#include "../details/blocking_queue.h"
|
||||||
|
|
||||||
namespace c11log {
|
namespace c11log {
|
||||||
namespace sinks {
|
namespace sinks {
|
||||||
class async_sink : base_sink {
|
class async_sink : public base_sink {
|
||||||
enum class fullq_policy {
|
|
||||||
BLOCK=0,
|
|
||||||
DROP_MSG
|
|
||||||
};
|
|
||||||
public:
|
public:
|
||||||
async_sink(std::size_t max_queue_size, fullq_policy q_policy) :_fullq_policy(q_policy), _back_thread(&_thread_loop)
|
using size_type = c11log::details::blocking_queue<std::string>::size_type;
|
||||||
{
|
explicit async_sink(const std::size_t max_queue_size, const std::chrono::seconds& timeout = std::chrono::seconds::max());
|
||||||
|
~async_sink();
|
||||||
|
void add_sink(logger::sink_ptr_t sink);
|
||||||
|
void remove_sink(logger::sink_ptr_t sink_ptr);
|
||||||
|
|
||||||
|
|
||||||
}
|
protected:
|
||||||
protected:
|
void sink_it_(const std::string& msg) override;
|
||||||
void _sink_it(const std::string& msg) override
|
void thread_loop_();
|
||||||
{
|
|
||||||
_msgs_mutex.unlock();
|
private:
|
||||||
_msgs.push(msg);
|
c11log::logger::sinks_vector_t sinks_;
|
||||||
}
|
bool active_ = true;
|
||||||
void _thread_loop()
|
const std::chrono::seconds timeout_;
|
||||||
{
|
c11log::details::blocking_queue<std::string> q_;
|
||||||
while (_active) {
|
std::thread back_thread_;
|
||||||
_msgs_mutex.lock();
|
void shutdown_();
|
||||||
std::string &msg = _msgs.front();
|
};
|
||||||
_msgs.pop();
|
}
|
||||||
_msgs_mutex.unlock();
|
}
|
||||||
std::cout << "Popped: " << msg << std::endl;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
//
|
||||||
c11log::logger::sinks_vector_t _sinks;
|
// async_sink inline impl
|
||||||
fullq_policy _fullq_policy;
|
//
|
||||||
std::queue<std::string> _msgs;
|
|
||||||
std::thread _back_thread;
|
|
||||||
bool _active = true;
|
|
||||||
std::mutex _msgs_mutex;
|
|
||||||
|
|
||||||
|
inline c11log::sinks::async_sink::async_sink(const std::size_t max_queue_size, const std::chrono::seconds& timeout)
|
||||||
|
:q_(max_queue_size),
|
||||||
|
timeout_(timeout),
|
||||||
|
back_thread_(&async_sink::thread_loop_, this)
|
||||||
|
{}
|
||||||
|
|
||||||
|
inline c11log::sinks::async_sink::~async_sink()
|
||||||
|
{
|
||||||
|
shutdown_();
|
||||||
|
}
|
||||||
|
inline void c11log::sinks::async_sink::sink_it_(const std::string& msg)
|
||||||
|
{
|
||||||
|
q_.push(msg, timeout_);
|
||||||
|
}
|
||||||
|
|
||||||
};
|
inline void c11log::sinks::async_sink::thread_loop_()
|
||||||
|
{
|
||||||
|
std::string msg;
|
||||||
|
while (active_)
|
||||||
|
{
|
||||||
|
if (q_.pop(msg, timeout_))
|
||||||
|
{
|
||||||
|
std::lock_guard<std::mutex> lock(mutex_);
|
||||||
|
for (auto &sink : sinks_)
|
||||||
|
{
|
||||||
|
if (active_)
|
||||||
|
sink->log(msg, _level);
|
||||||
|
else
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
inline void c11log::sinks::async_sink::add_sink(logger::sink_ptr_t sink)
|
||||||
|
{
|
||||||
|
std::lock_guard<std::mutex> lock(mutex_);
|
||||||
|
sinks_.push_back(sink);
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void c11log::sinks::async_sink::remove_sink(logger::sink_ptr_t sink_ptr)
|
||||||
|
{
|
||||||
|
std::lock_guard<std::mutex> lock(mutex_);
|
||||||
|
sinks_.erase(std::remove(sinks_.begin(), sinks_.end(), sink_ptr), sinks_.end());
|
||||||
}
|
}
|
||||||
|
|
||||||
void c11log::sinks::async_sink::_sink_it(const std::string& msg)
|
inline void c11log::sinks::async_sink::shutdown_()
|
||||||
{
|
{
|
||||||
|
{
|
||||||
|
std::lock_guard<std::mutex> lock(mutex_);
|
||||||
|
active_ = false;
|
||||||
|
}
|
||||||
|
q_.clear();
|
||||||
|
back_thread_.join();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
|
@ -1,41 +1,38 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include "base_sink.h"
|
#include "base_sink.h"
|
||||||
|
|
||||||
namespace c11log
|
namespace c11log
|
||||||
{
|
{
|
||||||
namespace sinks
|
namespace sinks
|
||||||
{
|
{
|
||||||
class ostream_sink:public base_sink
|
class ostream_sink: public base_sink
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
ostream_sink(std::ostream& os):_ostream(os)
|
ostream_sink(std::ostream& os):_ostream(os) {}
|
||||||
{}
|
virtual ~ostream_sink() = default;
|
||||||
|
|
||||||
virtual ~ostream_sink()
|
|
||||||
{}
|
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
virtual void _sink_it(const std::string& msg)
|
virtual void sink_it_(const std::string& msg) override
|
||||||
{
|
{
|
||||||
_ostream << msg;
|
_ostream << msg;
|
||||||
}
|
}
|
||||||
std::ostream& _ostream;
|
|
||||||
|
|
||||||
};
|
std::ostream& _ostream;
|
||||||
|
};
|
||||||
|
|
||||||
class stdout_sink:public ostream_sink
|
class stdout_sink:public ostream_sink
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
stdout_sink():ostream_sink(std::cout)
|
stdout_sink():ostream_sink(std::cout) {}
|
||||||
{}
|
};
|
||||||
};
|
|
||||||
|
|
||||||
class stderr_sink:public ostream_sink
|
class stderr_sink:public ostream_sink
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
stderr_sink():ostream_sink(std::cerr)
|
stderr_sink():ostream_sink(std::cerr) {}
|
||||||
{}
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -0,0 +1,8 @@
|
|||||||
|
// stdafx.cpp : source file that includes just the standard includes
|
||||||
|
// test.pch will be the pre-compiled header
|
||||||
|
// stdafx.obj will contain the pre-compiled type information
|
||||||
|
|
||||||
|
#include "stdafx.h"
|
||||||
|
|
||||||
|
// TODO: reference any additional headers you need in STDAFX.H
|
||||||
|
// and not in this file
|
@ -0,0 +1,16 @@
|
|||||||
|
// stdafx.h : include file for standard system include files,
|
||||||
|
// or project specific include files that are used frequently, but
|
||||||
|
// are changed infrequently
|
||||||
|
//
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <thread>
|
||||||
|
#include <iostream>
|
||||||
|
#include <chrono>
|
||||||
|
|
||||||
|
#include "utils.h"
|
||||||
|
#include "../include/c11log/logger.h"
|
||||||
|
#include "../include/c11log/sinks/async_sink.h"
|
||||||
|
#include "../include/c11log/sinks/stdout_sinks.h"
|
||||||
|
#include "../include/c11log/sinks/file_sinks.h"
|
@ -0,0 +1,24 @@
|
|||||||
|
// test.cpp : Defines the entry point for the console application.
|
||||||
|
//
|
||||||
|
|
||||||
|
#include "stdafx.h"
|
||||||
|
|
||||||
|
|
||||||
|
void fn();
|
||||||
|
int main(int argc, char* argv[])
|
||||||
|
{
|
||||||
|
|
||||||
|
c11log::logger logger("test");
|
||||||
|
|
||||||
|
auto sink = std::make_shared<c11log::sinks::stdout_sink>();
|
||||||
|
auto async = std::make_shared<c11log::sinks::async_sink>(100);
|
||||||
|
async->add_sink(sink);
|
||||||
|
logger.add_sink(async);
|
||||||
|
logger.info() << "Hello logger!";
|
||||||
|
utils::run(std::chrono::seconds(10), fn);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void fn()
|
||||||
|
{}
|
||||||
|
|
@ -0,0 +1,47 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <functional>
|
||||||
|
#include <chrono>
|
||||||
|
#include <iostream>
|
||||||
|
#include <sstream>
|
||||||
|
#include <iomanip>
|
||||||
|
#include <locale>
|
||||||
|
|
||||||
|
namespace utils
|
||||||
|
{
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
std::string format(const T& value)
|
||||||
|
{
|
||||||
|
static std::locale loc("");
|
||||||
|
std::stringstream ss;
|
||||||
|
ss.imbue(loc);
|
||||||
|
ss << value;
|
||||||
|
return ss.str();
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void run(const std::chrono::milliseconds &duration, const std::function<void() >& fn)
|
||||||
|
{
|
||||||
|
using namespace std::chrono;
|
||||||
|
typedef steady_clock the_clock;
|
||||||
|
size_t counter = 0;
|
||||||
|
seconds print_interval(1);
|
||||||
|
auto start_time = the_clock::now();
|
||||||
|
auto lastPrintTime = start_time;
|
||||||
|
while (true)
|
||||||
|
{
|
||||||
|
fn();
|
||||||
|
++counter;
|
||||||
|
auto now = the_clock::now();
|
||||||
|
if (now - start_time >= duration)
|
||||||
|
break;
|
||||||
|
auto p = now - lastPrintTime;
|
||||||
|
if (now - lastPrintTime >= print_interval)
|
||||||
|
{
|
||||||
|
std::cout << format(counter) << " per sec" << std::endl;
|
||||||
|
counter = 0;
|
||||||
|
lastPrintTime = the_clock::now();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue