Logging NSExceptions
Posted by Anonymous on 2013-11-21 01:50
Hi Jason,
I added logging NSException objects to my copy of SNLog. Here's the two additional methods in SNLog.m:
+ (void) Log: (NSException*) exception format:(NSString*)format, ... {
SNLog *log = [SNLog logManager];
va_list args;
va_start(args, format);
NSString *msg = [[NSString alloc] initWithFormat:format arguments:args];
NSString * logEntry = [NSString stringWithFormat:@"%@ %@", msg, causedBy ( exception ) ];
[log writeToLogs: 1 :logEntry];
}
+ (void) Log: (NSInteger)logLevel cause:(NSException*) exception format:(NSString*)format, ... {
SNLog *log = [SNLog logManager];
va_list args;
va_start(args, format);
NSString *msg = [[NSString alloc] initWithFormat:format arguments:args];
NSString * logEntry = [NSString stringWithFormat:@"%@ %@", msg, causedBy ( exception ) ];
[log writeToLogs:logLevel :logEntry];
}
and the static utility method:
static NSString * causedBy ( NSException * oops )
{
NSMutableString * buffer = [NSMutableString string];
[buffer appendFormat:@"caused by %@ because %@\r\n", oops.name, oops.reason];
if ( oops.userInfo ) {
[buffer appendString:@"[UserInfo]\r\n"];
for ( NSString * key in oops.userInfo ) {
[buffer appendFormat:@"\t%@ = %@\r\n", key, oops.userInfo[key]];
}
}
[buffer appendString:@"[StackTrace]\n"];
for ( NSString * backtrace in oops.callStackSymbols ) {
[buffer appendFormat:@"\t%@\r\n", backtrace];
}
return buffer;
}
I added logging NSException objects to my copy of SNLog. Here's the two additional methods in SNLog.m:
+ (void) Log: (NSException*) exception format:(NSString*)format, ... {
SNLog *log = [SNLog logManager];
va_list args;
va_start(args, format);
NSString *msg = [[NSString alloc] initWithFormat:format arguments:args];
NSString * logEntry = [NSString stringWithFormat:@"%@ %@", msg, causedBy ( exception ) ];
[log writeToLogs: 1 :logEntry];
}
+ (void) Log: (NSInteger)logLevel cause:(NSException*) exception format:(NSString*)format, ... {
SNLog *log = [SNLog logManager];
va_list args;
va_start(args, format);
NSString *msg = [[NSString alloc] initWithFormat:format arguments:args];
NSString * logEntry = [NSString stringWithFormat:@"%@ %@", msg, causedBy ( exception ) ];
[log writeToLogs:logLevel :logEntry];
}
and the static utility method:
static NSString * causedBy ( NSException * oops )
{
NSMutableString * buffer = [NSMutableString string];
[buffer appendFormat:@"caused by %@ because %@\r\n", oops.name, oops.reason];
if ( oops.userInfo ) {
[buffer appendString:@"[UserInfo]\r\n"];
for ( NSString * key in oops.userInfo ) {
[buffer appendFormat:@"\t%@ = %@\r\n", key, oops.userInfo[key]];
}
}
[buffer appendString:@"[StackTrace]\n"];
for ( NSString * backtrace in oops.callStackSymbols ) {
[buffer appendFormat:@"\t%@\r\n", backtrace];
}
return buffer;
}
Home / Developer API / Tour / Get a Project - Solutions for Bug & Issue Tracking, Collaboration Tools, Subversion Hosting, Git Hosting
Snlog is powered by Assembla.
0 Comments