C++ 組み込みの clog を再定義して、元の clog.rdbuf() のティーとして処理される新しい関連付けられた rdbuf() とログへの ofstream オブジェクトの rdbuf() を使用する方法の例を誰かが持っていますか?ディスク上のファイル。
目的は、コード全体で std::clog を使用することですが、デフォルトの clog 宛先とディスク上のログ ファイルの両方に移動するようにすることです。
ありがとう。
-ウィリアム
カスタム streambuf 派生クラスを作成する必要があります。ofstream の rdbuf と元の clog rdbuf の両方にデータを吐き出させます。
カスタム streambuf を記述する一般的な例:
http://www.dreamincode.net/code/snippet2499.htm
新しいストリーム バッファをスタッシュするには、次のようにします。
// grab buffer for clog
std::streambuf* oldClogBuf = std::clog.rdbuf();
// create custom buffer which feeds both clog and an ofstream
CustomBuffer* customBuf = new CustomBuffer( oldClogBuf );
// stash custom buffer
std::clog.rdbuf( customBuf );
...do stuff...
// restore original clog buffer
std::clog.rdbuf( oldClogBuf );
RAII イディオムを使用してバッファーの切り替えを管理することで、全体をより堅牢にすることができます。