You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
spdlog/example/example.cpp

73 lines
1.5 KiB
C++

12 years ago
// example.cpp : Simple logger example
//
12 years ago
#include <string>
12 years ago
#include <functional>
12 years ago
#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"
12 years ago
std::atomic<uint64_t> log_count;
12 years ago
std::atomic<bool> active;
using std::chrono::seconds;
12 years ago
void logging_thread()
12 years ago
{
12 years ago
auto &logger = c11log::get_logger("async");
while(active) {
logger.info()<<"Hello logger!";
++log_count;
}
12 years ago
}
12 years ago
12 years ago
void testlog(int threads)
12 years ago
{
12 years ago
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;
}
12 years ago
}
int main(int argc, char* argv[])
{
12 years ago
using namespace std::chrono;
12 years ago
if(argc !=3) {
std::cerr << "Usage: " << argv[0] << " qsize, threads" << std::endl;
return 0;
}
int qsize = atoi(argv[1]);
int threads = atoi(argv[2]);
using namespace c11log;
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);
auto fsink = std::make_shared<sinks::rotating_file_sink>("example_log", "txt", 1024*1024*50 , 5);
12 years ago
async->add_sink(fsink);
12 years ago
auto &logger = c11log::get_logger("async");
logger.add_sink(async);
12 years ago
testlog(threads);
}