将std :: cout重定向到自定义编写器

Dus*_*etz 6 c++ iostream stream stringstream streambuf

我想使用Mr-Edd的iostreams文章中的这个片段在某处打印std :: clog.

#include <iostream>
#include <iomanip>
#include <string>
#include <sstream>

int main()
{
    std::ostringstream oss;

    // Make clog use the buffer from oss
    std::streambuf *former_buff =
        std::clog.rdbuf(oss.rdbuf());

    std::clog << "This will appear in oss!" << std::flush;

    std::cout << oss.str() << '\\n';

    // Give clog back its previous buffer
    std::clog.rdbuf(former_buff);

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

所以,在一个主循环中,我会做类似的事情

while (! oss.eof())
{
    //add to window text somewhere
}
Run Code Online (Sandbox Code Playgroud)

这是ostringstream文档,但我无法理解这样做的最佳方法.我有一个显示文本的方法,我只想用ostringstream中的任何数据调用它.

将发送到std :: clog的任何内容重定向到我选择的方法的最简单/最好的方法是什么?它是如上所述,并填写while!eof部分(不确定如何),或者是否有更好的方法,比如通过在调用我的方法的地方重载一些'commit'运算符?我喜欢快速简单,我真的不想开始定义接收器,就像文章那样使用boost iostreams - 这些东西已经超出了我的想象.

Éri*_*ant 11

我鼓励你看看Boost.IOStreams.它似乎很适合您的用例,使用它非常简单:

#include <boost/iostreams/concepts.hpp> 
#include <boost/iostreams/stream_buffer.hpp>
#include <iostream>

namespace bio = boost::iostreams;

class MySink : public bio::sink
{
public:
    std::streamsize write(const char* s, std::streamsize n)
    {
        //Do whatever you want with s
        //...
        return n;
    }
};

int main()
{
    bio::stream_buffer<MySink> sb;
    sb.open(MySink());
    std::streambuf * oldbuf = std::clog.rdbuf(&sb);
    std::clog << "hello, world" << std::endl;
    std::clog.rdbuf(oldbuf);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)


Eva*_*ran 7

你想从ostream中取出文本而不是空的.你可以这样做:

std::string s = oss.str();
if(!s.empty()) {
    // output s here
    oss.str(""); // set oss to contain the empty string
}
Run Code Online (Sandbox Code Playgroud)

如果这不是您想要的,请告诉我.

当然,更好的解决方案是移除中间人,并在您真正想要的地方使用新的streambuf ,无需稍后进行探测.这样的事情(注意,这适用于每个char,但streambufs中还有很多缓冲选项):

class outbuf : public std::streambuf {
public:
    outbuf() {
        // no buffering, overflow on every char
        setp(0, 0);
    }

    virtual int_type overflow(int_type c = traits_type::eof()) {
        // add the char to wherever you want it, for example:
        // DebugConsole.setText(DebugControl.text() + c);
        return c;
    }
};

int main() {
    // set std::cout to use my custom streambuf
    outbuf ob;
    std::streambuf *sb = std::cout.rdbuf(&ob);

    // do some work here

    // make sure to restore the original so we don't get a crash on close!
    std::cout.rdbuf(sb);
    return 0;
Run Code Online (Sandbox Code Playgroud)

}


man*_*ake 6

我需要从第三方库获取 std::cout 和 std::cerr 的输出并使用 log4cxx 记录它们,并且仍然保留原始输出。

这就是我想出来的。这非常简单:

  • 我用我自己的类替换了 ostream 的旧缓冲区(如 std::cout),以便我可以访问写入其中的内容。

  • 我还使用旧缓冲区创建一个新的 std::ostream 对象,以便除了将其发送到记录器之外,我还可以继续将输出发送到控制台。我觉得这很方便。

代码:

class intercept_stream : public std::streambuf{
public:
    intercept_stream(std::ostream& stream, char const* logger):
      _logger(log4cxx::Logger::getLogger(logger)),
      _orgstream(stream),
      _newstream(NULL)
    {
        //Swap the the old buffer in ostream with this buffer.
        _orgbuf=_orgstream.rdbuf(this);
        //Create a new ostream that we set the old buffer in
        boost::scoped_ptr<std::ostream> os(new std::ostream(_orgbuf));
        _newstream.swap(os);
    }
    ~intercept_stream(){
        _orgstream.rdbuf(_orgbuf);//Restore old buffer
    }
protected:
    virtual streamsize xsputn(const char *msg, streamsize count){
        //Output to new stream with old buffer (to e.g. screen [std::cout])
        _newstream->write(msg, count);
        //Output to log4cxx logger
        std::string s(msg,count);
        if (_logger->isInfoEnabled()) {
            _logger->forcedLog(::log4cxx::Level::getInfo(), s, LOG4CXX_LOCATION); 
        }
        return count;
    }
private:
    log4cxx::LoggerPtr _logger;
    std::streambuf*    _orgbuf;
    std::ostream&      _orgstream;
    boost::scoped_ptr<std::ostream>  _newstream;
};
Run Code Online (Sandbox Code Playgroud)

然后使用它:

std::cout << "This will just go to my console"<<std::endl;
intercept_stream* intercepter = new intercept_stream(std::cout, "cout");
std::cout << "This will end up in both console and my log4cxx logfile, yay!" << std::endl;
Run Code Online (Sandbox Code Playgroud)