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

How do I log a std::iostream? #3144

Open
coyorkdow opened this issue Jul 25, 2024 · 2 comments
Open

How do I log a std::iostream? #3144

coyorkdow opened this issue Jul 25, 2024 · 2 comments

Comments

@coyorkdow
Copy link

I have a pointer of std::iostream, the object it points to might be std::stringstream, std::fstream, or anything inherit the base type. What is the most efficient and elegant way to extract its content and print it via spdlog?

I can do the following.

 std::shared_ptr<std::iostream> content_stream = result.value()->getContent();
 std::stringstream ss;
 ss << content_stream->rdbuf();
 spdlog::info("object content is\n{}", ss.str());

Is there any better way? I don't think my current approach either efficient or elegant...

@tt4g
Copy link
Contributor

tt4g commented Jul 25, 2024

The logging argument format uses fmt library, so spdlog does not support methods not supported by fmt.

@gabime
Copy link
Owner

gabime commented Jul 25, 2024

An elegant way would be to define custom type that accepts std::iostream, using https://fmt.dev/latest/api/#formatting-user-defined-types

And then one can just pass the stream to spdlog:

spdlog::info("Stream content is: {}", content_stream);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants