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

8
我想使用来自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;
}

所以,在主循环中,我会做类似这样的事情。
while (! oss.eof())
{
    //add to window text somewhere
}

这里是ostringstream文档,但我不太理解最佳方法。我有一个显示文本的方法,我只想用ostringstream中的任何数据调用它。
什么是将任何内容发送到std :: clog并重定向到我选择的方法的最简单/最佳方法?像上面那样填写while!eof部分(不确定如何),还是有更好的方法,例如在某个地方重载一些“提交”运算符以调用我的方法?我正在寻找快速简便的方法,我真的不想开始使用boost iostreams定义接收器之类的东西-这些东西超出了我的能力范围。

你能否更清楚地表达你的问题? - Evan Teran
5个回答

12

我鼓励你查看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;
}

你的代码片段在一个空项目中可以工作。我将你的代码复制到我的实际项目中,并将你的主函数复制到某个初始化方法中,什么都没有改变,但是我却得到了几页的boost错误信息。 - Dustin Getz
(在这行代码上:bio::stream_buffer<MySink> sb;) - Dustin Getz
我很乐意提供帮助,但需要更多关于您所见错误的信息。在这里,评论的严格大小限制并不实用,因此建议您在Boost用户列表上发布您的代码和生成的错误。http://www.boost.org/community/groups.html#users - Éric Malenfant

8

我认为您想在ostream不为空时提取文本。您可以像这样操作:

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

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

当然,更好的解决方案是去除中间人,并使新的streambuf到达您真正想要的地方,无需以后再进行探测。像这样的东西(注意,这对每个字符都做了,但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;

}


我没想到它会这么简单,但我会试一试。 - Dustin Getz

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;
};

然后使用它:
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;

1
通过将std::string构造移动到isInfoEnabled检查内部,可以提高一些性能。 - Rhys Ulerich
这个很好用!我只是改了intercept_stream,使其接受std::string而不是char const*,因为getLogger需要一个字符串,并根据Rhys Ulerich的建议将字符串构造移动到isInfoEnabled检查内部。 - Fred

1

对于log4cxx示例,您必须覆盖overflow()和sync(),否则在接收第一个流后,badbit始终被设置。

请参见: http://groups.google.com/group/comp.lang.c++.moderated/browse_thread/thread/fd9d973282e0a402/a872eaedb142debc

InterceptStream::int_type InterceptStream::overflow(int_type c)
{
    if(!traits_type::eq_int_type(c, traits_type::eof()))
    {
        char_type const t = traits_type::to_char_type(c);
        this->xsputn(&t, 1);
    }
    return !traits_type::eof();
}

int InterceptStream::sync()
{
    return 0;
}

0
如果您只想获取ostringstream的内容,则可以使用它的str()成员。例如:
string s = oss.str();    

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接