Where would you log your debug messages when you are designing an algorithm?
I tried various things. Used various libraries (apache’s log4cxx is very powerful) for logging and even used database (sqlite3 is my all time favorite for this) to store all data as well as debug messages. Problem with sqlite3 is that one has to type a lot but it is a very worthy investment if one needs to process a great deal of data later. Of course, one can write test-routines in the code itself and later disable them altogether once application is tested. This seems to be a standard practise. But many times one has to look at the data by oneself and in such cases logging is essential.
One must follow a standard format while writing logging messages. Readability is of great importance. What is the use of message which are hard to read. Prettyprinting requires work and often increases the size of code. But one has to do it, there is no escape. Apache logging framework format is pretty good. One can even write logs to XML file. Beware that XML is not easy to read by humans. Computers, on the other hand, can process xml easily. If logs are too big, I’d suggest go for either XML files or sqlite3 database. SOCI’s soci library is a good library in C++ for sqlite3.
For medium or small debugging, writing to simple text files or console is good enough. And one usually design his algorithm in steps. Therefore one can concentrate logs generated by a section of program. In C++, compiler macros are very useful. For example, if I am testing functionA, then I write its debug messages between #ifdef DEBUG_FUNA #endif and let the compiler know if I wants to activate or not.
I wish to suggest something more. If you are a big fan of terminal (no gui, no mouse, no problem) and have a look at ncurses library. It creates a text based user interface and I have used it in two applications. To cut the long story short, let me show you the screen-shot of how ncurses based screen can be split into many section in which different section can be used for different type of log messages.
Even this is lame. Let me show you a video. First few seconds to this video will show you the blindness caused by naive logging on the console. Then we’ll see the power of ncurses. You’ll see two matrices, we wish the left one must have rows which has only single 1. If this is achieved, our algorithm is correct.
- Processing XML files (dilawarnotes.wordpress.com)