English 中文(简体)
Pass all arguments of a method into NSLog
原标题:

I print out something to the console using NSLog(). Is there a way to pass all the current method s arguments to NSLog(), 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 just put LOGME in my code. The macro will create a call to a singleton class s logging-method and pass several useful things like _cmd and other stuff.

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

最佳回答

Use gdb. You can set a break point that logs the arguments to the method and continues.

If you insist on doing it the hard way... It s certainly not simple, but you could use the stack structure on a given architecture/ABI and make use of the Objective-C runtime to figure out how many arguments and of what size to look for. From here on out, I m in unchartered territory; I ve never done this nor would I ever bother. So YMMV...

Within your method, you can get the Method struct, then the number of arguments, then each argument type and the its size. You could then walk the stack from the address of the self parameter (i.e. &self), assuming you knew what you were 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 d have to convert from the argtype string (using the Objective-C type encodings) to the size of each argument on the stack.

Of course then you d have to derive the format string for each type, and call NSLogv with an appropriate variable argument array containing the arguments, copied from their location on the stack. Probably a lot more work than its worth. Use the debugger.

问题回答

There’s no way to address all arguments in the preprocessor or Objective-C.

The only way I’m aware of is by use of the debugger. You can add a breakpoint action in Xcode to do this easily. Right click in the leftmost column of a code window, select "Built-in breakpoints" -> "Log breakpoint and arguments and auto-continue".





相关问题
c# reflection with dynamic class

I need to execute a method "FindAll" in my page. This method returns a list of the object. This is my method that I execute "FindAll". FindAll requires an int and returns an List of these class. ...

Performance overhead of using attributes in .NET

1.. Is there any performance overhead caused by the usage of attributes? Think for a class like: public class MyClass { int Count {get;set;} } where it has 10 attibutes (...

WPF GridView, Display GetType().Name in DisplayMemberBinding

I want include the Type name of each object in my collection from my GridView. I have a collection which has four different types in it that all derive from a base class. Call them, Foo, Bar, Fizz, ...

Testing private method of an abstract class using Reflection

How can I test a private method of an abstract class using reflection (using C#)? I am specifically interested in adapting the code found in this thread. I am aware of the discussion around the ...

Adding Items to ListBox, RadioList, Combobox using reflection

I m trying to add items to a listbox,combobox, radiolist using reflection. The code I have at the moment is as follows: public static Control ConfigureControl(Control control, ControlConfig ctrlconf)...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

热门标签