我想在我的程序中包含一些 std::ostringstream 对象,用于日志记录和错误报告目的。根据编译时给出的设置,日志和错误流将收集它们各自的数据(要保存或其他),或者它们将被重定向到 std::coutstd::cerr(用于调试目的)。据我所看到的 references (和 here ),std::ostringstream 对象继承了 rdbuf() 方法,我应该能够在流对象上调用 rdbuf() 方法,将指向 std::streambuf 的指针作为参数传递给关联与溪流。

但是,当我尝试编译时,我收到一个没有匹配函数调用的错误,rdbuf() 方法没有列出任何参数作为候选。

我从 g++ 得到的错误(Ubuntu 12.04 x86_64 上的 4.6.3):

sigma@SigmaSys:~/Scripts/http/test$ g++ test.cc -c -o test.o
test.cc: In constructor ‘Logger::Logger()’:
test.cc:23:43: error: no matching function for call to ‘std::basic_ostringstream::rdbuf(std::basic_streambuf*)’
test.cc:23:43: note: candidate is:
/usr/include/c++/4.6/sstream:449:7: note: std::basic_ostringstream::__stringbuf_type* std::basic_ostringstream::rdbuf() const [with _CharT = char, _Traits = std::char_traits, _Alloc = std::allocator, std::basic_ostringstream::__stringbuf_type = std::basic_stringbuf]
/usr/include/c++/4.6/sstream:449:7: note:   candidate expects 0 arguments, 1 provided

Here is a stripped down version of the source and header file:

http_utils.h

#include <string>
#include <sstream>
#include <streambuf>

#define HTTP_DEBUG_MODE 1


class Logger {
    private:
        static std::ostringstream errOut;
        static std::ostringstream logOut;

        static std::streambuf* cerrStreamBuffBackup;
        static std::streambuf* coutStreamBuffBackup;

        static std::streambuf* errOutStreamBuffBackup;
        static std::streambuf* logOutStreamBuffBackup;
    public:
        Logger();
        ~Logger();
};

http_utils.cc

#include "test.h"
#include <string>
#include <iostream>
#include <sstream>
#include <streambuf>


std::ostringstream Logger::errOut;
std::ostringstream Logger::logOut;
std::streambuf* Logger::cerrStreamBuffBackup = NULL;
std::streambuf* Logger::coutStreamBuffBackup = NULL;
std::streambuf* Logger::errOutStreamBuffBackup = NULL;
std::streambuf* Logger::logOutStreamBuffBackup = NULL;


Logger::Logger() {
    if (HTTP_DEBUG_MODE) {//Redirect logging/error to stdout/stderr
        Logger::errOutStreamBuffBackup = Logger::errOut.rdbuf();
        Logger::logOutStreamBuffBackup = Logger::logOut.rdbuf();

        Logger::errOut.rdbuf( std::cerr.rdbuf() );
        Logger::logOut.rdbuf( std::cout.rdbuf() );
    } else {//Redirect stdout/stderr to custom logging/error functionality
        cerrStreamBuffBackup = std::cerr.rdbuf();
        coutStreamBuffBackup = std::cout.rdbuf();

        std::cerr.rdbuf( errOut.rdbuf() );
        std::cout.rdbuf( logOut.rdbuf() );
    }
}
Logger::~Logger() {
    if (HTTP_DEBUG_MODE) {

    } else {//Restore stdout/stderr streambuf
        std::cerr.rdbuf( Logger::cerrStreamBuffBackup );
        std::cout.rdbuf( Logger::coutStreamBuffBackup );
    }
}

如果有人可以帮我解决这个问题,或者根据我正在尝试做的事情提出替代方法,我将不胜感激。

最佳答案



你应该在 Liskov substitution principle 方面,但你不能。

std::basic_stringstream::rdbuf 隐藏了 std::basic_ios::rdbuf() 的两个签名。你可以用一些丑陋的 Actor 来称呼他们,但我认为这不会有任何影响。字符串流始终保证写入字符串缓冲区。

关于c++ - 无法设置 ostringstream 对象的流缓冲,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15721800/

10-11 22:06
查看更多