Код: Выделить всё
#include
#include
#include
#include
static std::shared_ptr s_Logger;
void Initialize(const std::string& logFilePath, spdlog::level::level_enum level) {
// Step 1: Initialize the global thread pool BEFORE creating the logger
spdlog::init_thread_pool(8192, 1); // Queue size 8192, 1 worker thread
// Step 2: Create console and file sinks
auto consoleSink = std::make_shared();
auto fileSink = std::make_shared(logFilePath, true); // 'true' for append mode
// Set log patterns for sinks
consoleSink->set_pattern("[%Y-%m-%d %H:%M:%S.%e] [%t] [%^%l%$] [%n]: %v");
fileSink->set_pattern("[%Y-%m-%d %H:%M:%S.%e] [%t] [%l] [%n]: %v");
// Step 3: Create an asynchronous logger
s_Logger = std::make_shared( //
"MAIN",
spdlog::sinks_init_list{ consoleSink, fileSink },
spdlog::thread_pool(),
spdlog::async_overflow_policy::block // Block if the queue is full
);
// Step 4: Set the log level and flush policy
s_Logger->set_level(level);
s_Logger->flush_on(spdlog::level::info); // Flush on info or higher levels
// Step 5: Register the logger globally
spdlog::register_logger(s_Logger);
spdlog::set_default_logger(s_Logger);
}
int main()
{
Initialize("log.txt", spdlog::level::trace);
spdlog::info("Test 1"); // No output
s_Logger->info("Test 2"); // No output
s_Logger->flush(); // Output here
}
Подробнее здесь: https://stackoverflow.com/questions/792 ... s-intended
Мобильная версия