通过一个方法的所有参数到的NSLog [英] Pass all arguments of a method into NSLog

查看:360
本文介绍了通过一个方法的所有参数到的NSLog的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我打印出来的东西使用控制台的NSLog()。有没有一种方法来传递所有的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?

例如,我已经有了的时候我只是把 LOGME 在我的code,打印有用的信息给控制台的宏。宏将创建一个调用单个类的测井方法,并传递一些有用的东西像 _cmd 和其他东西。

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?

推荐答案

使用gdb的。您可以设置记录的参数的方法,并继续一个破发点。

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

如果你坚持这样做了艰辛的道路......这当然不是简单的,但你可以使用一个给定的架​​构/ ABI堆栈结构和使用Objective-C的运行时间找出多少个参数和找什么尺寸。从现在开始,我在难以估计的领土;我从来没有这样做,也没有我会永远打扰。因此,情况因人而异...

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);
}

随着你必须从参数类型字符串转换的方式(使用的Objective-C型编码)堆栈上的每个参数的大小。

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.

当然,那么你就必须得到每个类型的格式字符串,并调用 NSLogv 含参数的适当的变量参数数组,从他们的位置复制堆栈。超过其价值可能很多更多的工作。使用调试器。

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.

这篇关于通过一个方法的所有参数到的NSLog的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆