Thread: displaying debug information

  1. #1
    UK2
    Join Date
    Sep 2003
    Posts
    112

    displaying debug information

    Hello,

    I have a program that displays output to the user.

    However, I would also like to display other information as well, that could be used for debugging purposes.

    example:
    Code:
    printf("Library has been opened successfully\n"); /* display always */
    printf("Library handle: 0x%\n", hLibrary); /* only displayed when running in debug mode */
    The code above represents some of my code. The user isn't interested to know what the resource value for the library. So I don't want to display this. However, sometime I would like to run my program and see what the handle value is.

    Is there something that I can do that will run my application. It seems like a waste of time to keep commenting out this line to run the application for the user. And then to uncomment it to see what the value is for debugging purposes.

    Many thanks,

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    #ifdef _DEBUG

    could help
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    278
    Two options...
    1. Assuming you have several places throughout your program with debugging code in it, this is what I do. I #define DEBUG 1 at the very top of the .c file (or .h file). Then I put if (DEBUG) printf(....); throughout my code. Then I only have to change it to #define DEBUG 0 to turn off all the debugging statements. You still have to make a change in the code, but it's much easier to change one thing.

    2. I assume when you say "Is there something that I can do that will run my application." You want someway while the program is running to be able to turn on/off the debugging information. All you have to do is setup a variable like I described the constant above and then just poll it from the user in some way.

    I think option 1 is the best though

  4. #4
    UK2
    Join Date
    Sep 2003
    Posts
    112
    Hello,

    It was a misunderstanding about option 2. Forget I said that about running my application.

    I am going with option 1.

    However, I think I have got this wrong:

    Code:
    #define _DEBUG 1
    
    int main(void)
    {
    (_DEBUG) printf("=== This is debug information\n");
    
     printf("=== This is user information ===\n");
    
    (_DEBUG) printf("=== This is dubug information again ===\n");
    
    (_DEBUG) printf("=== more debug information, when will it end ===\n");
    
     printf("=== more information for the user to degust ===\n");
    .
    .
    .
    Error: Expected ; before printf

    Many thanks,

  5. #5
    UK2
    Join Date
    Sep 2003
    Posts
    112
    Sorry mistake

    I forgot the if statement

    #define _DEBUG 0

    int main(void)
    {
    if(_DEBUG) printf("=== This is debug information\n");

    printf("=== This is user information ===\n");

    if(_DEBUG) printf("=== This is dubug information again ===\n");

    if(_DEBUG) printf("=== more debug information, when will it end ===\n");

    printf("=== more information for the user to degust ===\n");

  6. #6
    Registered User
    Join Date
    Feb 2009
    Posts
    278
    Code:
    #define DEBUG 1 // Change to 0 to turn off debug code
    
    int main(void) {
      if (DEBUG) printf("yadda");
      printf("yadda");
      if (DEBUG) printf("yadda");
    }

  7. #7
    Registered User
    Join Date
    Feb 2009
    Posts
    278
    You got it

  8. #8
    UK2
    Join Date
    Sep 2003
    Posts
    112
    Hello,

    I was just researching to see if there is anything better. And came across debug macros, such as ASSERT, VERIFY, TRACE, and etc.

    How can these be used?

    Many thanks,

  9. #9
    Complete Beginner
    Join Date
    Feb 2009
    Posts
    312
    The problem with your current approach is that you are introducing lots of if-statements where you don't need them. Here's a better approach, based on the same principle:

    Code:
    #ifdef DEBUG
    #define DMSG(msg) \
    	{ printf("%s:%d: ", __FILE__, __LINE__); \
    	  printf msg; \
    	  puts(""); }
    #else
    #define DMSG(msg)
    #endif
    This introduces the macro DMSG which will print filename, line number and your debug message if DEBUG is defined, and will expand to the empty statement otherwise, thus not wasting ressources.

    Note that you will need double parantheses in order for this to work properly:

    Code:
    DMSG(("foo is %d", foo));
    will expand to "<source file>:<line number>: foo is <foo>\n".


    If you're using C99, you can also use:

    Code:
    #define DEBUG(...) \
    	{ printf("%s:%d:%s(): ", __FILE__, __LINE__, __func__); \
    	  printf(__VA_ARGS__); \
    	  puts(""); }
    This will also print the corresponding function name, and you don't need double parenthesis.

    Greets,
    Philip
    All things begin as source code.
    Source code begins with an empty file.
    -- Tao Te Chip

  10. #10
    Registered User
    Join Date
    Feb 2009
    Posts
    278
    I was operating under the assumption that the debug code would be removed once development is complete, which would mean the resources used by the extra if statements would be only during development.

    Also, in my personal opinion, especially for beginning programmers, it's much easier to use and understand simple if statements than it is to define and use macros efficiently.

    But I must admire your solution

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Bladactania
    I was operating under the assumption that the debug code would be removed once development is complete, which would mean the resources used by the extra if statements would be only during development.
    Yes, it is true that an optimising compiler is likely to just remove those if statements so there would be zero overhead.

    EDIT:
    I just remembered: the standard assert macro depends on whether NDEBUG is defined, and it would be good to use this... but it does not fit so well into your example as it does with Snafuist's example, since you are relying on boolean values whereas Snafuist relies on whether a name is defined, just like how assert works.
    Last edited by laserlight; 02-17-2009 at 11:43 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 10
    Last Post: 04-28-2008, 05:45 PM
  2. Debug --> Exceptions in Visual Studio 2005
    By George2 in forum C# Programming
    Replies: 1
    Last Post: 08-10-2007, 02:12 AM
  3. Results in Debug and Release mode are different
    By jaro in forum C Programming
    Replies: 11
    Last Post: 05-27-2006, 11:08 AM
  4. debug dll
    By axr0284 in forum C++ Programming
    Replies: 3
    Last Post: 03-06-2006, 04:52 PM
  5. Going out of scope
    By nickname_changed in forum C++ Programming
    Replies: 9
    Last Post: 10-12-2003, 06:27 PM