Skip to content
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

spdlog: do not drop logger in destructor #3116

Merged
merged 1 commit into from
Apr 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions libraries/afu-test/afu_test.h
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,24 @@ inline std::vector<std::string> spdlog_levels() {
inline std::vector<std::string> spdlog_levels() { return SPDLOG_LEVEL_NAMES; }
#endif // SPDLOG_VERSION

// Dropping a logger is optional, but not doing so will cause an exception
// when re-registering a logger with the same name, e.g., in test runs.
class scoped_register_logger {
public:
scoped_register_logger(std::shared_ptr<spdlog::logger> logger)
: logger_(std::move(logger)) {
spdlog::register_logger(logger_);
}

~scoped_register_logger() { spdlog::drop(logger_->name()); }

scoped_register_logger(scoped_register_logger const &) = delete;
void operator=(scoped_register_logger const &) = delete;

private:
std::shared_ptr<spdlog::logger> logger_;
};

class afu {
public:
typedef int (*command_fn)(afu *afu, CLI::App *app);
Expand Down Expand Up @@ -186,8 +204,6 @@ class afu {
app_.add_option("-t,--timeout", timeout_msec_, "test timeout (msec)")->default_str(std::to_string(timeout_msec_));
}
virtual ~afu() {
if (logger_)
spdlog::drop(logger_->name());
}

CLI::App & cli() { return app_; }
Expand Down Expand Up @@ -310,7 +326,7 @@ class afu {

auto console_sink = std::make_shared<spdlog::sinks::stdout_color_sink_mt>();
logger_ = std::make_shared<spdlog::logger>(test->name(), console_sink);
spdlog::register_logger(logger_);
scoped_register_logger register_logger(logger_);
logger_->set_level(spdlog::level::from_str(log_level_));

int res = open_handle(test->afu_id());
Expand Down
21 changes: 19 additions & 2 deletions samples/cxl_hello_fpga/he_cache_test.h
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,24 @@ inline std::vector<std::string> spdlog_levels() {
inline std::vector<std::string> spdlog_levels() { return SPDLOG_LEVEL_NAMES; }
#endif // SPDLOG_VERSION

// Dropping a logger is optional, but not doing so will cause an exception
// when re-registering a logger with the same name, e.g., in test runs.
class scoped_register_logger {
public:
scoped_register_logger(std::shared_ptr<spdlog::logger> logger)
: logger_(std::move(logger)) {
spdlog::register_logger(logger_);
}

~scoped_register_logger() { spdlog::drop(logger_->name()); }

scoped_register_logger(scoped_register_logger const &) = delete;
void operator=(scoped_register_logger const &) = delete;

private:
std::shared_ptr<spdlog::logger> logger_;
};

class afu {
public:
typedef int (*command_fn)(afu *afu, CLI::App *app);
Expand Down Expand Up @@ -297,7 +315,6 @@ class afu {
}
virtual ~afu() {
if (fd_ > 0) close(fd_);
if (logger_) spdlog::drop(logger_->name());
}

CLI::App &cli() { return app_; }
Expand Down Expand Up @@ -449,7 +466,7 @@ class afu {

auto console_sink = std::make_shared<spdlog::sinks::stdout_color_sink_mt>();
logger_ = std::make_shared<spdlog::logger>(test->name(), console_sink);
spdlog::register_logger(logger_);
scoped_register_logger register_logger(logger_);
logger_->set_level(spdlog::level::from_str(log_level_));
current_command_ = test;
if (find_dev_feature() != 0) {
Expand Down
22 changes: 19 additions & 3 deletions samples/cxl_host_exerciser/he_cache_test.h
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,24 @@ inline std::vector<std::string> spdlog_levels() {
inline std::vector<std::string> spdlog_levels() { return SPDLOG_LEVEL_NAMES; }
#endif // SPDLOG_VERSION

// Dropping a logger is optional, but not doing so will cause an exception
// when re-registering a logger with the same name, e.g., in test runs.
class scoped_register_logger {
public:
scoped_register_logger(std::shared_ptr<spdlog::logger> logger)
: logger_(std::move(logger)) {
spdlog::register_logger(logger_);
}

~scoped_register_logger() { spdlog::drop(logger_->name()); }

scoped_register_logger(scoped_register_logger const &) = delete;
void operator=(scoped_register_logger const &) = delete;

private:
std::shared_ptr<spdlog::logger> logger_;
};

class afu {
public:
typedef int (*command_fn)(afu *afu, CLI::App *app);
Expand Down Expand Up @@ -304,8 +322,6 @@ class afu {

if (fd_ > 0)
close(fd_);
if (logger_)
spdlog::drop(logger_->name());
}

CLI::App &cli() { return app_; }
Expand Down Expand Up @@ -459,7 +475,7 @@ class afu {

auto console_sink = std::make_shared<spdlog::sinks::stdout_color_sink_mt>();
logger_ = std::make_shared<spdlog::logger>(test->name(), console_sink);
spdlog::register_logger(logger_);
scoped_register_logger register_logger(logger_);
logger_->set_level(spdlog::level::from_str(log_level_));
current_command_ = test;
if (find_dev_feature() != 0) {
Expand Down
Loading