Logging all passed Arguments to a Function
I'm trying to write a macro (or something else) to log all the parameters passed to a function, but I'm really stuck here.
In other words I want something like below:
Code:
void test(int a, double b, char c)
{
__LOG_FUNCTION__
}
void main()
{
test(2,3,'c');
}
I need to write __LOG_FUNCTION__ in someway so the output would be something like:
Code:
Entering function test:
arguments: a = 2, b = 3, c='c'
is this possible in anyway?
Re: Logging all passed Arguments to a Function
Does the number or types of arguments change?
If they do, or your _LOG_FUNCTION_ will be generic, you should search for: "perfect forwarding", it's from c++0x, so not many compilers will have this implemented, in VS 2010, as far as i remember, you can do it up to 10 arguments.
Re: Logging all passed Arguments to a Function
yeah, actually he ideal case that everything about the function is unknown.
I've read about "perfect forwarding" but I can not understand what I can do with it here.
more help plz?
Re: Logging all passed Arguments to a Function
Well, do you really need something that generic? I mean, what are you going to log once the passed parameters are complicated objects like maps, that have no operator<< defined.
Also, is it that important that your macro is __Log_func__ and not something like __log_func(args).
I see two solutions, but I don't like the first one:
1
Code:
#define LOGGER(func_name, argType1, arg1, ...) \
cout << "entering " << func_name << etc
#define LOGGED_FUNC(func_name, return_type, argType1, arg1, ...) \
return_typ func_name(argType1 arg1, ... ) \
{
LOGGER(unc_name, argType1, arg1, ...)
LOGGED_FUNC(foobar, void, int, a, double, b)
//actual code
}
The second option: It requires some copy pasting of your arguments, but at least your code will look like something.
void foobar(int a, double b)
{
LOGGER(foobar, int, a, dopuble b)
//actual code
}[/CODE]
This is not exactly what you requested, but it should work. As for the variable amount of arguments. I'm sure copy pasta can solve your problem.