他の複数のストリームに書き込むストリームバッファを作成し、std::cout
にインストールすることができます。これは古い十年以上、私は、まさにこのcomp.lang.c++ and comp.lang.c++.moderatedで(あった複数の記事に書いた記事を見て、持つことができますどのように動作するかの詳細については
#include <streambuf>
struct teebuf:
std::streambuf
{
teebuf(std::streambuf* sb0, std::streambuf* sb1): sb0_(sb0), sb1_(sb1) {}
int_type overflow(int_type c) {
this->sb0_->sputc(c);
this->sb1_->sputc(c);
return traits_type::not_eof(c);
}
int sync() {
this->sb0_->pubsync();
this->sb1_->pubsync();
return 0;
}
std::streambuf* sb0_;
std::streambuf* sb1_;
};
#include <iostream>
#include <fstream>
int main() {
std::ofstream file("file.log");
teebuf tee(std::cout.rdbuf(), file.rdbuf());
std::streambuf* orig(std::cout.rdbuf());
std::cout.rdbuf(&tee);
std::cout << "hello, world!\n";
std::cout.rdbuf(orig);
}
...:基本的に、これは次のようになります)。
ログライブラリが必要ですか? –