/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. #import "GTMLogger+ASL.h"
  19. #import "GTMDefines.h"
  20. @implementation GTMLogger (GTMLoggerASLAdditions)
  21. + (id)standardLoggerWithASL {
  22. id me = [self standardLogger];
  23. [me setWriter:[[[GTMLogASLWriter alloc] init] autorelease]];
  24. [me setFormatter:[[[GTMLogASLFormatter alloc] init] autorelease]];
  25. return me;
  26. }
  27. @end
  28. @implementation GTMLogASLWriter
  29. + (id)aslWriter {
  30. return [[[self alloc] initWithClientClass:nil facility:nil] autorelease];
  31. }
  32. + (id)aslWriterWithFacility:(NSString *)facility {
  33. return [[[self alloc] initWithClientClass:nil facility:facility] autorelease];
  34. }
  35. - (id)init {
  36. return [self initWithClientClass:nil facility:nil];
  37. }
  38. - (id)initWithClientClass:(Class)clientClass facility:(NSString *)facility {
  39. if ((self = [super init])) {
  40. aslClientClass_ = clientClass;
  41. if (aslClientClass_ == nil) {
  42. aslClientClass_ = [GTMLoggerASLClient class];
  43. }
  44. facility_ = [facility copy];
  45. }
  46. return self;
  47. }
  48. - (void)dealloc {
  49. [facility_ release];
  50. [super dealloc];
  51. }
  52. - (void)logMessage:(NSString *)msg level:(GTMLoggerLevel)level {
  53. // Because |facility_| is an argument to asl_open() we must store a separate
  54. // one for each facility in thread-local storage.
  55. static NSString *const kASLClientKey = @"GTMLoggerASLClient";
  56. NSString *key = kASLClientKey;
  57. if (facility_) {
  58. key = [NSString stringWithFormat:@"GTMLoggerASLClient-%@", facility_];
  59. }
  60. // Lookup the ASL client in the thread-local storage dictionary
  61. NSMutableDictionary *tls = [[NSThread currentThread] threadDictionary];
  62. GTMLoggerASLClient *client = [tls objectForKey:key];
  63. // If the ASL client wasn't found (e.g., the first call from this thread),
  64. // then create it and store it in the thread-local storage dictionary
  65. if (client == nil) {
  66. client = [[[aslClientClass_ alloc] initWithFacility:facility_] autorelease];
  67. [tls setObject:client forKey:key];
  68. }
  69. // Map the GTMLoggerLevel level to an ASL level.
  70. int aslLevel = ASL_LEVEL_INFO;
  71. switch (level) {
  72. case kGTMLoggerLevelUnknown:
  73. case kGTMLoggerLevelDebug:
  74. case kGTMLoggerLevelInfo:
  75. aslLevel = ASL_LEVEL_NOTICE;
  76. break;
  77. case kGTMLoggerLevelError:
  78. aslLevel = ASL_LEVEL_ERR;
  79. break;
  80. case kGTMLoggerLevelAssert:
  81. aslLevel = ASL_LEVEL_ALERT;
  82. break;
  83. }
  84. [client log:msg level:aslLevel];
  85. }
  86. @end // GTMLogASLWriter
  87. @implementation GTMLogASLFormatter
  88. #if !defined(__clang__) && (__GNUC__*10+__GNUC_MINOR__ >= 42)
  89. // Some versions of GCC (4.2 and below AFAIK) aren't great about supporting
  90. // -Wmissing-format-attribute
  91. // when the function is anything more complex than foo(NSString *fmt, ...).
  92. // You see the error inside the function when you turn ... into va_args and
  93. // attempt to call another function (like vsprintf for example).
  94. // So we just shut off the warning for this function.
  95. #pragma GCC diagnostic ignored "-Wmissing-format-attribute"
  96. #endif // !__clang__
  97. - (NSString *)stringForFunc:(NSString *)func
  98. withFormat:(NSString *)fmt
  99. valist:(va_list)args
  100. level:(GTMLoggerLevel)level {
  101. return [NSString stringWithFormat:@"%@ %@",
  102. [self prettyNameForFunc:func],
  103. // |super| has guard for nil |fmt| and |args|
  104. [super stringForFunc:func withFormat:fmt valist:args level:level]];
  105. }
  106. #if !defined(__clang__) && (__GNUC__*10+__GNUC_MINOR__ >= 42)
  107. #pragma GCC diagnostic error "-Wmissing-format-attribute"
  108. #endif // !__clang__
  109. @end // GTMLogASLFormatter
  110. @implementation GTMLoggerASLClient
  111. - (id)init {
  112. return [self initWithFacility:nil];
  113. }
  114. - (id)initWithFacility:(NSString *)facility {
  115. if ((self = [super init])) {
  116. client_ = asl_open(NULL, [facility UTF8String], 0);
  117. if (client_ == NULL) {
  118. // COV_NF_START - no real way to test this
  119. [self release];
  120. return nil;
  121. // COV_NF_END
  122. }
  123. }
  124. return self;
  125. }
  126. - (void)dealloc {
  127. if (client_ != NULL) asl_close(client_);
  128. [super dealloc];
  129. }
  130. // We don't test this one line because we don't want to pollute actual system
  131. // logs with test messages.
  132. // COV_NF_START
  133. - (void)log:(NSString *)msg level:(int)level {
  134. asl_log(client_, NULL, level, "%s", [msg UTF8String]);
  135. }
  136. // COV_NF_END
  137. @end // GTMLoggerASLClient