PageRenderTime 49ms CodeModel.GetById 9ms app.highlight 37ms RepoModel.GetById 1ms app.codeStats 0ms

/core/externals/update-engine/externals/google-toolbox-for-mac/Foundation/GTMLogger+ASL.m

http://macfuse.googlecode.com/
Objective C | 168 lines | 101 code | 31 blank | 36 comment | 14 complexity | a7cdfb71b7074a5772fbcd486ac05897 MD5 | raw file
  1//
  2//  GTMLogger+ASL.m
  3//
  4//  Copyright 2007-2008 Google Inc.
  5//
  6//  Licensed under the Apache License, Version 2.0 (the "License"); you may not
  7//  use this file except in compliance with the License.  You may obtain a copy
  8//  of the License at
  9//
 10//  http://www.apache.org/licenses/LICENSE-2.0
 11//
 12//  Unless required by applicable law or agreed to in writing, software
 13//  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 14//  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
 15//  License for the specific language governing permissions and limitations under
 16//  the License.
 17//
 18
 19#import "GTMLogger+ASL.h"
 20#import "GTMDefines.h"
 21
 22
 23@implementation GTMLogger (GTMLoggerASLAdditions)
 24
 25+ (id)standardLoggerWithASL {
 26  id me = [self standardLogger];
 27  [me setWriter:[[[GTMLogASLWriter alloc] init] autorelease]];
 28  [me setFormatter:[[[GTMLogASLFormatter alloc] init] autorelease]];
 29  return me;
 30}
 31
 32@end
 33
 34
 35@implementation GTMLogASLWriter
 36
 37+ (id)aslWriter {
 38  return [[[self alloc] initWithClientClass:nil facility:nil] autorelease];
 39}
 40
 41+ (id)aslWriterWithFacility:(NSString *)facility {
 42  return [[[self alloc] initWithClientClass:nil facility:facility] autorelease];
 43}
 44
 45- (id)init {
 46  return [self initWithClientClass:nil facility:nil];
 47}
 48
 49- (id)initWithClientClass:(Class)clientClass facility:(NSString *)facility {
 50  if ((self = [super init])) {
 51    aslClientClass_ = clientClass;
 52    if (aslClientClass_ == nil) {
 53      aslClientClass_ = [GTMLoggerASLClient class];
 54    }
 55    facility_ = [facility copy];
 56  }
 57  return self;
 58}
 59
 60- (void)dealloc {
 61  [facility_ release];
 62  [super dealloc];
 63}
 64
 65- (void)logMessage:(NSString *)msg level:(GTMLoggerLevel)level {
 66  // Because |facility_| is an argument to asl_open() we must store a separate
 67  // one for each facility in thread-local storage.
 68  static NSString *const kASLClientKey = @"GTMLoggerASLClient";
 69  NSString *key = kASLClientKey;
 70  if (facility_) {
 71    key = [NSString stringWithFormat:@"GTMLoggerASLClient-%@", facility_];
 72  }
 73
 74  // Lookup the ASL client in the thread-local storage dictionary
 75  NSMutableDictionary *tls = [[NSThread currentThread] threadDictionary];
 76  GTMLoggerASLClient *client = [tls objectForKey:key];
 77
 78  // If the ASL client wasn't found (e.g., the first call from this thread),
 79  // then create it and store it in the thread-local storage dictionary
 80  if (client == nil) {
 81    client = [[[aslClientClass_ alloc] initWithFacility:facility_] autorelease];
 82    [tls setObject:client forKey:key];
 83  }
 84
 85  // Map the GTMLoggerLevel level to an ASL level.
 86  int aslLevel = ASL_LEVEL_INFO;
 87  switch (level) {
 88    case kGTMLoggerLevelUnknown:
 89    case kGTMLoggerLevelDebug:
 90    case kGTMLoggerLevelInfo:
 91      aslLevel = ASL_LEVEL_NOTICE;
 92      break;
 93    case kGTMLoggerLevelError:
 94      aslLevel = ASL_LEVEL_ERR;
 95      break;
 96    case kGTMLoggerLevelAssert:
 97      aslLevel = ASL_LEVEL_ALERT;
 98      break;
 99  }
100
101  [client log:msg level:aslLevel];
102}
103
104@end  // GTMLogASLWriter
105
106
107@implementation GTMLogASLFormatter
108
109#if !defined(__clang__) && (__GNUC__*10+__GNUC_MINOR__ >= 42)
110// Some versions of GCC (4.2 and below AFAIK) aren't great about supporting
111// -Wmissing-format-attribute
112// when the function is anything more complex than foo(NSString *fmt, ...).
113// You see the error inside the function when you turn ... into va_args and
114// attempt to call another function (like vsprintf for example).
115// So we just shut off the warning for this function.
116#pragma GCC diagnostic ignored "-Wmissing-format-attribute"
117#endif  // !__clang__
118
119- (NSString *)stringForFunc:(NSString *)func
120                 withFormat:(NSString *)fmt
121                     valist:(va_list)args
122                      level:(GTMLoggerLevel)level {
123  return [NSString stringWithFormat:@"%@ %@",
124           [self prettyNameForFunc:func],
125           // |super| has guard for nil |fmt| and |args|
126           [super stringForFunc:func withFormat:fmt valist:args level:level]];
127}
128
129#if !defined(__clang__) && (__GNUC__*10+__GNUC_MINOR__ >= 42)
130#pragma GCC diagnostic error "-Wmissing-format-attribute"
131#endif  // !__clang__
132
133@end  // GTMLogASLFormatter
134
135
136@implementation GTMLoggerASLClient
137
138- (id)init {
139  return [self initWithFacility:nil];
140}
141
142- (id)initWithFacility:(NSString *)facility {
143  if ((self = [super init])) {
144    client_ = asl_open(NULL, [facility UTF8String], 0);
145    if (client_ == NULL) {
146      // COV_NF_START - no real way to test this
147      [self release];
148      return nil;
149      // COV_NF_END
150    }
151  }
152  return self;
153}
154
155- (void)dealloc {
156  if (client_ != NULL) asl_close(client_);
157  [super dealloc];
158}
159
160// We don't test this one line because we don't want to pollute actual system
161// logs with test messages.
162// COV_NF_START
163- (void)log:(NSString *)msg level:(int)level {
164  asl_log(client_, NULL, level, "%s", [msg UTF8String]);
165}
166// COV_NF_END
167
168@end  // GTMLoggerASLClient