Simple wostream logging class (with custom stream manipulators)

时光怂恿深爱的人放手 提交于 2019-12-10 18:07:56

问题


I've been reading tons of questions, articles, and documentation, but I've not found a solution to my problem.

I'd like to create a simple class for use in debugging. The end result of which would allow me to do something like this:

logger << error << L"This is a problem!" << endl;
logger << warning << L"This might be a problem!" << endl;
logger << info << L"This isn't a problem but I thought you should know about it" << endl;

With the idea that within the logger class I can toggle whether or not these things make it to the console/debug file.

logger.setLevel(ERROR);

I've got a skeleton together but I can't get the operator overloading for the manipulators to work.

Here's Logger.h:

class LoggerBuffer : public wfilebuf {
// Functions
    public:
        LoggerBuffer() { wfilebuf::open("NUL", ios::out); currentState = 1;}
        ~LoggerBuffer() {wcout << "DELETED!" << endl;}
        void open(const char fname[]);
        void close() {wfilebuf::close();} 
        virtual int sync();
        void setState(int newState);
// Variables
    private:
         int currentState;
};

class LoggerStream : public wostream {
// Functions
    public:
         LoggerStream() : wostream(new LoggerBuffer()), wios(0) {}
         ~LoggerStream() { delete rdbuf(); }
         void open(const char fname[] = 0) { 
    wcout << "Stream Opening " << fname << endl;((LoggerBuffer*)rdbuf())->open(fname); }
         void close() { ((LoggerBuffer*)rdbuf())->close(); }
         void setState(int newState);
};

And Logger.cpp:

void LoggerBuffer::open(const char fname[]) {
    wcout << "Buffer Opening " << fname << endl;
    close();
    wfilebuf* temp = wfilebuf::open(fname, ios::out);
    wcout << "Temp: " << temp << endl;
}
int LoggerBuffer::sync() {
    wcout << "Current State: " << currentState << ", Data: " << pbase();
    return wfilebuf::sync();
}
void LoggerBuffer::setState(int newState) {
    wcout << "New buffer state = " << newState << endl;
    currentState = newState;
}

void LoggerStream::setState(int newState) {
    wcout << "New stream state = " << newState << endl;
    ((LoggerBuffer*)rdbuf())->setState(newState);
}

And main.cpp:

struct doSetState {
    int _l;    
    doSetState ( int l ): _l ( l ) {}

    friend LoggerStream& operator<< (LoggerStream& os, doSetState fb ) {
        os.setState(3);
        return (os);
    }
};

...
LoggerStream test;
test.open("debug.txt");
test << "Setting state!" << doSetState(1) << endl;
...

This mess produces the following error in VS2005:

"error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'doSetState' (or there is no acceptable conversion)"

Any help is GREATLY appreciated.

Thanks!


回答1:


Your ostream operator does not have the correct signature. It should be:

friend LoggerStream& operator<< (LoggerStream& os, const doSetState& fb )

(It is necessary to use a reference, because a single-pass compiler does not know the size of doSetState when it is midway through the class definition.)




回答2:


The problem is that when you do this:

test << "Setting state!"

It returns a basic wostream object. So chaining it doesn't work, since there is no overload for:

wostream& operator<< (wostream& os, const doSetState& fb )

You can however do it on separate lines, like this:

test << "Setting state!";
test << doSetState(1) << endl;



回答3:


I would go for a slightly different approach.

Instead of inheriting from std::wostream, I would have a std::wfostream member in my logger class. Then you can have a generic templated operator<< that selectively forwards to the embedded stream.

For example:

class Logger;

template<class T> Logger& operator<<(Logger&, const T&);

enum LogLevel
{
    debug,
    info,
    warning,
    error
};

class Logger
{
public:
    void open(const char* file) { stream.open(file); }
    void close() { stream.close(); }
    void passLevel(Loglevel level) { pass = level; }
    void logLevel(LogLevel level) { current = level; }
private:
    bool passThrough() { return current >= pass; }

    std::wofstream stream;
    LogLevel pass;
    LogLevel current;

    friend template<class T> Logger& operator<<(Logger&, const T&);
};

template<class T> 
Logger& operator<<(Logger& log, const T& rhs)
{
    if (log.passthrough())
    {
        log.stream << rhs; 
    }
    return log;
}

Logger& operator<<(Logger&, LogLevel level)
{
    log.logLevel(level);
    return log;
}

struct setLogLevel {
    setLogLevel(LogLevel l) : level(l) { }
    LogLevel level;
};

Logger& operator<<(Logger&, const setLogLevel setter)
{
    log.passLevel(setter.level);
    return log;
}


来源:https://stackoverflow.com/questions/4162135/simple-wostream-logging-class-with-custom-stream-manipulators

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!