Tuesday, May 17, 2011

Multiline NSLog Output and Other Debugging Tips

While debugging mysterious memory management related crashes, I needed to write quite a lot of things into log. So much that log became unreadable.

Here's some tips:
  • Use text strings, which will catch your attention at right time. For example numbers for count ("11111"), arrows for direction (">>>>>" vs. "<<<<<") or just simple separators ("-----")
  • Use linebreaks
  • Use GCC compiler macros e.g. __FILE__, __LINE__, __PRETTY_FUNCTION__
  • Use "Command-K" keyboard shortcut to clear log screen (Xcode4)
  • Use "Command-Shift-Y" to open and close small log window (Xcode4)
  • Open several windows. Use one to debug and view log, use other(s) to read code (Xcode4)
  • Open Log Navigator with "Command-7". Select top item on list and make it full screen (Xcode4)
MPMoviePlayerController *mp = self.moviePlayer;
NSLog(@"---------- %s[%d] %s"
    @"\ncurrentPlaybackTime: %@ (%f)"
    @"\nduration: %@ (%f)"
    @"\nendPlaybackTime: %@"
    @"\ninitialPlaybackTime: %@"
    @"\nplayableDuration: %@",
    __FILE__,
    __LINE__,
    __PRETTY_FUNCTION__,
    [self magicText:mp.currentPlaybackTime],
    mp.currentPlaybackTime,
    [self magicText:mp.duration],
    mp.duration,
    [self magicText:mp.endPlaybackTime],
    [self magicText:mp.initialPlaybackTime],
    [self magicText:mp.playableDuration]);
...and this is what it would look like in log. Notice anything suspicious with "duration"? Not really, just trying to stream :) Therefore "playableDuration" does look suspicious!
2011-05-17 12:14:23.073 MyDemo[10540:40b] ---------- /Users/me/mydemo/Classes/myplayer.m[47] -[MyPlayer check]
currentPlaybackTime: 00:00 (0.000000)
duration: -2147483648:-2147483648:-2147483648 (nan)
endPlaybackTime: 00:00
initialPlaybackTime: 00:00
playableDuration: 00:00

No comments:

Post a Comment