spdlog/example/example.cpp

95 lines
3.0 KiB
C++
Raw Normal View History

2024-12-07 01:21:42 +08:00
//
2019-03-24 06:40:27 +08:00
// Copyright(c) 2015 Gabi Melman.
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
2019-05-12 22:05:14 +08:00
2019-03-24 06:40:27 +08:00
// spdlog usage example
2021-09-04 01:53:29 +08:00
#include <chrono>
2024-12-07 19:54:40 +08:00
#include <clocale>
2023-09-29 05:20:26 +08:00
#include <cstdio>
2019-12-13 22:17:09 +08:00
2024-12-07 19:54:40 +08:00
#include "/home/gabi//spdlog2/tests/test_sink.h"
#include "spdlog/sinks/async_sink.h"
#include "spdlog/sinks/stdout_color_sinks.h"
2023-09-29 05:20:26 +08:00
#include "spdlog/spdlog.h"
2024-12-07 19:54:40 +08:00
#include "spdlog/stopwatch.h"
2019-08-23 00:58:49 +08:00
2024-12-07 06:52:27 +08:00
using namespace spdlog::sinks;
void worker_thread(std::shared_ptr<spdlog::logger> logger, int n_messages) {
2024-12-07 19:54:40 +08:00
for (int i = 0; i < n_messages; ++i) {
// std::this_thread::sleep_for(std::chrono::milliseconds(10));
logger->info("Worker thread message #{}", i);
2020-09-26 20:30:45 +08:00
}
}
2024-12-07 19:54:40 +08:00
bool async_example2(size_t n_threads, size_t n_messages) {
auto test_sink = std::make_shared<test_sink_st>();
auto n_total_messages = n_messages * n_threads;
{
std::vector<std::thread> threads;
auto async_sink = std::make_shared<async_sink_mt>();
2024-12-07 19:54:40 +08:00
async_sink->add_sink(test_sink);
auto logger = std::make_shared<spdlog::logger>("async_logger", async_sink);
logger->set_level(spdlog::level::trace);
2024-12-07 19:54:40 +08:00
spdlog::stopwatch sw;
2024-12-07 19:54:40 +08:00
for (int i = 0; i < n_threads; ++i) {
threads.emplace_back(worker_thread, logger, n_messages);
2024-12-07 19:54:40 +08:00
}
2020-09-26 20:30:45 +08:00
2024-12-07 19:54:40 +08:00
// wait for worker threads to finish
for (auto &t : threads) {
t.join();
}
2020-09-26 20:30:45 +08:00
2024-12-07 19:54:40 +08:00
auto millis = sw.elapsed_ms().count();
if (millis == 0) {
millis = 1;
}
spdlog::info("Elapsed {} millis ({:L}/sec)", millis, n_total_messages * 1000 / millis);
2020-09-26 20:30:45 +08:00
}
2024-12-07 19:54:40 +08:00
// wait for the worker thread to finish
// check that all messages were processed
if (test_sink->msg_counter() != n_messages * n_threads) {
spdlog::error("Expected: {:L}, Counted: {:L}", n_total_messages,
test_sink->msg_counter());
return false;
2020-09-26 20:30:45 +08:00
}
2024-12-07 19:54:40 +08:00
spdlog::info("OK: {:L}", test_sink->msg_counter());
return true;
2020-09-26 20:30:45 +08:00
}
2024-12-07 19:54:40 +08:00
#include <random>
int main(int, char *[]) {
std::locale::global(std::locale("en_US.UTF-8"));
// constexpr size_t n_messages = 1'00;
// constexpr size_t n_threads = 1;
2024-12-07 19:54:40 +08:00
try {
// random n_messages and n_threads
std::mt19937 gen(std::random_device{}());
for (int i = 0; i < 100; i++) {
auto n_threads = std::uniform_int_distribution<size_t>(1, 10)(gen);
auto n_messages = std::uniform_int_distribution<size_t>(1, 1'000'000)(gen);
2024-12-07 19:54:40 +08:00
spdlog::info("***********************************");
spdlog::info("Test #{}. Threads: {}, Count: {:L}", i, n_threads, n_messages);
2024-12-07 19:54:40 +08:00
if (!async_example2(n_threads, n_messages)) {
spdlog::error("Stopped");
break;
}
}
2020-09-26 20:30:45 +08:00
2024-12-07 19:54:40 +08:00
spdlog::shutdown();
2020-09-26 20:30:45 +08:00
}
2024-12-07 19:54:40 +08:00
// Exceptions will only be thrown upon failed logger or sink construction (not during logging).
catch (const spdlog::spdlog_ex &ex) {
std::printf("Log initialization failed: %s\n", ex.what());
return 1;
2024-12-02 05:59:09 +08:00
}
}