记录标准输入和标准输出
Logging stdin and stdout
有没有一种(理想情况下简单而优雅)的方式来记录stdin
和stdout
?
请注意,我不打算重定向流。我希望标准流保持功能以与其他软件通信,同时将所有进程间通信写入某个文件。
选项 1:
正如@PaulR建议的那样,您可以使用外部进程,例如tee(在Linux/Mac/Unix上),或者编写自己的进程以循环从stdin读取并写入stdout和另一个文件。
选项 2:
我多年前就用 std::basic_ios::rdbuf 做了这件事std::cout
.所要做的就是定义一个类(参见std::filebuf
和std::streambuf
):
class tee_buf : public std::filebuf {
public:
// Not an owing pointer
tee_buf(std::streambuf * other_stream)
: m_other_stream(other_stream) {}
void swap( tee_buf& rhs );
// No need to override open/close since we need to manage only the file.
// The open/close calls don't touch the other_stream.
protected:
int_type overflow(int_type c = traits_type::eof()) override;
// The parent calls this->overflow(), but then still need to call
// m_other_stream->sync(). This is problematic since m_other_stream
// gets flushed twice.
int sync() override;
pos_type seekoff( off_type off,
std::ios_base::seekdir dir,
std::ios_base::openmode which) override {
return pos_type(off_type(-1)); // ???
}
pos_type seekpos( pos_type sp,
std::ios_base::openmode which) override {
return pos_type(off_type(-1)); // ???
}
....
这对于密集的 IO 更有效,因为它避免了中间人。但在大多数情况下,三通解决方案更简单、更可取。如果性能是一个问题(在大多数情况下不是),则可以使两个流缓冲区共享一个内存缓冲区。也可以使用异步 IO 并行写入两个流。
内存泄漏的用法:
std::cout.rdbuf(new tee_buf(std::cout.rdbuf());
无内存泄漏的使用:
编写一个RAII类来包含tee_buf
,以保存原始内容并设置新std::cout.rdbuf()
。销毁后恢复std::cout.rdbuf()
状态。创建此类的单个实例,它将在构造和销毁时完成肮脏的工作。
至于C风格的stdout
:我不相信有办法覆盖它的行为。最多可以使用缓冲内存,但这还不足以获得所需的功能。有了stdout
,唯一能做的就是使用类似tee
的解决方案。
这应该相当简单 用tee
.这将允许您维护任何现有的重定向,同时还将标准/标准输出发送到其他地方(在您的情况下是文件)。这也避免了对现有代码进行任何修改。
相关文章:
- 捕获标准输出以压缩并使用 CTRL-C 中断会给出损坏的 zip 文件
- 使用提升过程获取 shell 命令的标准输出
- 将标准输出重定向到ostream
- 如何在C++中执行命令并获取命令的返回代码标准输出和标准
- 在程序执行期间从标准输出重定向到自定义流
- 标准输出C++意外行为
- 防止 qDebug() 写入标准输出
- C++ - <<运算符重载,链表 - 地址而不是标准输出
- 有没有办法知道C++中将打印到标准输出的大小?
- 最后推迟标准输出
- 得到~对标准输出没有回应~在黑客排名问题上
- Win32 GUI C(++) 应用将标准输出和标准输出重定向到磁盘上的同一文件
- 在标准输出中执行打印会导致数组中随机分配值
- 记录标准输入和标准输出
- 如何使用BOOST_LOG_TRIVIAL宏将任何内容记录到标准输出/标准和文件
- c++在标准输入上写入数据,并从标准输出中获得输出
- Python和c++通信标准输出和标准输入
- 在c# GUI项目上重定向标准输出+标准错误
- 帮助从标准输入和标准输出读入.还有运算符重载
- 如何从分离的QProcess中读取标准输出/标准错误