Pass all method arguments to NSLog

I output something to the console using NSLog() . Is there a way to pass all the current arguments to the NSLog() method or any other function or method without looking at each of them explicitly?

For example, I already have a macro that prints useful information to the console when I simply put LOGME in my code. The macro will create a call to the singleton class logging method and pass a few useful things, such as _cmd and other materials.

I would also like to catch all the arguments of this method and pass them for printing automatically. Is it possible?

+3
source share
2 answers

Use gdb. You can set a breakpoint that writes arguments to the method and continues.

If you insist on doing it the hard way ... It's certainly not easy, but you can use the stack structure for a given architecture / ABI and use the Objective-C runtime to figure out how many arguments and what size to look for. From there I am in uninhabited territory; I have never done this, and I will never worry. So YMMV ...

Within your method, you can get the Method structure, then the number of arguments, then the type of each argument and its size. You can then go through the stack from the address of the self parameter (i.e. &self ), assuming you know what you are doing ...

 Method method = class_getInstanceMethod([self class], _cmd); unsigned nargs = method_getNumberOfArguments(method); void *start = &self; for(unsigned i = 0; i<nargs; i++) { char *argtype = method_copyArgumentType(method, i); //find arg size from argtype // walk stack given arg zie free(argtype); } 

Along the way, you will have to convert the argtype string (using the Objective-C encoding type ) to the size of each argument on the stack.

Of course, you will need to get a format string for each type and call NSLogv with a corresponding array of argument arguments containing the arguments copied from their location on the stack. Probably a lot more work than its value. Use a debugger.

+3
source

There is no way to address all arguments in a preprocessor or Objective-C.

The only way I know about is using a debugger. You can add a breakpoint action in Xcode to make this easy. Right-click in the far left column of the code window, select "Built-in breakpoints" → "Log breakpoint and arguments and continue automatically."

+3
source

All Articles