4

I want to remove all my debugging printf statements from my code .How can i do this without using conditional debugging by turning on/off debug statements?

Iam using printf just to check whether a particular value is coming wright or not ...like below... : printf("value read %d\n", a);

1
  • 11
    Is there anything that distinguishes debugging printf statements from "normal" printf statements? Commented Jan 5, 2012 at 12:11

6 Answers 6

19

No way you can do that, without removing them using your editor.

What you can do is:

Allen Holub recommends in his book to use the following debug macro:

#ifdef DEBUG # define D(x) x #else # define D(x) #endif 

and use it like this:

D(printf("oh, hai, debug")); 
Sign up to request clarification or add additional context in comments.

5 Comments

what is the advantage of using conditional macros for debug printing .....? Is this reduce executable size...pls tell ...
When DEBUG is not defined, everything in D(...) is dismissed (by preprocessor) even before compilation -- i.e. there is no overhead of these statements. And, yes, this reduces executable size.
hmm shouldn't the last line be D(printf("oh, hai, debug");) instead of D(printf("oh, hai, debug"));? (watch the semicolon position) currently, if debug is not defined, an unnecessary ";" will remain or am I wrong with that?
@stefan.at.wpf, yes you are right, this will give a bonus ";" if DEBUG is not defined.
@stefan.at.wpf The extra semi-colon does not matter as that is a no-op in C.
2

Replace all printf() calls for DEBUG(). DEBUG is a macro defined as:

#define DEBUG printf 

which will call the real printf() function, or you could also define it as:

#define DEBUG fake_printf 

which will then call a fake printf to suppress the debugging info.

The fake printf function could be as lame as:

int fake_printf( const char * format, ... ) { return 0; } 

4 Comments

I won't recommend this approach, at least with GCC. The compiler usually don't inline varargs call (but has some builtin knowledge thru __builtin_printf about printf) and even if it did, all the function calls inside the printf would be called, and you don't want that.
Interesting, thanks. I'm using VStudio right now and I suspect it behaves differently from GCC on this matter.
Are you sure that VStudio is inlining varargs? Do you have a reference? And even if it did, the function calls you pass to fake_printf (eg log or your own compute N-th digit of Pi) is called, so this is not really correct. You don't want the side-effect of computing arguments.
I understand what you are saying and I agree with you. I've considered deleting this answer but I'll leave it here because of your informative comments. Thanks again
2

You should explain how you do your debugging printf. A simple way could be to have a macro like

bool debug_flag; // to be set in the debugger or at initialization pthread_mutex_t debug_mutex = PTHREAD_MUTEX_INITIALIZER; #define debugprintf(Fmt,...) debugprintf_at(__FILE__,__LINE__,Fmt,__VA_ARGS__) #ifndef NDEBUG #define debugprintf_at(Fil,Lin,Fmt,...) do {if (debug_flag) { \ pthread_mutex_lock(&debug_mutex); \ fprintf (stderr, "%s:%d %s:" Fmt, Fil, Lin, __func__, \ ##__VA_ARGS__); \ pthread_mutex_unlock(&debug_mutex); } \ } while(0) #else #define debugprintf(Fmt,...) do {} while(0) #endif 

(it uses a mutex because you don't want to mix debug printf messages from different threads; if you don't use any thread, remove the mutex and its locking.)

If your question is how to find all debug printf in a huge software (having its source code), you could try with grep or with much fancier things like GCC plugins or MELT extension. But such an approach (GCC customization) takes time (week or more of your work) and is worth only for huge software base (eg million of lines of source).

For a not too big software, just examine manually all printf in your code and replace those you think are for debugging with a debugprintf macro invocation.

1 Comment

Upvoted since your debugprintf() macro effectively removes evaluation and storage of arguments when debugging is disabled. In other solutions posted, there's still an empty function call.
0

In visual studio, if the printf is on a single line, you could use Regex find/replace to remove printf statements. Ok it will be extremely laborious but may work.

Alternatively, write another console app to parse your code and do this for you (you could handle multiline statements then).

A good pattern to employ regarding debug logging would be to replace your printf statements throughout the code with a global function or macro and call that. For instance, if you had a global Log(const char* message) function (or macro) you could change that implementation once if you wanted to change how logging operated, rather than litter your code with preprocessor statements.

Going one step further than that, there are many logging frameworks (and here) which perform the same thing but with a bit more configurability.

3 Comments

Embedded applications are not written in Visual studio and mostly require clean, small and fast solutions
@friendzis , my comment about Visual Studio was not related to the code solution of using a macro or global function, but the IDE, which provides Regex find/replace. I believe notepad++ has the same feature.
@friendzis I sometimes use Visual Studio to write C/C++ and compile with AVR. See instructables.com/id/…
0

this is an approach I use:

#if !defined ( NDEBUG ) void PrintMsg( const char_t* pFormat, ... ) { va_list args; va_start( args, pFormat ); (void)vprintf( pFormat, args ); va_end( args ); } #else void PrintMsg( const char_t*, ... ) { } #endif #define DBG_PRINTF PrintMsg 

then substitute all the "printf" in your code to "DBG_PRINTF"

It will also execute the arguments of the printf function.

Comments

-1

Not sure if that is the question. I personally like having separate function/macro for debug info, which is enabled with preprocessor directives. If you want to remove debug output from production code... Can't think of any automated way

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.