説明なし

DDDispatchQueueLogFormatter.m 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. // Software License Agreement (BSD License)
  2. //
  3. // Copyright (c) 2010-2015, Deusty, LLC
  4. // All rights reserved.
  5. //
  6. // Redistribution and use of this software in source and binary forms,
  7. // with or without modification, are permitted provided that the following conditions are met:
  8. //
  9. // * Redistributions of source code must retain the above copyright notice,
  10. // this list of conditions and the following disclaimer.
  11. //
  12. // * Neither the name of Deusty nor the names of its contributors may be used
  13. // to endorse or promote products derived from this software without specific
  14. // prior written permission of Deusty, LLC.
  15. #import "DDDispatchQueueLogFormatter.h"
  16. #import <libkern/OSAtomic.h>
  17. #if !__has_feature(objc_arc)
  18. #error This file must be compiled with ARC. Use -fobjc-arc flag (or convert project to ARC).
  19. #endif
  20. @interface DDDispatchQueueLogFormatter () {
  21. NSString *_dateFormatString;
  22. int32_t _atomicLoggerCount;
  23. NSDateFormatter *_threadUnsafeDateFormatter; // Use [self stringFromDate]
  24. OSSpinLock _lock;
  25. NSUInteger _minQueueLength; // _prefix == Only access via atomic property
  26. NSUInteger _maxQueueLength; // _prefix == Only access via atomic property
  27. NSMutableDictionary *_replacements; // _prefix == Only access from within spinlock
  28. }
  29. @end
  30. @implementation DDDispatchQueueLogFormatter
  31. - (instancetype)init {
  32. if ((self = [super init])) {
  33. _dateFormatString = @"yyyy-MM-dd HH:mm:ss:SSS";
  34. _atomicLoggerCount = 0;
  35. _threadUnsafeDateFormatter = nil;
  36. _minQueueLength = 0;
  37. _maxQueueLength = 0;
  38. _replacements = [[NSMutableDictionary alloc] init];
  39. // Set default replacements:
  40. _replacements[@"com.apple.main-thread"] = @"main";
  41. }
  42. return self;
  43. }
  44. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  45. #pragma mark Configuration
  46. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  47. @synthesize minQueueLength = _minQueueLength;
  48. @synthesize maxQueueLength = _maxQueueLength;
  49. - (NSString *)replacementStringForQueueLabel:(NSString *)longLabel {
  50. NSString *result = nil;
  51. OSSpinLockLock(&_lock);
  52. {
  53. result = _replacements[longLabel];
  54. }
  55. OSSpinLockUnlock(&_lock);
  56. return result;
  57. }
  58. - (void)setReplacementString:(NSString *)shortLabel forQueueLabel:(NSString *)longLabel {
  59. OSSpinLockLock(&_lock);
  60. {
  61. if (shortLabel) {
  62. _replacements[longLabel] = shortLabel;
  63. } else {
  64. [_replacements removeObjectForKey:longLabel];
  65. }
  66. }
  67. OSSpinLockUnlock(&_lock);
  68. }
  69. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  70. #pragma mark DDLogFormatter
  71. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  72. - (NSString *)stringFromDate:(NSDate *)date {
  73. int32_t loggerCount = OSAtomicAdd32(0, &_atomicLoggerCount);
  74. NSString *calendarIdentifier = nil;
  75. #if defined(__IPHONE_8_0) || defined(__MAC_10_10)
  76. calendarIdentifier = NSCalendarIdentifierGregorian;
  77. #else
  78. calendarIdentifier = NSGregorianCalendar;
  79. #endif
  80. if (loggerCount <= 1) {
  81. // Single-threaded mode.
  82. if (_threadUnsafeDateFormatter == nil) {
  83. _threadUnsafeDateFormatter = [[NSDateFormatter alloc] init];
  84. [_threadUnsafeDateFormatter setFormatterBehavior:NSDateFormatterBehavior10_4];
  85. [_threadUnsafeDateFormatter setDateFormat:_dateFormatString];
  86. }
  87. [_threadUnsafeDateFormatter setCalendar:[[NSCalendar alloc] initWithCalendarIdentifier:calendarIdentifier]];
  88. return [_threadUnsafeDateFormatter stringFromDate:date];
  89. } else {
  90. // Multi-threaded mode.
  91. // NSDateFormatter is NOT thread-safe.
  92. NSString *key = @"DispatchQueueLogFormatter_NSDateFormatter";
  93. NSMutableDictionary *threadDictionary = [[NSThread currentThread] threadDictionary];
  94. NSDateFormatter *dateFormatter = threadDictionary[key];
  95. if (dateFormatter == nil) {
  96. dateFormatter = [[NSDateFormatter alloc] init];
  97. [dateFormatter setFormatterBehavior:NSDateFormatterBehavior10_4];
  98. [dateFormatter setDateFormat:_dateFormatString];
  99. threadDictionary[key] = dateFormatter;
  100. }
  101. [dateFormatter setCalendar:[[NSCalendar alloc] initWithCalendarIdentifier:calendarIdentifier]];
  102. return [dateFormatter stringFromDate:date];
  103. }
  104. }
  105. - (NSString *)queueThreadLabelForLogMessage:(DDLogMessage *)logMessage {
  106. // As per the DDLogFormatter contract, this method is always invoked on the same thread/dispatch_queue
  107. NSUInteger minQueueLength = self.minQueueLength;
  108. NSUInteger maxQueueLength = self.maxQueueLength;
  109. // Get the name of the queue, thread, or machID (whichever we are to use).
  110. NSString *queueThreadLabel = nil;
  111. BOOL useQueueLabel = YES;
  112. BOOL useThreadName = NO;
  113. if (logMessage->_queueLabel) {
  114. // If you manually create a thread, it's dispatch_queue will have one of the thread names below.
  115. // Since all such threads have the same name, we'd prefer to use the threadName or the machThreadID.
  116. NSArray *names = @[
  117. @"com.apple.root.low-priority",
  118. @"com.apple.root.default-priority",
  119. @"com.apple.root.high-priority",
  120. @"com.apple.root.low-overcommit-priority",
  121. @"com.apple.root.default-overcommit-priority",
  122. @"com.apple.root.high-overcommit-priority"
  123. ];
  124. for (NSString * name in names) {
  125. if ([logMessage->_queueLabel isEqualToString:name]) {
  126. useQueueLabel = NO;
  127. useThreadName = [logMessage->_threadName length] > 0;
  128. break;
  129. }
  130. }
  131. } else {
  132. useQueueLabel = NO;
  133. useThreadName = [logMessage->_threadName length] > 0;
  134. }
  135. if (useQueueLabel || useThreadName) {
  136. NSString *fullLabel;
  137. NSString *abrvLabel;
  138. if (useQueueLabel) {
  139. fullLabel = logMessage->_queueLabel;
  140. } else {
  141. fullLabel = logMessage->_threadName;
  142. }
  143. OSSpinLockLock(&_lock);
  144. {
  145. abrvLabel = _replacements[fullLabel];
  146. }
  147. OSSpinLockUnlock(&_lock);
  148. if (abrvLabel) {
  149. queueThreadLabel = abrvLabel;
  150. } else {
  151. queueThreadLabel = fullLabel;
  152. }
  153. } else {
  154. queueThreadLabel = logMessage->_threadID;
  155. }
  156. // Now use the thread label in the output
  157. NSUInteger labelLength = [queueThreadLabel length];
  158. // labelLength > maxQueueLength : truncate
  159. // labelLength < minQueueLength : padding
  160. // : exact
  161. if ((maxQueueLength > 0) && (labelLength > maxQueueLength)) {
  162. // Truncate
  163. return [queueThreadLabel substringToIndex:maxQueueLength];
  164. } else if (labelLength < minQueueLength) {
  165. // Padding
  166. NSUInteger numSpaces = minQueueLength - labelLength;
  167. char spaces[numSpaces + 1];
  168. memset(spaces, ' ', numSpaces);
  169. spaces[numSpaces] = '\0';
  170. return [NSString stringWithFormat:@"%@%s", queueThreadLabel, spaces];
  171. } else {
  172. // Exact
  173. return queueThreadLabel;
  174. }
  175. }
  176. - (NSString *)formatLogMessage:(DDLogMessage *)logMessage {
  177. NSString *timestamp = [self stringFromDate:(logMessage->_timestamp)];
  178. NSString *queueThreadLabel = [self queueThreadLabelForLogMessage:logMessage];
  179. return [NSString stringWithFormat:@"%@ [%@] %@", timestamp, queueThreadLabel, logMessage->_message];
  180. }
  181. - (void)didAddToLogger:(id <DDLogger>)logger {
  182. OSAtomicIncrement32(&_atomicLoggerCount);
  183. }
  184. - (void)willRemoveFromLogger:(id <DDLogger>)logger {
  185. OSAtomicDecrement32(&_atomicLoggerCount);
  186. }
  187. @end