在C++中,是否有一種智能的方式來將輸出從stdout鏡像到控制檯和文件? 我希望有一種方法可以像this question那樣做。鏡像控制檯輸出到C++文件
編輯:這將是很好能夠只用標準庫做到這一點(即:沒有提升)..
在C++中,是否有一種智能的方式來將輸出從stdout鏡像到控制檯和文件? 我希望有一種方法可以像this question那樣做。鏡像控制檯輸出到C++文件
編輯:這將是很好能夠只用標準庫做到這一點(即:沒有提升)..
你可以嘗試通過了Boost.Iostreams提供Tee Device。
Tee設備將輸出指向多個流。據我所知,你可以鏈接它們以從一個write
呼叫到達理論上無限的輸出設備。
This answer顯示了一個如何做到你想要的例子。
另外,只需啓動您的程序,使其連接到tee命令。
您可以通過創建一個擴展std::streambuf
,並具有std::ofstream
成員的類做到這一點。在覆蓋std::streambuf::overflow
和std::streambuf::sync
成員函數之後,您將全部設置好。
以下大部分代碼均來自here。我已經指出了我爲文件鏡像添加的內容(「ADDED:」)。這可能過於複雜,因爲我在工作中,無法完全簡化它,但它的工作原理 - 這樣做的獎金(而不僅僅是使用std::streambuf*
是任何代碼(說你有一個一個寫入std::cout
外部庫)將寫信給你的文件
mystreambuf.h
#ifndef MYSTREAMBUF_H
#define MYSTREAMBUF_H
template <typename charT, typename traits = std::char_traits<charT> >
class mystreambuf : public std::basic_streambuf<charT, traits>
{
public:
// The size of the input and output buffers.
static const size_t BUFF_SIZE = 1024;
typedef traits traits_type;
typedef typename traits_type::int_type int_type;
typedef typename traits_type::pos_type pos_type;
typedef typename traits_type::off_type off_type;
// You can use any method that you want, but here, we'll just take in a raw streambuf as a
// slave I/O object. xor_char is what each character is xored with before output.
explicit mystreambuf(std::streambuf* buf)
: out_buf_(new charT[BUFF_SIZE])
{
// ADDED: store the original cout stream and open our output file
this->original_cout = buf;
outfile.open("test.txt");
// Initialize the put pointer. Overflow won't get called until this buffer is filled up,
// so we need to use valid pointers.
this->setp(out_buf_, out_buf_ + BUFF_SIZE - 1);
}
// It's a good idea to release any resources when done using them.
~mystreambuf() {
delete [] out_buf_;
// ADDED: restore cout, close file
std::cout.rdbuf(original_cout);
outfile.flush();
outfile.close();
}
protected:
// This is called when there are too many characters in the buffer (thus, a write needs to be performed).
virtual int_type overflow(int_type c);
// This is called when the buffer needs to be flushed.
virtual int_type sync();
private:
// Output buffer
charT* out_buf_;
// ADDED: tracking the original std::cout stream & the file stream to open
std::streambuf* original_cout;
std::ofstream outfile;
};
#endif
mystreambuf.cpp
// Based on class by perfectly.insane from http://www.dreamincode.net/code/snippet2499.htm
#include <fstream>
#include <iostream>
#include <streambuf>
#include "mystreambuf.h"
// This function is called when the output buffer is filled.
// In this function, the buffer should be written to wherever it should
// be written to (in this case, the streambuf object that this is controlling).
template <typename charT, typename traits>
typename mystreambuf<charT, traits>::int_type
mystreambuf<charT, traits>::overflow(typename mystreambuf<charT, traits>::int_type c)
{
charT* ibegin = this->out_buf_;
charT* iend = this->pptr();
// Reset the put pointers to indicate that the buffer is free
// (at least it will be at the end of this function).
setp(out_buf_, out_buf_ + BUFF_SIZE + 1);
// If this is the end, add an eof character to the buffer.
// This is why the pointers passed to setp are off by 1
// (to reserve room for this).
if(!traits_type::eq_int_type(c, traits_type::eof())) {
*iend++ = traits_type::to_char_type(c);
}
// Compute the write length.
int_type ilen = iend - ibegin;
// ADDED: restore cout to its original stream, output to it, output to the file, then set cout's stream back to this, our streambuf)
std::cout.rdbuf(original_cout);
out_buf_[ilen] = '\0';
std::cout << out_buf_;
outfile << out_buf_;
std::cout.rdbuf(this);
return traits_type::not_eof(c);
}
// This is called to flush the buffer.
// This is called when we're done with the file stream (or when .flush() is called).
template <typename charT, typename traits>
typename mystreambuf<charT, traits>::int_type
mystreambuf<charT, traits>::sync()
{
return traits_type::eq_int_type(this->overflow(traits_type::eof()),
traits_type::eof()) ? -1 : 0;
}
int main(int argc, char* argv[]) {
mystreambuf<char> filter(std::cout.rdbuf());
std::cout.rdbuf(&filter);
std::cout << "Hello World" << std::endl;
return 0;
}
希望這有助於。乾杯
我認爲最接近你可以來的是一個巧妙編寫的'#define'或兩個。 – suszterpatt 2012-03-19 02:40:51