Created
April 20, 2013 16:16
-
-
Save jpouellet/5426483 to your computer and use it in GitHub Desktop.
Fancy debug
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
/* | |
* depending on whether you use a named variadic macro or not you get either: | |
* fancy-debug.c:20:20: warning: ISO C does not permit named variadic macros | |
* or | |
* fancy-debug.c:28:13: warning: ISO C99 requires rest arguments to be used | |
* or sometimes both depending on your compiler. | |
* | |
* See also: | |
* http://gcc.gnu.org/onlinedocs/gcc-3.1/cpp/Standard-Predefined-Macros.html | |
* http://gcc.gnu.org/onlinedocs/cpp/Variadic-Macros.html | |
*/ | |
#include <stdio.h> | |
#include <stdarg.h> | |
#include <stdlib.h> | |
#include <string.h> | |
void | |
fancy_debug(const char *file, int line, const char *fmt, ...) | |
{ | |
va_list ap; | |
fprintf(stderr, "%s:%d: ", file, line); | |
va_start(ap, fmt); | |
vfprintf(stderr, fmt, ap); | |
va_end(ap); | |
fprintf(stderr, "\n"); | |
} | |
#define fdbg(str, ...) fancy_debug(__FILE__, __LINE__, str, ##__VA_ARGS__) | |
int | |
main() | |
{ | |
fancy_debug("test", 42, "a%s", "b"); | |
fancy_debug("test", 42, "what"); | |
fdbg("asdf%s", " -- !"); | |
fdbg("blah"); | |
return 0; | |
} | |
/* | |
* output is | |
* test:42: ab | |
* test:42: what | |
* fancy-debug.c:27: asdf -- ! | |
* fancy-debug.c:28: blah | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment