-
Notifications
You must be signed in to change notification settings - Fork 4.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
SEGV when using spdlog inside dll #1562
Comments
As far as the backtrace shows, in the This is a strange occurrence. spdlog/include/spdlog/details/registry-inl.h Lines 277 to 281 in 83b9149
spdlog/include/spdlog/details/registry.h Lines 97 to 101 in 83b9149
This is a strange occurrence. Can you provide a snippet? NOTE: I've had similar issue reports before. The cause of this bug is the initialization of spdlog with the |
thank you for the fast answer. header: #ifndef LIBRETHFELD__LOGGING_HPP_
#define LIBRETHFELD__LOGGING_HPP_
//librethfeld
//global
#include <memory>
#include "spdlog/fmt/ostr.h"
#include "spdlog/spdlog.h"
#include <vector>
namespace librethfeld {
static std::shared_ptr<spdlog::logger> logger;
std::shared_ptr<spdlog::logger> setup_logger(
std::vector<spdlog::sink_ptr> sinks,
std::string logger_name
);
}
#endif //LIBRETHFELD__LOGGING_HPP_ cpp file: //librethfeld
#include "librethfeld/logging.hpp"
//global
#include <memory>
#include "spdlog/async.h"
#include "spdlog/fmt/ostr.h"
#include "spdlog/sinks/rotating_file_sink.h"
#include "spdlog/sinks/stdout_color_sinks.h"
#include "spdlog/spdlog.h"
#include <vector>
std::shared_ptr<spdlog::logger> librethfeld::setup_logger(std::vector<spdlog::sink_ptr> sinks, std::string logger_name)
{
std::shared_ptr<spdlog::logger> 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 {
/*
auto console_sink = std::make_shared<spdlog::sinks::stdout_color_sink_mt>();
console_sink->set_pattern("[%^%l%$] %v");
console_sink->set_level(spdlog::level::info);
auto file_sink = std::make_shared<spdlog::sinks::rotating_file_sink_mt>("var/log/librethfeld", 1048576 * 5, 10, true);
file_sink->set_pattern("[%^%l%$] %v");
file_sink->set_level(spdlog::level::trace);
spdlog::sinks_init_list sink_list = {file_sink, console_sink };
auto logger = std::make_shared<spdlog::async_logger>(logger_name, sink_list, spdlog::thread_pool(), spdlog::async_overflow_policy::block);
logger->set_level(spdlog::level::trace);
*/
logger = spdlog::stdout_color_mt(logger_name);
}
}
librethfeld::logger = logger;
return logger;
} |
@L0ric0 Are you really declaring I took a hint from your snippet and made a small project to generate a shared library and application that links to the static spdlog. https://github.com/tt4g/spdlog-issue-1562 Project source hereLibrary header: #ifndef SPDLOG_ISSUE_1562_HPP
#define SPDLOG_ISSUE_1562_HPP
#include "spdlog_issue_1562_export.hpp"
#include "spdlog/spdlog.h"
#include "spdlog/fmt/ostr.h"
#include <memory>
#include <vector>
namespace spdlog_issue_1562
{
static std::shared_ptr<spdlog::logger> logger;
std::shared_ptr<spdlog::logger> SPDLOG_ISSUE_1562_API setup_logger(
std::vector<spdlog::sink_ptr> sinks,
std::string logger_name
);
} // namespace spdlog_issue_1562
#endif // SPDLOG_ISSUE_1562_HPP Library implementation: #include "spdlog_issue_1562.hpp"
#include "spdlog/async.h"
#include "spdlog/sinks/rotating_file_sink.h"
#include "spdlog/sinks/stdout_color_sinks.h"
#include <iterator>
#include <string>
namespace spdlog_issue_1562
{
std::shared_ptr<spdlog::logger> setup_logger(
std::vector<spdlog::sink_ptr> sinks,
std::string logger_name)
{
std::shared_ptr<spdlog::logger> logger = spdlog::get(logger_name);
if (!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);
}
}
spdlog_issue_1562::logger = logger;
return logger;
}
} // namespace spdlog_issue_1562
main.cpp: #include "spdlog_issue_1562/spdlog_issue_1562.hpp"
#include <iostream>
int main(int argc, char** argv)
{
auto logger = spdlog_issue_1562::setup_logger({}, "main");
if (logger) {
logger->info("Hello, World!");
} else {
std::cerr << "invalid logger\n";
}
if (spdlog_issue_1562::logger) {
spdlog_issue_1562::logger->info("global: Hello, World!");
} else {
std::cerr << "invalid spdlog_issue_1562::logger\n";
}
return 0;
} When I build and run this project with MSVC, I see the following output: $ spdlog_issue_1562
[2020-05-20 21:46:47.314] [main] [info] Hello, World!
invalid spdlog_issue_1562::logger That is, when an application accesses the Can you build this project too and see if you get the same results as me? |
ok i'm getting the same output as you. does that mean i have to declare the logger var as |
Right. As an implementation, the library user accesses the global logger through an access function. Example:Library header: ```diff - static std::shared_ptr logger; + std::shared_ptr SPDLOG_ISSUE_1562_API global_logger();
NOTE: If you are accessing the global logger from multiple applications at the same time, the read/write operation to the global logger must be atomic. |
that is working like a charm. thanks for your quick and extensive help |
i'm trying to use spdlog inside of an so/dll. i'm getting a segfault when trying to call the line
logger = spdlog::stdout_color_mt(logger_name);
from https://github.com/gabime/spdlog/wiki/How-to-use-spdlog-in-DLLsher is the backtrace:
The text was updated successfully, but these errors were encountered: