如何监控 c++ 中的流
通常,当用 C++ 编写文件编写器或解析器时,我想在写入/读取文件时监视编写器/解析器的进度。为此,我正在考虑实现类似的东西:
ostream_monitor my_monitor(&my_output_stream, &cout);
my_monitor << my_variable;
这将具有将 my_variable 输出到 my_output_stream 以及 cout 的效果,这样我就可以看到它。
同样,最好有一个相同的输入版本:
istream_monitor my_monitor(&my_input_stream, &cout);
my_monitor >> my_variable;
这将从 my_input_stream 中读取下一个标记,并使用它来设置 my_variable,同时还将任何读取的字符输出到 cout,这样我就可以看到流了被阅读。
我可以继续尝试创建 std::istream 和 std::ostream 的子类来执行此操作,但似乎可能需要对可能有用或可能没有用的调试工具进行大量工程。因此,我会问:
您如何通过文件监视解析器的进度?我有兴趣听到任何看起来比上述解决方案更容易实施的解决方案。
Often when writing file writers or parsers in c++, I would like to monitor the writer/parser's progress through the file as it gets written/read. To this end, I was thinking of implementing something like:
ostream_monitor my_monitor(&my_output_stream, &cout);
my_monitor << my_variable;
This would have the effect of outputting my_variable to my_output_stream, and also to cout, so I can see it.
Likewise, it'd be nice to have an input version of the same:
istream_monitor my_monitor(&my_input_stream, &cout);
my_monitor >> my_variable;
This would read the next token off of my_input_stream, and use it to set my_variable, but also output any read characters to cout, so I can see the stream as it gets read.
I could go ahead and try to make subclasses of std::istream and std::ostream which do this, but it seems like potentially a lot of engineering for a debug tool that may or may not be useful. I am therefore prompted to ask:
What do you do to monitor the progress of a parser through a file? I am interested in hearing any solutions that seem simpler to implement than the one above.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
似乎 boost.IOStreams 的 tee_device 和可能的 tee_filter 可以用来快速实现我上面描述的内容: 我怎样才能组成输出流,因此输出一次会到达多个地方?
Seems like boost.IOStreams' tee_device, and possibly tee_filter, can be used to quickly implement what I describe above: How can I compose output streams, so output goes multiple places at once?