I have a workaround to a problem that really feels like a kludge to me and I'd appreciate any recommendations on how it might be rendered more "conceptually clean".
I implemented a class exception handler, but it ouputs to two different streams. One goes out to stderr and just tells the user that an exception has occurred, and another goes out to a file handle with a very specific format (actually .dot format for graphviz) so that the user can later turn the error output into a postscript document.
I thought of creating a separate error file for everytime my program throws an exception, but in the end decided to dump it all into one file. So here's how I implemented it in the header:
Code:
class A {
static ofstream* os;
void close();
};
Then in the source code:
Code:
static ofstream* A::os = new ofstream(XXX);
A::close()
{
os->close();
delete os;
}
So the program repeatedly instantiates A, and when there's an exception, it just dumps things into "os".
Then, when main terminates, you have to instantiate a class, call close on it, and exit main. Obviously, this last step isn't necessary since if main terminates, everything's going to be wiped clean, but just to maintain best practices.
Or maybe you could just
Code:
int main() {
A::os->close();
delete A::os;
}
Haven't tried it. But maybe it'll work.
All of this seems very ugly to me. So any suggestions for improvement are welcome.