Debug logger or data logger strategy
hi,
i was trying to develop a good way to provide my program a simple yet flexible data logger (basically records debbug messages to a text file). Flexible meaning i can set what kinds of debug messages i want recorded at run time rather than having to keep going back to the code to comment out or add in debug messages.
So what i do is something like this:
//I create an array of BOOL''s
BOOL debug_flag[100];
//intialize them all to FALSE
//then for printing my debug messages i create a simple function
debug_log ("my message", 10);
//the debug_log function would be something like:
BOOL debug_log (char* log_message, int debug_id)
{
if (debug_flag[debug_id] == TRUE)
{
fprintf (logfile, "%s",log_message);
fflush (logfile); //print to file immediately
return (TRUE); //successful
}
return (FALSE); //no debug message recorded
}
-------------------------------------------------------
So now if i set debug_flag[10] = TRUE i will only get all those debug messages i called with the value 10.
This means however the debug_flag[] is global in order to use it throughout my code.
How do you all do your debug messages? Is the way i am doing it useful or is it just a waste of time? Is there better ways for doing this?
I only use debug messages when there is a problem and afterwards when I've fixed the problem, I remove them. Sooner or later you will end up with so much code, that you'll be spending lots of time writing debug messages (waste of time).
you could easilly encapsulate the global debug_flag data by doing:
if you didn't want certain "levels" to be enabled/disabled I suggest you do:
you could also do this the c++ way with a singleton or some static member functions with some static member data.
[edited by - doho on June 19, 2003 4:53:32 PM]
you could easilly encapsulate the global debug_flag data by doing:
// -- header file --void set_debug_log_level(int level);void debug_log(const char *message, int level);// -- c file --static bool s_enabled[10]; // 100 seems a little bigset_debug_log_level(int level){ s_enabled[level] = true;}void debug_log(const char *message, int level){ if(s_enabled[level]) { print_debug_message_somehow(); }}
if you didn't want certain "levels" to be enabled/disabled I suggest you do:
// -- c file --static int s_level;set_debug_log_level(int level){ s_level = level;}void debug_log(const char *message, int level){ if(level <= s_level) { print_debug_message_somehow(); }}
you could also do this the c++ way with a singleton or some static member functions with some static member data.
[edited by - doho on June 19, 2003 4:53:32 PM]
Have a look at part two of the Enginuity article. It demonstrates a good logging system.
Also, you may want to think about using #define and #ifdef to enable/disable certain error messages.
[edited by - Plasmadog on June 19, 2003 9:19:45 PM]
Also, you may want to think about using #define and #ifdef to enable/disable certain error messages.
[edited by - Plasmadog on June 19, 2003 9:19:45 PM]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement