在 std::cout 刷新事件上捕获和引发事件

Capturing and raising events on std::cout flush events

我正在尝试编写一个作用域对象来重定向 std::cout 的输出并在它的底层缓冲区被刷新时调用一个函数。

我的实现大量借鉴了以下 SO 答案:

c++ execute function any time a stream is written to

我让它部分工作,但只有当我在 std::cout 上明确调用 flush 时才会调用回调函数。但是,我希望它在将 anything 写入流时调用回调函数。

注意:我正在编译 MSVC++11。

struct stream_redirect
{
    stream_redirect(std::ostream& stream, std::streambuf* rdbuf) :
        stream(stream),
        rdbuf_old(stream.rdbuf())
    {
        stream.set_rdbuf(rdbuf);
    }

    ~stream_redirect()
    {
        stream.set_rdbuf(rdbuf_old);
    }

private:
    stream_redirect(const stream_redirect&) = delete;
    stream_redirect& operator = (const stream_redirect&) = delete;

    std::ostream& stream;
    std::streambuf* rdbuf_old;
};

struct functionbuf : public std::streambuf
{
    typedef std::function<void(std::string)> function_type;

    functionbuf(const function_type& function)
        : function(function)
    {
        setp(buffer, buffer + sizeof(buffer) - 1);
    }

private:
    char buffer[1024];
    function_type function;

    virtual int_type overflow(int_type c) override
    {
        if (!traits_type::eq_int_type(c, traits_type::eof()))
        {
            *this->pptr() = traits_type::to_char_type(c);
            pbump(1);
        }

        return sync() ? traits_type::not_eof(c) : traits_type::eof();
    }

    virtual int_type sync() override
    {
        if (pbase() != pptr())
        {
            function(std::string(pbase(), pptr()));

            setp(pbase(), epptr());
        }

        return 0;
    }
};

struct ofunctionstream :
    private virtual functionbuf,
    public std::ostream
{
    ofunctionstream(const function_type& function) :
        functionbuf(function),
        std::ostream(static_cast<std::streambuf*>(this))
    {
        setf(std::ios_base::unitbuf);
    }
};

现在举个例子:

void callback(std::string string)
{
    printf("callback(%s)\n", string.c_str());
}

int main()
{
    ofunctionstream fs(&callback);
    stream_redirect sr(std::cout, fs.rdbuf());

    printf("sending string to cout...");
    std::cout << "hello!";
    printf("string sent to cout");

    //this is necessary to 
    printf("flushing cout...");
    std::cout.flush();
    printf("cout flushed");
}

我得到以下输出:

sending string to cout...
string sent to cout
flushing cout...
callback(hello!)
cout flushed

同样,我希望在调用 std::cout << "hello!"; 时立即调用该回调函数。我认为这会发生,因为我在其构造函数中的 ofunctionstream 对象上调用 setf(std::ios_base::unitbuf) (http://en.cppreference.com/w/cpp/io/manip/unitbuf)。

非常感谢任何帮助!

如果您检查您正在使用的回调是如何工作的,它通过子类化 std::streambuf 和覆盖 overflow() 来工作。这点很重要。

引用C++标准库的相关部分:

27.6.3.2.5 Put area [streambuf.pub.put]

int_type sputc(char_type c);

Returns: If the output sequence write position is not available, returns overflow(traits::to_int_- type(c)). Otherwise, stores c at the next pointer for the output sequence, increments the pointer, and returns traits::to_int_type(c)

std::ostream、a.k.a。格式化输出,使用 sputc() 写入流缓冲区。因此,唯一一次调用 overflow() 是在输出缓冲区耗尽时。或者明确地通过 std::flush.

因此,不幸的是,您的选择有些有限。要么处理当前行为,要么临时设置 std::streambuf 子类根本没有写入缓冲区,这样 每个 最终通过 [=16= 写入的字符] 将被踢到 overflow(),并调用您的子类实现。

不幸的是,流操作不会在每次格式化操作后执行任何显式操作,这可以被 std::streambuf 拦截。它们只是通过 sputc() 写入格式化的输出,一次一个字符,以便输出被收集到流缓冲区的写缓冲区中,只有当它已满或 std::flush 被显式获取时才会被刷新用过。

所以,这就是它的工作原理。