0

How can I log every message sent in a single iteration of the Objective-C event loop?

I want to further my understanding of the Objective-C runtime and thought this would be a good start.

SundayMonday
  • 19,147
  • 29
  • 100
  • 154
  • I think I misread your question. You want a log of all the messages objects are sending to each other? – jscs Sep 02 '12 at 20:32
  • @W'rkncacnter I'd just like to see all the events processed in a single iteration of the runLoop (for the main thread for starters). I'm expecting to see something along the lines of "objectA sent this message to objectB, objectC was released...". – SundayMonday Sep 02 '12 at 20:38
  • 1
    At first I thought you just wanted to do something yourself every iteration. Okay, in that case your answer is already in one of these three questions: [Call a macro any time any method is called](http://stackoverflow.com/questions/2783223/), [Break on every method call](http://stackoverflow.com/questions/6961023/), [Track all method calls](http://stackoverflow.com/questions/7223555/). – jscs Sep 02 '12 at 20:40

2 Answers2

5

These functions will cause all messages to be logged to a file in /tmp, based on the PID of the process. Good on simulator, but not on an iDevice.

// Start logging all messages
instrumentObjcMessageSends(YES);

// Stop logging all messages
instrumentObjcMessageSends(NO);
Jody Hagins
  • 27,943
  • 6
  • 58
  • 87
2

The CFRunLoopObserver opaque type should do exactly what you want. It is

a general means to receive callbacks at different points within a running run loop.

Use the activity argument to its creation function to specify when you want your observer serviced. For your case, this will probably be either kCFRunLoopEntry or kCFRunLoopExit.

You can get the CFRunLoopRef from the current NSRunLoop, [[NSRunLoop currentRunLoop] getCFRunLoop], or by using CFRunLoopGetCurrent().

jscs
  • 63,694
  • 13
  • 151
  • 195