I read a lot of questions, articles and documentation, but I did not find a solution to my problem.
I would 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 in the logger class, I can toggle if these things get into the console / debug file.
logger.setLevel(ERROR);
I have a skeleton, but I can not get the operator to overload the manipulators to work.
Here is Logger.h:
class LoggerBuffer : public wfilebuf {
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);
private:
int currentState;
};
class LoggerStream : public wostream {
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:
"C2679: binary '<<: there is no operator that accepts the right operand of type' doSetState '(or not an acceptable conversion)
Any help POSSIBLE is much appreciated.
Thank!