Skip to content

Problem casting ringbuffer_sink_st #2411

Answered by tt4g
dradic2 asked this question in Q&A
Discussion options

You must be logged in to vote

Don't use C style cast for casting std::shared_ptr<T>. Must use std::dynamic_pointer_cast instead (this is a problem with the use of std::shared_ptr<T>).
See: https://en.cppreference.com/w/cpp/memory/shared_ptr/pointer_cast

- 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.
+ }

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@dradic2
Comment options

Answer selected by dradic2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants