-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
Add more usages for fmt::ostream #2354
Comments
I think this is good idea. It would make code more testable, because you can replace console output with string output and make assertions on that string. I wonder how hard would be to actually implement it? |
It'll be also great in embedded platforms, for "printing" to a serial port! |
Is any progress?? void print_something_anywhere(std::ostream &output)
{
output<<"hello world""<<std::endl;
// ...
} Then, the Can |
There is no update on the {fmt}-only solution but you can use |
Added an experimental #include <fmt/os.h>
void write_text(fmt::writer w) {
w.print("The answer is {}.", 42);
}
int main() {
// Write to FILE.
write_text(stdout);
// Write to fmt::ostream.
auto f = fmt::output_file("myfile");
write_text(f);
// Write to std::string.
auto sb = fmt::string_buffer();
write_text(sb);
std::string s = sb.str();
} It is more efficient than Thanks for the suggestion! |
I noticed
fmt::output_file()
returns a specialfmt::ostream
. It'd be nice if there was something similar for printing to the screen (cout
stream replacement) / building into a string (ostringstream
replacement).Then we could do something like this:
at the moment I think we have to fall back to stl streams in order to do this? But I thought those were slow in practice.
Please let me know if there's something I'm missing!
The text was updated successfully, but these errors were encountered: