-
Hello, I am using the ring buffer sink in a multiple sink configuration. Setting up the logger in main and using it somewhere else.
This seems to be working as I can see the log messages in the buffer. The problem is, how cast the sink to ring_buffer_st to be able to use last_formatted(10)
I get the following error
If I don't try casting, I can't use last_formatted() Help would be appreciated! Thanks, |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Don't use C style cast for casting - auto ringBufferSink = (std::make_shared<spdlog::sinks::ringbuffer_sink_st>)(logger->sinks().end());
+ auto ringBufferSink = std::dynamic_pointer_cast<spdlog::sinks::ringbuffer_sink_st>(logger->sinks().back());
+ if (ringBufferSink) {
+ ringBufferSink->last_formatted(10);
+ } else {
+ // Failed to cast.
+ } |
Beta Was this translation helpful? Give feedback.
Don't use C style cast for casting
std::shared_ptr<T>
. Must usestd::dynamic_pointer_cast
instead (this is a problem with the use ofstd::shared_ptr<T>
).See: https://en.cppreference.com/w/cpp/memory/shared_ptr/pointer_cast