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+ASL.m | 66 +++++++++++++++++++++++++++++++++++----------- 1 file changed, 50 insertions(+), 16 deletions(-) (limited to 'Foundation/GTMLogger+ASL.m') diff --git a/Foundation/GTMLogger+ASL.m b/Foundation/GTMLogger+ASL.m index 90ea7e5..435cec3 100644 --- a/Foundation/GTMLogger+ASL.m +++ b/Foundation/GTMLogger+ASL.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 @@ -25,7 +25,7 @@ + (id)standardLoggerWithASL { id me = [self standardLogger]; [me setWriter:[[[GTMLogASLWriter alloc] init] autorelease]]; - [me setFormatter:[[[GTMLogBasicFormatter alloc] init] autorelease]]; + [me setFormatter:[[[GTMLogASLFormatter alloc] init] autorelease]]; return me; } @@ -35,37 +35,53 @@ @implementation GTMLogASLWriter + (id)aslWriter { - return [[[self alloc] init] autorelease]; + return [[[self alloc] initWithClientClass:nil facility:nil] autorelease]; +} + ++ (id)aslWriterWithFacility:(NSString *)facility { + return [[[self alloc] initWithClientClass:nil facility:facility] autorelease]; } - (id)init { - return [self initWithClientClass:nil]; + return [self initWithClientClass:nil facility:nil]; } -- (id)initWithClientClass:(Class)clientClass { +- (id)initWithClientClass:(Class)clientClass facility:(NSString *)facility { if ((self = [super init])) { aslClientClass_ = clientClass; if (aslClientClass_ == nil) { aslClientClass_ = [GTMLoggerASLClient class]; } + facility_ = [facility copy]; } return self; } +- (void)dealloc { + [facility_ release]; + [super dealloc]; +} + - (void)logMessage:(NSString *)msg level:(GTMLoggerLevel)level { - static NSString *const kASLClientKey = @"GTMLoggerASLClientKey"; - + // Because |facility_| is an argument to asl_open() we must store a separate + // one for each facility in thread-local storage. + static NSString *const kASLClientKey = @"GTMLoggerASLClient"; + NSString *key = kASLClientKey; + if (facility_) { + key = [NSString stringWithFormat:@"GTMLoggerASLClient-%@", facility_]; + } + // Lookup the ASL client in the thread-local storage dictionary NSMutableDictionary *tls = [[NSThread currentThread] threadDictionary]; - GTMLoggerASLClient *client = [tls objectForKey:kASLClientKey]; - + GTMLoggerASLClient *client = [tls objectForKey:key]; + // If the ASL client wasn't found (e.g., the first call from this thread), // then create it and store it in the thread-local storage dictionary if (client == nil) { - client = [[[aslClientClass_ alloc] init] autorelease]; - [tls setObject:client forKey:kASLClientKey]; + client = [[[aslClientClass_ alloc] initWithFacility:facility_] autorelease]; + [tls setObject:client forKey:key]; } - + // Map the GTMLoggerLevel level to an ASL level. int aslLevel = ASL_LEVEL_INFO; switch (level) { @@ -81,18 +97,36 @@ aslLevel = ASL_LEVEL_ALERT; break; } - + [client log:msg level:aslLevel]; } @end // GTMLogASLWriter +@implementation GTMLogASLFormatter + +- (NSString *)stringForFunc:(NSString *)func + withFormat:(NSString *)fmt + valist:(va_list)args + level:(GTMLoggerLevel)level { + return [NSString stringWithFormat:@"%@ %@", + [self prettyNameForFunc:func], + [super stringForFunc:func withFormat:fmt valist:args level:level]]; +} + +@end // GTMLogASLFormatter + + @implementation GTMLoggerASLClient - (id)init { + return [self initWithFacility:nil]; +} + +- (id)initWithFacility:(NSString *)facility { if ((self = [super init])) { - client_ = asl_open(NULL, NULL, 0); + client_ = asl_open(NULL, [facility UTF8String], 0); if (client_ == nil) { // COV_NF_START - no real way to test this [self release]; @@ -115,7 +149,7 @@ } #endif -// We don't test this one line because we don't want to pollute actual system +// We don't test this one line because we don't want to pollute actual system // logs with test messages. // COV_NF_START - (void)log:(NSString *)msg level:(int)level { -- cgit v1.2.3