Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call a macro every time any method is called - Objective C

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.

like image 929
Jacob Relkin Avatar asked May 06 '10 17:05

Jacob Relkin


1 Answers

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.

like image 55
Jay O'Conor Avatar answered Sep 22 '22 15:09

Jay O'Conor