how to create a breakpoint's log message action in xcode?

12,701

Solution 1

Create a Breakpoint 'Log Message' action. For the log message include something like:

URL is @(char*) [[userDocumentsURL description] UTF8String]@

Alternatively you can create a breakpoint 'Debugger command' action similar to:

po [NSString stringWithFormat:@"URL is: %@", userDocumentsURL]

I prefer using breakpoint actions for logging, as it's arguably easier to clear out a bunch of breakpoints than it is to remove NSLogs. A possible downside to using breakpoints in this fashion is that they are significantly slower (during debugging) than a direct NSLog.

Solution 2

Here is a similar solution using NSLog, which might be fewer characters than the other solutions.

debugger command using NSlog

However, unless you add the void like this:

po (void)NSLog(@"the person name is: %@", p.name)

you will get an annoying "nil" printed out with your log. for example:

(lldb) po NSLog(@"foo")
 nil
2013-06-19 14:42:59.025 TheMove[95864:c07] foo

(lldb) po (void)NSLog(@"foo")
2013-06-19 14:43:10.758 TheMove[95864:c07] foo

If you can live with the nil (I can) it's faster to type and easier to remember just the po

Share:
12,701
PrimeSeventyThree
Author by

PrimeSeventyThree

Updated on June 05, 2022

Comments

  • PrimeSeventyThree
    PrimeSeventyThree almost 2 years

    Been watching a WWDC video today about new features in xCode 4. They have mentioned that it a good idea to use log message actions on breakpoints along with "automatically continue after evaluation actions" enabled to output a variable's value for instance instead of using NSLogs all the time.

    lets say I have something like that:

    NSLog(@"URL is : %@", userDocumentsURL);
    

    How would I write a log message action to display userDocumentsURL's value? Is it really a good idea to use the above method instead of NSLog?

  • mbm29414
    mbm29414 over 11 years
    I believe it does if you select the correct share options in the breakpoint editor. I don't use this feature myself (solo developer), but the WWDC talks indicate that sharing integrates with source control.
  • Rhubarb
    Rhubarb over 11 years
    I wish I knew why a: the debugger is so stupid as to require the char* cast, and b: it doesnt work half the time and c: the apple documentation doesnt make it clear you need the char* and d: why it doesnt complain if the variables dont exist to help you debug. Compare with Eclipse breakpoints for Java which don't even have this feature, so should be much poorer, but allow you to hack it in by putting a print in a condition. This is annoying of eclipse but even so is still much easier since the print requires no special casting and it will stop and tell you about errors
  • Matthias Bauch
    Matthias Bauch about 11 years
    In my opinion not to be forced to read all the debug output of other team members is a pro. You don't have to clean the NSLog (or the // before the NSLog) before committing. I worked on a team where 1 in 10 commits was "removed logging", "added more NSLog", "changed NSLog to be more/less verbose" or anything else related to logging.
  • Alex Cio
    Alex Cio about 9 years
    Like @MatthiasBauch just commented this might also be useful. And on the other hand, you should know, there are some usecases where you don't want to restart the app but would like to see a log in the debugger, so this would be the only possibility to do this.