Debug logger or data logger strategy

Started by
1 comment, last by moronmaster 20 years, 10 months ago
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?
Newbie
Advertisement
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:
// -- 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]
You are not the one beautiful and unique snowflake who, unlike the rest of us, doesn't have to go through the tedious and difficult process of science in order to establish the truth. You're as foolable as anyone else. And since you have taken no precautions to avoid fooling yourself, the self-evident fact that countless millions of humans before you have also fooled themselves leads me to the parsimonious belief that you have too.--Daniel Rutter

This topic is closed to new replies.

Advertisement