如何NSLog调用函数

发布于 2024-12-04 14:09:28 字数 382 浏览 4 评论 0原文

不是调用 NSLog 或 Dlog 的函数,而是调用该函数的函数。

我创建了一个类,

+(void) computeTime:(void (^)())block
{
    NSDate * currentTime = [NSDate date];
    block();
    DLog ("Time Running is: %f", [[NSDate date] timeIntervalSinceDate:currentTime);
}

因此每次有一个我想要测量其时间的操作时,我都会将其放在一个块中。

并执行[工具computeTime:^{//操作}];

但是,我想知道调用该computeTime 的函数。我该怎么做?

Not the function calling the NSLog or Dlog but function that call that function.

I created a class

+(void) computeTime:(void (^)())block
{
    NSDate * currentTime = [NSDate date];
    block();
    DLog ("Time Running is: %f", [[NSDate date] timeIntervalSinceDate:currentTime);
}

So everytime there is an operation whose time I want to measure I will put that in a block instead.

and do [Tools computeTime:^{//operation}];

However, I want to know the function that call that computeTime. How do I do so?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

执着的年纪 2024-12-11 14:09:28

两个选择:

第一个是滥用 +[NSThread callStackSymbols] 来获取调用堆栈中所有符号的数组,并取出您想要的符号。我怀疑这会相对缓慢。

第二种是使用宏。 C 预处理器提供了一个名为 __PRETTY_FUNCTION__ 的漂亮宏,其中包含格式良好的函数名称,并且它也适用于 Obj-C 方法。您可以使用类似 [ToolscomputeTimeWithName:__PRETTY_FUNCTION__ block:^{/*operation*/}][ToolscomputeTime:^{/*operation*/}] > 或者您可以将其全部包装在您自己的宏中,这样您就可以说 TOOLS_COMPUTE_TIME(^{/*operation*/})

#define TOOLS_COMPUTE_TIME(...) [Tools computeTimeWithName:__PRETTY_FUNCTION__ block:(__VA_ARGS__)]

注意,我使用了 varargs 风格的宏,因为C 预处理器不能很好地理解 obj-c 语法,因此块内的任何逗号都将被解释为宏的单独参数。如果我使用 TOOLS_COMPUTE_TIME(op) 定义它,那么编译器会抱怨该宏只接受 1 个参数,但给了多个参数。通过使用可变参数,编译器不关心您给它多少个参数,它会将它们全部传递给 __VA_ARGS__ 标记。

Two options:

The first is to abuse +[NSThread callStackSymbols] to get an array of all the symbols in the call stack and pull out the one you want. I suspect this is going to be relatively slow.

The second is to use a macro. The C preprocessor provides a nice macro called __PRETTY_FUNCTION__ which contains the name of the function all nicely formatted, and it works well for Obj-C methods as well. Instead of [Tools computeTime:^{/*operation*/}] you can either use something like [Tools computeTimeWithName:__PRETTY_FUNCTION__ block:^{/*operation*/}] or you could wrap it all up in your own macro so you can say TOOLS_COMPUTE_TIME(^{/*operation*/}):

#define TOOLS_COMPUTE_TIME(...) [Tools computeTimeWithName:__PRETTY_FUNCTION__ block:(__VA_ARGS__)]

Note, I've used a varargs-style macro because the C Preprocessor doesn't understand obj-c syntax very well, so any commas inside your block will be interpreted as separate arguments to the macro. If I defined it using TOOLS_COMPUTE_TIME(op) then the compiler would complain that the macro only takes 1 argument but it was given multiple. By using varargs the compiler doesn't care how many arguments you give it, and it will pass them all on to the __VA_ARGS__ token.

倾城泪 2024-12-11 14:09:28

对于搜索OP原始问题并添加Kevin的第一个使用调用堆栈的建议的人来说,另一个可能的答案。

如果您正在寻找所谓的函数(方法),请考虑以下事项:

 NSArray *callStack = [NSThread callStackSymbols];
 // Top of the stack. Current method
 NSLog(@"Current method: %@", [callStack objectAtIndex:0]);
 // Called by
 NSLog(@"called by: %@", [callStack objectAtIndex:1]);

查找的堆栈项可能位于 stackArray 的更深处。

希望这有助于更快地发现错误。

Another possible answer for someone searching for the OP's original question and adding to Kevin's first suggestion to use the call stack.

If you're looking for what called a function (method), consider the following:

 NSArray *callStack = [NSThread callStackSymbols];
 // Top of the stack. Current method
 NSLog(@"Current method: %@", [callStack objectAtIndex:0]);
 // Called by
 NSLog(@"called by: %@", [callStack objectAtIndex:1]);

It is possible that the stack item looked for is further into stackArray.

Hope this helps find a bug quicker.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文