简体   繁体   English

如何使用boost :: iostreams :: null_sink作为std :: ostream

[英]How to use boost::iostreams::null_sink as std::ostream

I want to make my output verbose/non verbose based on a flag given at runtime. 我想根据运行时给出的标志使输出详细/非详细。 My idea is, to construct a std::ostream dependent of the flag, such as in: 我的想法是,构造一个依赖于标志的std :: ostream,例如:

std::ostream out;
if (verbose) {
    out = std::cout
else {
    // Redirect stdout to null by using boost's null_sink.
    boost::iostreams::stream_buffer<boost::iostreams::null_sink> null_out{boost::iostreams::null_sink()};

    // Somehow construct a std::ostream from nullout
}

Now I'm stuck with constructing a std::ostream from such a boost streambuffer. 现在我不得不从这样的提升streambuffer构建一个std :: ostream。 How would I do this? 我该怎么做?

Using Standard Library 使用标准库

Just reset the rdbuf : 只需重置rdbuf

auto old_buffer = std::cout.rdbuf(nullptr);

Otherwise, just use a stream: 否则,只需使用一个流:

std::ostream nullout(nullptr);
std::ostream& out = verbose? std::cout : nullout;

See it Live On Coliru 看到Live On Coliru

#include <iostream>

int main(int argc, char**) {
    bool verbose = argc>1;

    std::cout << "Running in verbose mode: " << std::boolalpha << verbose << "\n";

    std::ostream nullout(nullptr);
    std::ostream& out = verbose? std::cout : nullout;

    out << "Hello world\n";
}

When run ./test.exe : 运行./test.exe

Running in verbose mode: false

When run ./test.exe --verbose : 运行时./test.exe --verbose

Running in verbose mode: true
Hello world

Using Boost Iostreams 使用Boost Iostreams

You /can/ of course use Boost IOstreams if you insist: 如果您坚持,您/可以/当然使用Boost IOstreams:

Note, as per the comments, this is strictly better because the stream will not be in "error" state all the time. 请注意,根据评论,这是非常好的,因为流不会一直处于“错误”状态。

Live On Coliru 住在Coliru

#include <iostream>
#include <boost/iostreams/device/null.hpp>
#include <boost/iostreams/stream.hpp>

int main(int argc, char**) {
    bool verbose = argc>1;

    std::cout << "Running in verbose mode: " << std::boolalpha << verbose << "\n";

    boost::iostreams::stream<boost::iostreams::null_sink> nullout { boost::iostreams::null_sink{} };
    std::ostream& out = verbose? std::cout : nullout;

    out << "Hello world\n";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将 boost IOStreams 与 std::ostream_iterator 一起使用 - Using boost IOStreams with std::ostream_iterator 如何在Boost :: Log中使用压缩器Boost :: Iostreams过滤器作为接收器 - How to use a compressor Boost::Iostreams filter as a sink in Boost::Log 使用boost :: iostreams :: zlib_compressor为什么需要销毁boost :: iostream :: filtering_ostream才能写入接收器? - Why boost::iostream::filtering_ostream using boost::iostreams::zlib_compressor needs to be destroyed for the sink to be written? 如何使用boost :: iostreams管道进入std :: cout - How to pipe into std::cout with boost::iostreams 如何禁用boost :: iostreams接收器中的缓冲区? - How can you disable the buffer in a boost::iostreams sink? boost :: iostreams :: copy()关闭源但不关闭接收器 - boost::iostreams::copy() closes the source but not the sink 如何更改 C++ boost::iostreams 中源/接收器设备的读/写方法? - How to change read/write method of source/sink device in C++ boost::iostreams? std :: fstream和Boost Iostreams库之间的区别 - Difference between std::fstream and Boost Iostreams Library boost :: iostreams :: mapped_file_sink抛出未知异常 - boost::iostreams::mapped_file_sink throws unknown exception boost :: iostreams :: copy - sink - ENOSPC(设备上没有剩余空间)错误处理 - boost::iostreams::copy - sink - ENOSPC (No space left on device) error handling
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM