async_sink

This commit is contained in:
gabi
2014-01-25 17:28:56 +02:00
parent df56bb775a
commit 9727692a83
6 changed files with 85 additions and 48 deletions

View File

@@ -4,21 +4,32 @@
#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);
auto screen_sink = std::make_shared<c11log::sinks::stdout_sink>();
auto file_sink = std::make_shared<c11log::sinks::midnight_file_sink>("logtest");
auto async = std::make_shared<c11log::sinks::async_sink>(1000);
async->add_sink(file_sink);
logger.add_sink(async);
logger.info() << "Hello logger!";
utils::run(std::chrono::seconds(10), fn);
//logger.add_sink(file_sink);
auto fn = [&logger]()
{
logger.info() << "Hello logger!";
};
utils::bench("test log", std::chrono::seconds(3), fn);
logger.info() << "bye";
utils::bench("shutdown", [&async]() {
async->shutdown(std::chrono::seconds(10));
});
return 0;
}
static void fn()
{}

View File

@@ -20,7 +20,7 @@ std::string format(const T& value)
return ss.str();
}
inline void run(const std::chrono::milliseconds &duration, const std::function<void() >& fn)
inline void bench(const std::string& fn_name, const std::chrono::milliseconds &duration, const std::function<void() >& fn)
{
using namespace std::chrono;
typedef steady_clock the_clock;
@@ -38,10 +38,21 @@ inline void run(const std::chrono::milliseconds &duration, const std::function<v
auto p = now - lastPrintTime;
if (now - lastPrintTime >= print_interval)
{
std::cout << format(counter) << " per sec" << std::endl;
std::cout << fn_name << ": " << format(counter) << " per sec" << std::endl;
counter = 0;
lastPrintTime = the_clock::now();
}
}
}
inline void bench(const std::string& fn_name, const std::function<void() >& fn)
{
using namespace std::chrono;
auto start = steady_clock::now();
fn();
auto delta = steady_clock::now() - start;
std::cout << fn_name << ": " << duration_cast<milliseconds>(delta).count() << " ms" << std::endl;
}
}