Created
November 18, 2009 20:56
-
-
Save yukioc/238244 to your computer and use it in GitHub Desktop.
verbosely printf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* verbose_c : verbosely printf for old-c */ | |
#include <stdio.h> | |
#include <stdarg.h> | |
extern int verbose_lvl; | |
extern verbose(int lvl,const char*fmt,...); | |
int verbose_lvl=3; | |
int verbose(int lvl,const char*fmt,...){ | |
int ret=0; | |
va_list ap; | |
va_start(ap,fmt); | |
if(verbose_lvl>=lvl) ret=vprintf(fmt,ap); | |
va_end(ap); | |
return ret; | |
} | |
int main(int argc,char* argv[]){ | |
verbose_lvl=3; | |
verbose( 3,"This line is output. lvl=%d\n", verbose_lvl); | |
verbose( 5,"This line is not output.\n"); | |
return 0; | |
} | |
/* output: | |
gcc verbose_c.c && ./.aout | |
This line is output. lvl=3 | |
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* verbose_m.c : verbosely printf for c99 */ | |
#include <stdio.h> | |
extern int verbose_lvl; | |
#define verbose(err,...) do{if(verbose_lvl>=(err)){printf(__VA_ARGS__);}}while(0); | |
int verbose_lvl; | |
int main(int argc, char* argv[]) { | |
verbose_lvl = 3; | |
verbose( 3,"This line is output. lvl=%d\n", verbose_lvl); | |
verbose( 5,"This line is not output.\n"); | |
return 0; | |
} | |
/* output: | |
gcc verbose_m.c && ./.aout | |
This line is output. lvl=3 | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment