When I do web development, I use a custom made logger that catches fatal errors and appends a trace to a file and displays a message to the user. I can occasionally glance to see if the file changed, which means, some user encountered an error and I can dig in to see what they encountered.
I'd like something similar on the iphone, with some caveats:
- While developing, it should be trivial to reset the list of errors or turn off notification.
- While developing, the error messages should also show up in some obvious place, like on the screen on in the console
- Once deployed, errors should politely be sent to the mothership for analysis (for a bug fix in the next update)
- Turn on Trace/Info logging when trying to track down a problem during development
- Turn off console logging for 'Release' to speed up things for the user
- Should clean-up after itself so as to be a good citizen on the phone
Some Related Links
- Using GSLog for instead of NSLog
- logging to a file on the iphone
- On the Mac, people say Apple System Logger and GTM Logger are the way to go objective-c logging best practices
- Jeff A's Blog entry on logging
It seem like there would be a common toolkit to do this - how do you handle this?
[Update Oct 2011] There have been some developments, of varying maturity...
- PLCrashReporter.
- Quincy sits on top of PLC.
- Bugsense commercial crash reporter.
- Crittercism crash and error reporting (some free packages, some paid).
- Test flight now has an SDK that catches crashes (but not yet for app store apps, just dev apps).
- Like Test Flight, Hockey aims to combine ad hoc distribution with crash reporting.
Here's what we do:
In more detail:
I've included example code below showing how we've written this, and what the output looks like.
We define multiple different trace levels so developers can identify which lines of trace are important, and can filter out lower level detail if they want to.
Example code:
Example trace output:
Our trace definitions:
Xcode settings:
In Xcode build settings, choose "Add User-Defined Setting" (by clicking on the little cog at the bottom left of the build configuration screen), then define a new setting called
GCC_PREPROCESSOR_DEFINITIONS
and give it the valueTRC_LEVEL=0
.The only subtlety is that Xcode doesn't know to do a clean build if you change this setting, so remember to manually do a Clean if you change it.
Do you know that CrashReporter for iPhone exists?
There is a repository on github which demos that code.
It has some cool features like maping the stack trace to your code and manages some git specific things like version hashes.
Apple automatically collects crash logs from users for you, and you can download them from iTunes connect.
If that's not enough for you, I'm not aware of a toolkit but I wouldn't want to roll something on my own, personally. It seems like too much effort to develop something robust, might raise privacy concerns, and in the end, with 100,000K apps in the app store, how many users would use your application again after discovering it was buggy?
I highly recommend Robbie Hanson's CocoaLumberJack: https://github.com/robbiehanson/CocoaLumberjack
It is very flexible and powerful maybe even a bit excessive if abused. Supports different levels of logging. Logging to files can be turned on with a couple of lines of code and even be sent over the network.