Page:
How to use spdlog in DLLs
Pages
3. Custom formatting)
Asynchronous logging
CMake
Creating loggers
Custom formatting
Default logger
Error handling
FAQ
Flush policy
Home
How to use spdlog in DLLs
Integrating into Eclipse IDE
Language bindings
Logger registry
Makefile Only
Note for adding sinks on the fly
Preventing child processes from inheriting open log files
Preventing undefined thread ids in child processes
QuickStart
Reference documentation using Doxygen
Setting up JSON logging with spdlog
Sinks
Thread Safety
Tweaking
Windows unicode filenames
No results
2
How to use spdlog in DLLs
Matthew Pare edited this page 2022-01-12 12:12:31 -08:00
Table of contents
Problem
Because spdlog is header-only, building shared libraries and using it in a main program will not share the registry between them.
This means that calls to functions like spdlog::set_level(spdlog::level::level_enum::info) will not change loggers in DLLs.
Workaround
What can be done is to register the logger in both registries.
/*
* Disclaimer:
* This was not compiled but extracted from documentation and some code.
*/
// mylibrary.h
// In library, we skip the symbol exporting part
#include <memory>
#include <vector>
#include <spdlog/spdlog.h>
#include <spdlog/logger.h>
#include <spdlog/sinks/stdout_color_sinks.h>
namespace library
{
static const std::string logger_name = "example";
std::shared_ptr<spdlog::logger> setup_logger(std::vector<spdlog::sink_ptr> sinks)
{
auto logger = spdlog::get(logger_name);
if(not logger)
{
if(sinks.size() > 0)
{
logger = std::make_shared<spdlog::logger>(logger_name,
std::begin(sinks),
std::end(sinks));
spdlog::register_logger(logger);
}
else
{
logger = spdlog::stdout_color_mt(logger_name);
}
}
return logger;
}
void test(std::string message)
{
auto logger = spdlog::get(logger_name);
if(logger)
{
logger->debug("{}::{}", __FUNCTION__, message);
}
}
}
// In the main program
#include <mylibrary.h>
#include <spdlog/logger.h>
#include <spdlog/sinks/daily_file_sink.h>
#include <spdlog/sinks/stdout_sinks.h>
int main()
{
// We assume that we load the library here
...
// Let's use the library
std::vector<spdlog::sink_ptr> sinks;
sinks.push_back(std::make_shared<spdlog::sinks::stdout_sink_st>());
sinks.push_back(std::make_shared<spdlog::sinks::daily_file_sink_st>("logfile", 23, 59));
auto logger = library::setup_logger(sinks);
spdlog::set_level(spdlog::level::level_enum::debug); // No effect for the library.
library::test("Hello World!"); // No logging
spdlog::register_logger(logger);
// Now this will also affect the library logger
spdlog::set_level(spdlog::level::level_enum::debug);
library::test("Hello World!"); // Hurray !
return 0;
}
©gabime 2023-2024 spdlog. All Rights Reserved.