spdlog/src/test.cpp

64 lines
1.8 KiB
C++
Raw Normal View History

2014-01-25 08:52:10 -05:00
// test.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
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-01-25 10:28:56 -05:00
2014-01-25 08:52:10 -05:00
int main(int argc, char* argv[])
{
2014-01-26 14:23:26 -05:00
int nthreads = argc > 1 ? atoi(argv[1]) : 1;
2014-01-25 18:53:23 -05:00
auto null_sink = std::make_shared<c11log::sinks::null_sink>();
2014-01-26 14:23:26 -05:00
auto stdout_sink = std::make_shared<c11log::sinks::stdout_sink>();
2014-01-27 06:20:00 -05:00
auto async = std::make_shared<c11log::sinks::async_sink>(1000);
2014-01-27 06:07:57 -05:00
//auto fsink = std::make_shared<c11log::sinks::rotating_file_sink>("newlog", "txt", 1024*1024*10 , 2);
2014-01-27 06:20:00 -05:00
auto fsink = std::make_shared<c11log::sinks::daily_file_sink>("daily", "txt");
2014-01-25 10:28:56 -05:00
2014-01-27 06:07:57 -05:00
async->add_sink(fsink);
2014-01-25 10:28:56 -05:00
2014-01-25 18:53:23 -05:00
c11log::logger logger("test");
logger.add_sink(async);
2014-01-26 14:23:26 -05:00
2014-01-25 18:53:23 -05:00
std::atomic<uint32_t> counter { 0 };
2014-01-27 06:57:52 -05:00
auto counter_ptr = &counter;
std::atomic<bool> active{true};
auto active_ptr = &active;
std::vector<std::thread*> threads;
std::cout << "Starting " << nthreads << " threads for 3 seconds.." << std::endl;
2014-01-26 14:23:26 -05:00
for (int i = 0; i < nthreads; i++)
2014-01-25 18:53:23 -05:00
{
2014-01-27 06:57:52 -05:00
auto t = new std::thread([&logger, counter_ptr, active_ptr]() {
while (*active_ptr)
2014-01-25 18:53:23 -05:00
{
2014-01-26 14:23:26 -05:00
logger.info() << "Hello from thread " << std::this_thread::get_id() << "\tcounter: " << counter_ptr->load();
2014-01-25 18:53:23 -05:00
(*counter_ptr)++;
2014-01-27 06:57:52 -05:00
}
2014-01-25 18:53:23 -05:00
});
2014-01-27 06:57:52 -05:00
threads.push_back(t);
2014-01-25 18:53:23 -05:00
}
2014-01-27 06:57:52 -05:00
2014-01-27 06:20:00 -05:00
int seconds = 0;
2014-01-27 06:57:52 -05:00
while (seconds++ < 3)
2014-01-25 18:53:23 -05:00
{
counter = 0;
std::this_thread::sleep_for(std::chrono::seconds(1));
std::cout << "Counter = " << utils::format(counter.load()) << std::endl;
}
2014-01-27 06:57:52 -05:00
active = false;
for(auto t:threads)
t->join();
async->shutdown(std::chrono::seconds(1));
2014-01-25 08:52:10 -05:00
return 0;
}