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

后端 未结 4 1236
情深已故
情深已故 2020-12-06 07:46

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

相关标签:
4条回答
  • 2020-12-06 08:21

    I ended up with this code, after asking a similar question here How can I log names of each called class method in Objective-C?

    - (BOOL)respondsToSelector:(SEL)aSelector {
        if(aSelector){
            NSLog(@"%@", NSStringFromSelector(aSelector));
        }
        return [super respondsToSelector:aSelector];
    }
    
    0 讨论(0)
  • 2020-12-06 08:24

    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.

    0 讨论(0)
  • 2020-12-06 08:28

    sounds like you were looking for the __PRETTY_FUNCTION__ macro... but the accepted answer is probably a better way to go.

    0 讨论(0)
  • 2020-12-06 08:36

    Take a look at Dtrace. Instruments provides a GUI interface. You can, say, provide a regex to match all the methods you want to log.

    0 讨论(0)
提交回复
热议问题