问题
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:
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"));
回答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;
}
回答3:
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.
回答4:
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.
回答5:
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.
回答6:
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
来源:https://stackoverflow.com/questions/8742270/how-to-remove-all-debug-printf-statements-from-c-code