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

查看:21
本文介绍了每次调用任何方法时调用宏 - 目标 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天全站免登陆