From 784f85ae9a475667b7afb726d0653701c196ed48 Mon Sep 17 00:00:00 2001 From: "gtm.daemon" Date: Thu, 28 Apr 2011 20:30:21 +0000 Subject: [Author: aharper] - Fix ASL logging to reference the correct formatter (and then replace with a new formatter). - Format ASL messages to include information that would normally be present in the standard formatter. - Allow ASL writers to pass a facility string. - Pretty up the function names in all logging, better matching NSAssert() names (as one example). - Don't throw if a logging filehandle is closed (SIGPIPE). Random exceptions from logging is bad, m'kay? R=thomasvl,dmaclach APPROVED=thomasvl --- Foundation/GTMLogger.m | 62 ++++++++++++++++++++++++++++++++++---------------- 1 file changed, 43 insertions(+), 19 deletions(-) (limited to 'Foundation/GTMLogger.m') diff --git a/Foundation/GTMLogger.m b/Foundation/GTMLogger.m index e40defc..ec51107 100644 --- a/Foundation/GTMLogger.m +++ b/Foundation/GTMLogger.m @@ -6,9 +6,9 @@ // Licensed under the Apache License, Version 2.0 (the "License"); you may not // use this file except in compliance with the License. You may obtain a copy // of the License at -// +// // http://www.apache.org/licenses/LICENSE-2.0 -// +// // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the @@ -36,20 +36,20 @@ - (void)logInternalFunc:(const char *)func format:(NSString *)fmt - valist:(va_list)args + valist:(va_list)args level:(GTMLoggerLevel)level NS_FORMAT_FUNCTION(2, 0); @end -// Reference to the shared GTMLogger instance. This is not a singleton, it's +// Reference to the shared GTMLogger instance. This is not a singleton, it's // just an easy reference to one shared instance. static GTMLogger *gSharedLogger = nil; @implementation GTMLogger -// Returns a pointer to the shared logger instance. If none exists, a standard +// Returns a pointer to the shared logger instance. If none exists, a standard // logger is created and returned. + (id)sharedLogger { @synchronized(self) { @@ -245,16 +245,16 @@ static GTMLogger *gSharedLogger = nil; - (void)logInternalFunc:(const char *)func format:(NSString *)fmt - valist:(va_list)args + valist:(va_list)args level:(GTMLoggerLevel)level { GTMLOGGER_ASSERT(formatter_ != nil); GTMLOGGER_ASSERT(filter_ != nil); GTMLOGGER_ASSERT(writer_ != nil); - + NSString *fname = func ? [NSString stringWithUTF8String:func] : nil; NSString *msg = [formatter_ stringForFunc:fname withFormat:fmt - valist:args + valist:args level:level]; if (msg && [filter_ filterAllowsMessage:msg level:level]) [writer_ logMessage:msg level:level]; @@ -279,7 +279,13 @@ static GTMLogger *gSharedLogger = nil; - (void)logMessage:(NSString *)msg level:(GTMLoggerLevel)level { @synchronized(self) { NSString *line = [NSString stringWithFormat:@"%@\n", msg]; - [self writeData:[line dataUsingEncoding:NSUTF8StringEncoding]]; + // Closed pipes should not generate exceptions in our caller + @try { + [self writeData:[line dataUsingEncoding:NSUTF8StringEncoding]]; + } + @catch (NSException *e) { + // Ignored + } } } @@ -306,18 +312,18 @@ static GTMLogger *gSharedLogger = nil; - (void)logMessage:(NSString *)msg level:(GTMLoggerLevel)level { switch (level) { case kGTMLoggerLevelDebug: - [self logDebug:@"%@", msg]; + [self logDebug:@"%@", msg]; break; case kGTMLoggerLevelInfo: [self logInfo:@"%@", msg]; break; - case kGTMLoggerLevelError: + case kGTMLoggerLevelError: [self logError:@"%@", msg]; break; case kGTMLoggerLevelAssert: [self logAssert:@"%@", msg]; break; - default: + default: // Ignore the message. break; } @@ -328,12 +334,30 @@ static GTMLogger *gSharedLogger = nil; @implementation GTMLogBasicFormatter +- (NSString *)prettyNameForFunc:(NSString *)func { + NSString *name = [func stringByTrimmingCharactersInSet: + [NSCharacterSet whitespaceAndNewlineCharacterSet]]; + NSString *function = @"(unknown)"; + if ([name length]) { + if (// Objective C __func__ and __PRETTY_FUNCTION__ + [name hasPrefix:@"-["] || [name hasPrefix:@"+["] || + // C++ __PRETTY_FUNCTION__ and other preadorned formats + [name hasSuffix:@")"]) { + function = name; + } else { + // Assume C99 __func__ + function = [NSString stringWithFormat:@"%@()", name]; + } + } + return function; +} + - (NSString *)stringForFunc:(NSString *)func withFormat:(NSString *)fmt - valist:(va_list)args + valist:(va_list)args level:(GTMLoggerLevel)level { // Performance note: We may want to do a quick check here to see if |fmt| - // contains a '%', and if not, simply return 'fmt'. + // contains a '%', and if not, simply return 'fmt'. return [[[NSString alloc] initWithFormat:fmt arguments:args] autorelease]; } @@ -361,7 +385,7 @@ static GTMLogger *gSharedLogger = nil; - (NSString *)stringForFunc:(NSString *)func withFormat:(NSString *)fmt - valist:(va_list)args + valist:(va_list)args level:(GTMLoggerLevel)level { GTMLOGGER_ASSERT(dateFormatter_ != nil); NSString *tstamp = nil; @@ -370,7 +394,7 @@ static GTMLogger *gSharedLogger = nil; } return [NSString stringWithFormat:@"%@ %@[%d/%p] [lvl=%d] %@ %@", tstamp, pname_, pid_, pthread_self(), - level, (func ? func : @"(no func)"), + level, [self prettyNameForFunc:func], [super stringForFunc:func withFormat:fmt valist:args level:level]]; } @@ -388,7 +412,7 @@ static BOOL IsVerboseLoggingEnabled(void) { static char *env = NULL; if (env == NULL) env = getenv([kVerboseLoggingKey UTF8String]); - + if (env && env[0]) { return (strtol(env, NULL, 10) != 0); } @@ -403,9 +427,9 @@ static BOOL IsVerboseLoggingEnabled(void) { #if DEBUG return YES; #endif - + BOOL allow = YES; - + switch (level) { case kGTMLoggerLevelDebug: allow = NO; -- cgit v1.2.3