spdlog/example/example.cpp

73 lines
1.5 KiB
C++
Raw Normal View History

2014-02-22 04:51:54 +08:00
// example.cpp : Simple logger example
2014-01-25 21:52:10 +08:00
//
2014-02-22 04:51:54 +08:00
#include <string>
2014-01-29 10:00:05 +08:00
#include <functional>
2014-01-27 03:23:26 +08: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 21:52:10 +08:00
2014-02-22 04:51:54 +08:00
std::atomic<uint64_t> log_count;
2014-01-29 10:00:05 +08:00
std::atomic<bool> active;
using std::chrono::seconds;
2014-02-22 04:51:54 +08:00
void logging_thread()
2014-01-29 10:00:05 +08:00
{
2014-02-22 04:51:54 +08:00
auto &logger = c11log::get_logger("async");
while(active) {
logger.info()<<"Hello logger!";
++log_count;
}
2014-01-29 10:00:05 +08:00
}
2014-02-06 07:21:40 +08:00
2014-02-22 04:51:54 +08:00
void testlog(int threads)
2014-01-29 10:00:05 +08:00
{
2014-02-22 04:51:54 +08: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-29 10:00:05 +08:00
}
2014-02-01 07:47:37 +08:00
2014-01-25 21:52:10 +08:00
int main(int argc, char* argv[])
{
2014-02-22 04:51:54 +08:00
using namespace std::chrono;
2014-02-01 07:47:37 +08:00
2014-02-22 04:51:54 +08: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-22 04:51:54 +08:00
using namespace c11log::sinks;
auto null_sink = std::make_shared<null_sink>();
auto stdout_sink = std::make_shared<stdout_sink>();
auto async = std::make_shared<async_sink>(qsize);
auto fsink = std::make_shared<rotating_file_sink>("example_log", "txt", 1024*1024*50 , 5);
2014-02-22 04:51:54 +08:00
async->add_sink(fsink);
2014-02-22 04:51:54 +08:00
auto &logger = c11log::get_logger("async");
logger.add_sink(async);
2014-02-22 04:51:54 +08:00
testlog(threads);
}
2014-01-25 21:52:10 +08:00