I'm trying to write a simple but effective logger class. I got most of it working how I want so far, but I have a feeling adding the last step will require either heavily rewriting how it's done right now, or some strange way of doing it.
First, here's some of the relevant code (not in final state, but the way it's written and used will be the same). This is in the logger class:
enum log_type { info, warn, error }; // this enum is outside the class (global)
ofstream &operator <<(log_type obj) { // enumerated log-type is passed
switch(obj) {
case info:
flog << " <info> ";
break;
case warn:
flog << "<warning> ";
break;
case error:
quit = true;
flog << " <error> ";
break;
}
return flog;
}
template<class T>
ofstream &operator <<(T obj) { // something else is passed (not preceded by info, warn, etc)
flog << " <log> " << obj;
return flog;
}
And it's used like this:
App << info << "an info " << 1 << 2 << endl;
App << warn << "a warning " << 3 << 4 << endl;
App << error << "an error " << 5 << 6 << endl;
App << "alone" << endl;
App << 7 << " - preceded by int" << endl;
All of this works exactly how I want it. Now, the problem is, I want a MessageBox to show up with the logged message once endl is reached for the error case. That is, the line [ App << error << "an error " << 5 << 6 << endl; ] should show a MessageBox that will say "an error 56".
I can't think of a way to do it other than put everything being logged into a buffer, and then have a macro that will do the normal App << ... thing followed by calling something to MessageBox the stuff in the buffer and clear the buffer. Is there a way to do it while keeping the same usage format as I wrote above? It seems like I'm looking for a way to have the endl trigger a call to the MessageBox buffer thing I described. I guess if I knew how to implement something like the stream formatting system that iostreams uses instead of the enumerating way I'm doing, I'd know how to solve my problem.
[Edited by - Ivko on January 1, 2006 4:56:48 AM]