Logging with Breakpoints
Say at a certain line in your code, you want to log some statements. You do so by first adding a breakpoint by clicking on the vertical strip of space on the left side of your source file. Then you right click on your breakpoint and select ‘edit break point’.
For option Action, select Log Message and type in some message you would like to appear. For example, the name of the file would be a good choice. Also, the name of the method you’re in is also another great choice. You do so by using %B.
You can also log custom variables by putting variables in between %%
Now, execution will stop at your breakpoint and print out your log messages
However, say you want the execution to keep going. You’d check the “Automatically continue…” checkbox so that you get the logging and all that, but execution continues.
Now it just uses your breakpoint to do whatever Action you intended and continues on to the next one.
Voice Logging
Next to Action, there is a + and – button. Use the + button to add more actions. Select Log Message and there is a Speak Message option. Select it and it will voice your logs.
Debugger Command
Add a breakpoint on a line right beneath a variable say:
1 2 3 |
int i, j=3; DataStore *dataStore = [DataStore sharedDataStore]; //add the breakpoint here |
Control click or right click the breakpoint, click ‘Edit Breakpoint’, then select select “Debugger Command” from the dropdown. In the text field type the follow:
1 |
po yourVariableName |
po is a debugger command that will print out the contents of an object. You can keep adding Actions >> Debugger Command and po-ing variables.
Logging with Date
Action >> Debugging command
1 |
expr (void)NSLog(@"dataStore: %@", dataStore) |
The expr command will evaluate an expression in real time. The expression command needs to know the actual type of the returning value, so a cast is necessary. Since there is no return type for NSLog, the return type is cast to a void value. Build and run.
You should now see the following:
2012-12-20 08:57:39.942 GiftLister[1984:11603] dataStore:
Being able to add NSLog messages via. breakpoints means you no longer have to stop the program just to log important data, there’s no chance of introducing new bugs because you are not touching the code, but best of all, there’s no last minute scrambles to remove all your debug statements the night before release.
Disabling breakpoints before a release
Say you’re ready to turn in a release and you want to disable all the breakpoint logging. Simply go to your navigator and press the debug section. Then select ‘Disable breakpoints’ and that’s it. Now your code will run without all the breakpoint logging.