2014-02-21 15:51:54 -05:00
|
|
|
// example.cpp : Simple logger example
|
2014-01-25 08:52:10 -05:00
|
|
|
//
|
2014-02-21 15:51:54 -05:00
|
|
|
#include <string>
|
2014-01-28 21:00:05 -05:00
|
|
|
#include <functional>
|
2014-03-01 07:06:58 -05:00
|
|
|
#include <iomanip>
|
2014-01-26 14:23:26 -05:00
|
|
|
#include "c11log/logger.h"
|
|
|
|
#include "c11log/sinks/async_sink.h"
|
|
|
|
#include "c11log/sinks/file_sinks.h"
|
|
|
|
#include "c11log/sinks/stdout_sinks.h"
|
|
|
|
|
|
|
|
#include "utils.h"
|
2014-01-25 08:52:10 -05:00
|
|
|
|
2014-02-21 15:51:54 -05:00
|
|
|
std::atomic<uint64_t> log_count;
|
2014-01-28 21:00:05 -05:00
|
|
|
std::atomic<bool> active;
|
|
|
|
|
|
|
|
using std::chrono::seconds;
|
|
|
|
|
2014-02-21 15:51:54 -05:00
|
|
|
void logging_thread()
|
2014-01-28 21:00:05 -05:00
|
|
|
{
|
2014-02-21 15:51:54 -05:00
|
|
|
auto &logger = c11log::get_logger("async");
|
|
|
|
while(active) {
|
|
|
|
logger.info()<<"Hello logger!";
|
|
|
|
++log_count;
|
|
|
|
}
|
2014-01-28 21:00:05 -05:00
|
|
|
}
|
|
|
|
|
2014-02-05 18:21:40 -05:00
|
|
|
|
2014-02-21 15:51:54 -05:00
|
|
|
void testlog(int threads)
|
2014-01-28 21:00:05 -05:00
|
|
|
{
|
|
|
|
|
2014-02-21 15:51:54 -05:00
|
|
|
active = true;
|
|
|
|
|
|
|
|
for(int i = 0; i < threads; i++)
|
|
|
|
new std::thread(std::bind(logging_thread));
|
|
|
|
|
|
|
|
while(active) {
|
|
|
|
using std::endl;
|
|
|
|
using std::cout;
|
|
|
|
using utils::format;
|
|
|
|
|
|
|
|
log_count = 0;
|
|
|
|
std::this_thread::sleep_for(seconds(1));
|
|
|
|
cout << "Logs/sec =\t" << format(log_count.load()) << endl;
|
|
|
|
}
|
2014-01-28 21:00:05 -05:00
|
|
|
}
|
2014-01-31 18:47:37 -05:00
|
|
|
|
|
|
|
|
2014-01-25 08:52:10 -05:00
|
|
|
int main(int argc, char* argv[])
|
|
|
|
{
|
2014-03-01 07:06:58 -05:00
|
|
|
|
2014-03-03 18:23:38 -05:00
|
|
|
if(argc || argv) {};
|
2014-02-21 15:51:54 -05:00
|
|
|
using namespace std::chrono;
|
2014-03-01 07:06:58 -05:00
|
|
|
using namespace c11log;
|
|
|
|
using namespace utils;
|
|
|
|
using std::cout;
|
|
|
|
using std::endl;
|
|
|
|
|
2014-03-03 17:08:30 -05:00
|
|
|
auto fsink = std::make_shared<sinks::rotating_file_sink>("log", "txt", 1024*1024*50 , 5, seconds(1));
|
2014-03-02 09:36:06 -05:00
|
|
|
auto &my_logger = get_logger("example");
|
2014-03-03 17:08:30 -05:00
|
|
|
auto null_sink = std::make_shared<sinks::null_sink>();
|
|
|
|
//auto async = std::make_shared<sinks::async_sink>(1000);
|
|
|
|
//async->add_sink(fsink);
|
2014-03-03 18:23:38 -05:00
|
|
|
my_logger.add_sink(null_sink);
|
2014-03-01 07:06:58 -05:00
|
|
|
auto start = system_clock::now();
|
2014-03-02 09:36:06 -05:00
|
|
|
|
2014-03-01 07:06:58 -05:00
|
|
|
const unsigned int howmany = 10000000;
|
|
|
|
for(unsigned int i = 0; i < howmany ; i++)
|
2014-03-02 09:36:06 -05:00
|
|
|
my_logger.info() << "Hello logger " << i;
|
2014-03-03 18:23:38 -05:00
|
|
|
|
|
|
|
//async->shutdown(seconds(3));
|
2014-03-01 07:06:58 -05:00
|
|
|
auto delta = system_clock::now() - start;
|
|
|
|
auto delta_d = duration_cast<duration<double>> (delta);
|
|
|
|
cout << "Total " << format(howmany) << endl;
|
|
|
|
cout << "Delta " << format(delta_d.count()) << endl;
|
|
|
|
cout << "Rate: " << format(howmany/delta_d.count()) << "/sec" << endl;
|
|
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
2014-03-03 18:23:38 -05:00
|
|
|
/*
|
2014-02-21 15:51:54 -05:00
|
|
|
if(argc !=3) {
|
|
|
|
std::cerr << "Usage: " << argv[0] << " qsize, threads" << std::endl;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
int qsize = atoi(argv[1]);
|
|
|
|
int threads = atoi(argv[2]);
|
2014-02-08 18:25:23 -05:00
|
|
|
|
2014-03-01 07:06:58 -05:00
|
|
|
|
2014-02-21 18:16:59 -05:00
|
|
|
auto null_sink = std::make_shared<sinks::null_sink>();
|
|
|
|
auto stdout_sink = std::make_shared<sinks::stdout_sink>();
|
|
|
|
auto async = std::make_shared<sinks::async_sink>(qsize);
|
2014-03-01 07:06:58 -05:00
|
|
|
auto fsink = std::make_shared<sinks::rotating_file_sink>("log", "txt", 1024*1024*50 , 5, std::chrono::milliseconds(1000));
|
2014-02-08 18:25:23 -05:00
|
|
|
|
2014-02-21 15:51:54 -05:00
|
|
|
async->add_sink(fsink);
|
2014-02-08 18:25:23 -05:00
|
|
|
|
2014-03-01 07:06:58 -05:00
|
|
|
//auto &logger = c11log::get_logger("async");
|
|
|
|
//logger.add_sink(fsink);
|
|
|
|
|
|
|
|
|
2014-02-08 18:25:23 -05:00
|
|
|
|
2014-02-21 15:51:54 -05:00
|
|
|
testlog(threads);
|
2014-03-03 17:08:30 -05:00
|
|
|
*/
|
2014-02-08 18:25:23 -05:00
|
|
|
}
|
2014-01-25 08:52:10 -05:00
|
|
|
|