每次调用任何方法时都调用一个宏-目标C [英] Call a macro every time any method is called - Objective C

查看:69
本文介绍了每次调用任何方法时都调用一个宏-目标C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个调试宏,每当全局kDebug标志== YES时,该宏都会将传入的字符串打印到控制台.

每当调用任何方法时,我都需要打印出方法的名称及其类名.

当我不厌其烦地检查每个方法并将类名和方法写在字符串中时,这种方法就很好了.

在调用Objective-C中的任何方法时是否有任何特殊的处理程序被调用,如果是的话,有没有一种方法可以使我以某种方式覆盖它来调用我的调试宏?

这样做的全部目的是使我不必遍历代码中的每个方法,而在调试宏调用中手动编写方法签名.

解决方案

如果您正在寻找一种方法记录工具,而不是构建自己的方法,那么它实际上已内置在Objective-C运行时中.

您可能想阅读Apple Technote 2124的Objective-C部分: 技术说明TN2124:Mac OS X调试魔术

我还建议您阅读Dave Dribin的博客文章,内容涉及使用运行时的设施来跟踪消息.您可以在这里找到:跟踪Objective-C消息-Dave Dribin的博客. Dave记录了一个被调用的logObjCMessageSend函数,您可以使用该函数来自定义日志记录行为.使用起来有些棘手,但是Dave提供了成功使用它所需的信息.

I wrote a debug macro that prints to the console the passed-in string whenever the global kDebug flag == YES.

I need to print out the name of a method and its class name whenever any method is called.

That works fine when I painstakingly go through every method and write the name of the class and the method in a string.

Is there any special handler that gets called when any method in Objective-C is called, and if so, is there a way that I can somehow override it to call my debug macro?

The entire purpose of this is so that I don't have to go through every method in my code and hand-code the method signature in the debug macro call.

解决方案

If you're looking for a method logging facility, rather than build your own, one is actually built into the Objective-C runtime.

You might want to read over the Objective-C section of Apple's Technote 2124: Technical Note TN2124: Mac OS X Debugging Magic

I would also recommend reading over Dave Dribin's blog posting about using the runtime's facility for tracing messages. You can find that here: Tracing Objective-C messages - Dave Dribin's Blog. Dave notes a logObjCMessageSend function that gets called, which you could use to customize your logging behavior. It's a little tricky to use, but Dave gives the info necessary to successfully use it.

这篇关于每次调用任何方法时都调用一个宏-目标C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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