-
Notifications
You must be signed in to change notification settings - Fork 5k
Description
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.