我一直在阅读大量的问题、文章和文档,但我还没有找到解决问题的方法。
我想创建一个用于调试的简单类。其最终结果将允许我做这样的事情:
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;
有了在记录器类中我可以切换这些东西是否进入控制台/调试文件的想法。
logger.setLevel(ERROR);
我有一个骨架,但我无法让操作员重载以使操纵器工作。
这是 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);
};
和 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);
}
和 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;
...
这种混乱在VS2005中产生以下错误:
“错误 C2679:二进制 '<<':未找到采用 'doSetState' 类型的右侧操作数的运算符(或没有可接受的转换)”
任何帮助是极大的赞赏。
谢谢!