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:
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...
An error log is a file that contains detailed records of error conditions a computer software encounters when it's running. The name is generic: sometimes, an application can log non-error type messages in its error log. However, error logs are generally meant to record only error messages generated by a program.
Go to Window > Devices and select your device from the list. Click the "up" triangle at the bottom left of the right hand panel. All logs from all apps on the device will be displayed here.
Error logs are useful in many respects. In the case of servers and office networks, error logs track issues faced by users and help in root causes analysis of those issues. A network or system administrator can resolve errors more quickly and easily with the information available from the error logs.
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:
- (void)myMethod:(NSObject *)xiObj { TRC_ENTRY; TRC_DBG(@"Boring low level stuff"); TRC_NRM(@"Higher level trace for more important info"); TRC_ALT(@"Really important trace, something bad is happening"); TRC_ERR(@"Error, this indicates a coding bug or unexpected condition"); TRC_EXIT; }
Example trace output:
2009-09-11 14:22:48.051 MyApp[3122:207] ENTRY:+[MyClass myMethod:] 2009-09-11 14:22:48.063 MyApp[3122:207] DEBUG:+[MyClass myMethod:]:Boring low level stuff 2009-09-11 14:22:48.063 MyApp[3122:207] NORMAL:+[MyClass myMethod:]:Higher level trace for more important info 2009-09-11 14:22:48.063 MyApp[3122:207] ALERT:+[MyClass myMethod:]:Really important trace, something bad is happening 2009-09-11 14:22:48.063 MyApp[3122:207] ERROR:+[MyClass myMethod:]:Error, this indicates a coding bug or unexpected condition 2009-09-11 14:22:48.073 MyApp[3122:207] EXIT:+[MyClass myMethod:]
Our trace definitions:
#ifndef TRC_LEVEL #if TARGET_IPHONE_SIMULATOR != 0 #define TRC_LEVEL 0 #else #define TRC_LEVEL 5 #endif #endif /*****************************************************************************/ /* Entry/exit trace macros */ /*****************************************************************************/ #if TRC_LEVEL == 0 #define TRC_ENTRY NSLog(@"ENTRY: %s:%d:", __PRETTY_FUNCTION__,__LINE__); #define TRC_EXIT NSLog(@"EXIT: %s:%d:", __PRETTY_FUNCTION__,__LINE__); #else #define TRC_ENTRY #define TRC_EXIT #endif /*****************************************************************************/ /* Debug trace macros */ /*****************************************************************************/ #if (TRC_LEVEL <= 1) #define TRC_DBG(A, ...) NSLog(@"DEBUG: %s:%d:%@", __PRETTY_FUNCTION__,__LINE__,[NSString stringWithFormat:A, ## __VA_ARGS__]); #else #define TRC_DBG(A, ...) #endif #if (TRC_LEVEL <= 2) #define TRC_NRM(A, ...) NSLog(@"NORMAL:%s:%d:%@", __PRETTY_FUNCTION__,__LINE__,[NSString stringWithFormat:A, ## __VA_ARGS__]); #else #define TRC_NRM(A, ...) #endif #if (TRC_LEVEL <= 3) #define TRC_ALT(A, ...) NSLog(@"ALERT: %s:%d:%@", __PRETTY_FUNCTION__,__LINE__,[NSString stringWithFormat:A, ## __VA_ARGS__]); #else #define TRC_ALT(A, ...) #endif #if (TRC_LEVEL <= 4) #define TRC_ERR(A, ...) NSLog(@"ERROR: %s:%d:%@", __PRETTY_FUNCTION__,__LINE__,[NSString stringWithFormat:A, ## __VA_ARGS__]); #else #define TRC_ERR(A, ...) #endif
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 value TRC_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.
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?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With