Wednesday, February 14, 2007

Optional breakpoints

I sometimes find myself wanting to break into the debugger only after a series of steps that would otherwise trigger the breakpoint. I find the following code snippet useful in some of these situations.

BOOL optionKeyIsDown = (([[NSApp currentEvent] modifierFlags] & NSAlternateKeyMask) != 0);
if (optionKeyIsDown) {
NSLog(@"voila");
}

You set a breakpoint on the NSLog() which it's only triggered when you have the option key down.

It's easy to use another modifier key if you like by using another key mask constant instead of NSAlternateKeyMask.

The downside of course is that it requires a little piece of live code that needs to be cleaned up before ship time.

This same approach can useful for other things as well. For example, you could trigger a data structure verification routine. Maybe that routine is very time consuming and you don't want it interfering with normal execution, but by simply holding down the option key (or may multiple modifier keys) you can trigger some special code.

No comments: