Skip to content

Some short guidance for adding sinks on the fly  #3014

@damienhocking

Description

@damienhocking

We had a need to add a callback sink to our logs so we can reprocess log messages to different formats (json and MQTT) while keeping the original logging. I discovered a small gotcha. To add a sink, be careful how you access sinks() in the logger. This doesn't work:

auto sinks = log_->sinks();
sinks.push_back(std::make_shared<spdlog::sinks::callback_sink<std::mutex> >([this](const spdlog::details::log_msg& msg) {this->LogCallback(msg);}));

"auto sinks = ..." gives you a copy of the sinks vector. You're not modifying the sinks vector in the logger. You need to use auto& (or the original sink_ptr type defined in common.h, but it needs to be a reference to the vector):

auto& sinks = log_->sinks();
sinks.push_back( .....etc

Hopefully this saves someone else an hour of head-scratching.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions