I'd like to create a proxy to fprintf, like so:
void raise_exception(char *filename, int line, char *format_string, ...) {
fprintf(stderr, "Exception in `%s`:%d!\n", filename, line);
fprintf(stderr, format_string, ...);
exit(EXIT_FAILURE);
}
But what do I put in the place of the second ellipsis? Is it at all possible?
I'd like to simplify it even a bit more like so:
#define RAISE(...) raise_exception(__FILE__, __LINE__, ...)
But I don't think this would work either.
Any ideas? Thanks!
UPDATE
Straight from Wikipedia:
Variable-argument macros were introduced in the ISO/IEC 9899:1999 (C99)
So the define that would do it should look like so:
#define RAISE(...) raise_exception(__FILE__, __LINE__, __VA_ARGS__)
#include <stdarg.h>
void raise_exception(char *filename, int line, char *format_string, ...)
{
va_list args;
fprintf(stderr, "Exception in `%s`:%d!\n", filename, line);
va_start(args, format_string);
vfprintf(stderr, format_string, args);
va_end(args);
exit(EXIT_FAILURE);
}
Use vfprintf instead.
Please see this question:
Passing variable number of arguments around
Your exact example -- of wrapping printf -- is used as an example in the discussion here:
http://www.swig.org/Doc1.3/Varargs.html
Related
I'm looking for a function that print log information, in order to debug my program.
This function must look at a boolean and decide if write the log in a file or to the console.
int useFile;
int log_informations( ? )
{
if(useFile)
{
// open/close FILE *pf omitted
fprintf(pf, ? );
}
else
{
printf( ? );
}
return 0;
}
int main()
{
int answer = 42;
log_informations("The answer is %d.", answer);
return 0;
}
Can you help me with the parameter? I did not found any reference.
NB: In this question I made things clear and simpler as they were in my context, so I do not need workaround but, possibly, a simple answer.
Thank you in advance ;)
As other mentioned, you don't need to actually write a function. A macro could help here:
#define LOG(fmt, ...) fprintf(logToFile ? fileLogger : stdout, fmt, ##__VA_ARGS__)
To answer your question you can look at how printf and vprintf work:
int printf(const char *format, ...);
int vprintf(const char *format, va_list arg);
printf is a variadic function and you want to provide your own version that wraps vprintf and vfprintf.
#include <stdarg.h> // needed for va_list, va_start, and va_end.
int my_printf(const char *format, ...)
{
va_list ap;
int ret;
va_start(ap, format);
if (useFile) ret = vfprintf(pf, format, ap);
else ret = vprintf(format, ap);
va_end(ap);
return ret;
}
EDIT: as #phuclv mentioned, if you want to include file, line, and/or function information in your log a macro is the only way. For example, for appending file and line information you could do something like this:
#define LOG(fmt, ...) fprintf(logToFile ? fileLogger : stdout, __FILE__ ":" STRINGIFY(__LINE__) " " fmt, ##__VA_ARGS__)
#define STRINGIFY_HELPER(x) #x
#define STRINGIFY(x) STRINGIFY_HELPER(x)
You need STRINGIGY for __LINE__ because it is an int.
I have two macros:
#define LogFunction(str) fprintf(stdout, "%s: %s\n",__FUNCTION__,(str))
#define LogPrintf(f_, ...) fprintf(stdout, (f_), ##__VA_ARGS__)
So i can use them this way:
void MyFunction()
{
int N=4;
LogFunction("START"); // Outputs "MyFunction: START"
LogPrintf("N=%d\n", N); // Outputs "N=4"
}
What I would like to change is to
add FUNCTION at the start of the LogPrintf as it is in LogFunction
add "\n" at the end of the LogPrintf without having to remember to put it in myself
so in the end i could have just one macro for my outputs.
I've tried to understand if Appending to __VA_ARGS__ could've been useful, but i admit that i've not understood if it is related to my case :(
Thanks.
why not doing it in 3 steps?
#define LogPrintf(f_, ...) do { fprintf(stdout, "%s: ",__FUNCTION__); \
fprintf(stdout, (f_), ##__VA_ARGS__); \
fprintf(stdout,"\n"); } while(0)
this does 3 prints, but at least it's simple and does what you want. the do while(0) trick makes sure this is one only block (when using if without braces) and requires semicolon.
If you're willing to rely on the first argument to LogPrintf being a string literal, then you should be able to use string concatenation to achieve your objective:
// Assumes f_ always corresponds to a string literal:
#define LogPrintf(f_, ...) fprintf(stdout, "%s: " f_ "\n", __FUNCTION__, ##__VA_ARGS__)
Note, however, that in standard C, the LogPrintf macro requires at least two arguments, and the ## has no place. I keep it here only because you use it in your original code.
If you must accept format string expressions other than string literals, however, then your simplest alternative is to perform multiple I/O calls, as another answer also suggests:
#define LogPrintf(f_, ...) do { \
fprintf(stdout, "%s: ", __FUNCTION__); \
fprintf(stdout, (f_), ##__VA_ARGS__); \
fputc('\n', stdout); \
} while (0)
Note that in this case, the macro expands to a statement (sans trailing semicolon), whereas in the other, the macro expands to an expression. If you want the return value(s) of any of the I/O functions, then you'll have to make special provisions for that in this case.
If that doesn't work for you either, then the ultimate alternative is to write and use a helper function, as was suggested in comments:
#define LogPrintf(f_, ...) log_printf_impl(stdout, __FUNCTION__, (f_), ##__VA_ARGS__)
int log_printf_impl(FILE *f, const char *func, const char *fmt, ...) {
static const char prefix[] = "%s: ";
size_t flen = strlen(fmt);
va_list args;
int result = -1;
char *aug_fmt = malloc(sizeof(prefix) + strlen(fmt) + 1);
if (aug_fmt) {
va_start(args, fmt);
sprintf(aug_fmt, "%s%s\n", prefix, fmt);
result = vfprintf(f, aug_fmt, func, args);
va_end(args);
free(aug_fmt);
}
return result;
}
I'm trying to create a printf function that print like this
[INFO] whatever 123
va_args works but I don't know how to add a prefix. At least the following code won't do
#include <stdio.h>
#include <stdarg.h>
void myprintf (char *fmt, ...)
{
va_list argp;
va_start (argp, fmt);
vfprintf (stdout, "[INFO] " fmt, argp);
va_end (argp);
}
int main (int argc , char **argv)
{
myprintf ("arg count is %d\n", argc);
return 0;
}
Any ideas?
"[INFO] " fmt
This code won't work. The "string pasting" behavior you're trying to use here is a preprocessor behavior, not a C operator. It can only be used on string constants -- not variables.
The easiest way of getting the behavior you want here will be to simply call printf twice:
printf("[INFO] ");
va_start(argp, fmt);
vfprintf(stdout, fmt, argp);
va_end(argp);
More difficult approaches which you may want to consider include:
Define myprintf() as a macro instead of a function so that it can use string pasting on the format argument.
Copy "[INFO] " and fmt into a temporary buffer and use that as a formatting string.
Copy "[INFO] " into a temporary buffer, use vsnprintf() to append the output to the buffer, then output that.
I got stuck here...
#include <stdio.h>
#define DBG_LVL(lvl, stmt) \
do{ \
if(lvl>1) printf stmt; \
}while(0)
#define DBG_INFO(stmt) DBG_LVL(1, stmt)
#define DBG_ERROR(stmt) DBG_LVL(2, stmt)
int main()
{
DBG_INFO(("hello, %s!\n", "world"));
DBG_ERROR(("crazy, %s!\n", "world"));
return 0;
}
As you can see, the code above uses macros like "DBG_INFO" or "DBG_ERROR" to control debug information level.
Now for some reason, I have to replace DBG_LVL() with a new function.
void myprint(int lvl, const char * format, ...);
The only difference is the debug level is taken as its fisrt parameter.
I was thinking:
#define DBG_LVL(lvl, stmt) myprint(lvl, stmt)
Of course it failed, because the "stmt" expression includes parentheses around.
Then I googled around trying to find a way to strip the parentheses, seems there's nothing could help.
I also tried some tricks to pass parameters into "stmt", still failed... :(
Can you help me?
# define EXPAND_ARGS(...) __VA_ARGS__
# define DBG_LVL(lvl, stmt) myprint(lvl, EXPAND_ARGS stmt);
Don't write this as a macro.
Write instead an ordinary varargs function:
void DBG_LVL(int level, char *fmt, ...)
{
if (level < 1) return;
va_list args;
va_start(args, fmt);
vaprintf(fmt, args);
va_end(args);
}
For myprint(), define a similar vamyprint(int lvl, const char *format, va_list ap) as well, and forward the same way.
I have a variable argument function which prints error messages in my application, whose code is given below:
void error(char *format,...)
{ va_list args;
printf("Error: ");
va_start(args, format);
vfprintf(stderr, format, args);
va_end(args);
printf("\n");
abort();
}
This function is used in error conditions as follows:
error("invalid image width %d and image height %d in GIF file %s",wid,hei,name);
The error() function is called from different places with different arguments (variable argument function).
The function approach works fine.
Now, if I have to convert this function into a macro, how do I do it? I tried doing it as:
#define error(format) {va_list args;\
printf("Error: ");\
va_start(args, format);\
vfprintf(stderr, format, args);\
va_end(args);\
printf("\n"); abort()}
But this does not print the arguments correctly.
What is wrong in the macro definition above?
What is the fix?
If your compiler supports ISO style variadic macros, you can define a macro as such:
#define error(...) \
fprintf(stderr, "Error: "); \
fprintf(stderr, __VA_ARGS__); \
fprintf(stderr, "\n"); \
abort();
Alternatively, if you are using GCC, there's also the GNU style variadic macro used as such:
#define error(format, args...) \
fprintf(stderr, "Error: "); \
fprintf(stderr, format , ## args); \
fprintf(stderr, "\n"); \
abort();
For more information, see http://gcc.gnu.org/onlinedocs/cpp/Variadic-Macros.html
Update
If your compiler does not have variadic macro support, a (poor?) alternative would be to stick to the va_list in function approach. If you wish the definition to reside in a header file, then perhaps a static inline function?
static inline void error(const char *fmt, ...) {
#define PRINT_ERROR
va_list args;
fprintf(stderr, "Error: ");
va_start(args, fmt);
vfprintf(stderr, fmt, args);
va_end(args);
fprintf(stderr, "\n");
abort();
#endif
}
Here is an article with some examples on variable arguments used in a macro. It looks like it should do what you're looking for. You can use __VA_ARGS__ in your macro.
Which compiler are you using?
Macros do not (yet?) support variadic arguments and anyway, using va_list wouldn't work here, it only works on variadic function arguments.
Why do you want to replace the function (which works fine, according to you) with a macro anyway?
There's a common extension that does what you want, simply write:
#define error(args...) (fputs("Error: ",stdout),printf(args),puts(""))
C99 users can also say:
#define error(...) (fputs("Error: ",stdout),printf(__VA_ARGS__),puts(""))
but there are some problems with using __VA_ARGS__. Fortunately there's a GCC extension to deal with it, but then you're back to using a compiler-specific extension, and the args... mode is more widely available.
Many compilers support GNU style variadic macros, like this:
#define error(format,args...) do { \
fprintf(stderr, "error: " format "\n", ##args); \
abort(); \
} while(0)
However, if you aim for portability, do not use variadic macros.