DDDispatchQueueLogFormatter.m 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. // Software License Agreement (BSD License)
  2. //
  3. // Copyright (c) 2010-2025, 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. #if !__has_feature(objc_arc)
  16. #error This file must be compiled with ARC. Use -fobjc-arc flag (or convert project to ARC).
  17. #endif
  18. #import <pthread/pthread.h>
  19. #import <stdatomic.h>
  20. #import <sys/qos.h>
  21. #import <CocoaLumberjack/DDDispatchQueueLogFormatter.h>
  22. DDQualityOfServiceName const DDQualityOfServiceUserInteractive = @"UI";
  23. DDQualityOfServiceName const DDQualityOfServiceUserInitiated = @"IN";
  24. DDQualityOfServiceName const DDQualityOfServiceDefault = @"DF";
  25. DDQualityOfServiceName const DDQualityOfServiceUtility = @"UT";
  26. DDQualityOfServiceName const DDQualityOfServiceBackground = @"BG";
  27. DDQualityOfServiceName const DDQualityOfServiceUnspecified = @"UN";
  28. static DDQualityOfServiceName _qos_name(NSUInteger qos) {
  29. switch ((qos_class_t) qos) {
  30. case QOS_CLASS_USER_INTERACTIVE: return DDQualityOfServiceUserInteractive;
  31. case QOS_CLASS_USER_INITIATED: return DDQualityOfServiceUserInitiated;
  32. case QOS_CLASS_DEFAULT: return DDQualityOfServiceDefault;
  33. case QOS_CLASS_UTILITY: return DDQualityOfServiceUtility;
  34. case QOS_CLASS_BACKGROUND: return DDQualityOfServiceBackground;
  35. default: return DDQualityOfServiceUnspecified;
  36. }
  37. }
  38. #pragma mark - DDDispatchQueueLogFormatter
  39. @interface DDDispatchQueueLogFormatter () {
  40. NSDateFormatter *_dateFormatter; // Use [self stringFromDate]
  41. pthread_mutex_t _mutex;
  42. NSUInteger _minQueueLength; // _prefix == Only access via atomic property
  43. NSUInteger _maxQueueLength; // _prefix == Only access via atomic property
  44. NSMutableDictionary *_replacements; // _prefix == Only access from within spinlock
  45. }
  46. @end
  47. @implementation DDDispatchQueueLogFormatter
  48. - (instancetype)init {
  49. if ((self = [super init])) {
  50. _dateFormatter = [self createDateFormatter];
  51. pthread_mutex_init(&_mutex, NULL);
  52. _replacements = [[NSMutableDictionary alloc] init];
  53. // Set default replacements:
  54. _replacements[@"com.apple.main-thread"] = @"main";
  55. }
  56. return self;
  57. }
  58. - (instancetype)initWithMode:(DDDispatchQueueLogFormatterMode)mode {
  59. return [self init];
  60. }
  61. - (void)dealloc {
  62. pthread_mutex_destroy(&_mutex);
  63. }
  64. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  65. #pragma mark Configuration
  66. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  67. @synthesize minQueueLength = _minQueueLength;
  68. @synthesize maxQueueLength = _maxQueueLength;
  69. - (NSString *)replacementStringForQueueLabel:(NSString *)longLabel {
  70. NSString *result = nil;
  71. pthread_mutex_lock(&_mutex);
  72. {
  73. result = _replacements[longLabel];
  74. }
  75. pthread_mutex_unlock(&_mutex);
  76. return result;
  77. }
  78. - (void)setReplacementString:(NSString *)shortLabel forQueueLabel:(NSString *)longLabel {
  79. pthread_mutex_lock(&_mutex);
  80. {
  81. if (shortLabel) {
  82. _replacements[longLabel] = shortLabel;
  83. } else {
  84. [_replacements removeObjectForKey:longLabel];
  85. }
  86. }
  87. pthread_mutex_unlock(&_mutex);
  88. }
  89. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  90. #pragma mark DDLogFormatter
  91. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  92. - (NSDateFormatter *)createDateFormatter {
  93. NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
  94. [self configureDateFormatter:formatter];
  95. return formatter;
  96. }
  97. - (void)configureDateFormatter:(NSDateFormatter *)dateFormatter {
  98. [dateFormatter setFormatterBehavior:NSDateFormatterBehavior10_4];
  99. [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss:SSS"];
  100. [dateFormatter setLocale:[NSLocale localeWithLocaleIdentifier:@"en_US_POSIX"]];
  101. [dateFormatter setCalendar:[[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian]];
  102. }
  103. - (NSString *)stringFromDate:(NSDate *)date {
  104. return [_dateFormatter stringFromDate:date];
  105. }
  106. - (NSString *)queueThreadLabelForLogMessage:(DDLogMessage *)logMessage {
  107. // As per the DDLogFormatter contract, this method is always invoked on the same thread/dispatch_queue
  108. __auto_type useQueueLabel = NO;
  109. if (logMessage->_queueLabel) {
  110. useQueueLabel = YES;
  111. // If you manually create a thread, it's dispatch_queue will have one of the thread names below.
  112. // Since all such threads have the same name, we'd prefer to use the threadName or the machThreadID.
  113. const NSArray<NSString *> *names = @[
  114. @"com.apple.root.low-priority",
  115. @"com.apple.root.default-priority",
  116. @"com.apple.root.high-priority",
  117. @"com.apple.root.low-overcommit-priority",
  118. @"com.apple.root.default-overcommit-priority",
  119. @"com.apple.root.high-overcommit-priority",
  120. @"com.apple.root.default-qos.overcommit",
  121. ];
  122. for (NSString *name in names) {
  123. if ([logMessage->_queueLabel isEqualToString:name]) {
  124. useQueueLabel = NO;
  125. break;
  126. }
  127. }
  128. }
  129. // Get the name of the queue, thread, or machID (whichever we are to use).
  130. NSString *queueThreadLabel;
  131. if (useQueueLabel || [logMessage->_threadName length] > 0) {
  132. __auto_type fullLabel = useQueueLabel ? logMessage->_queueLabel : logMessage->_threadName;
  133. NSString *abrvLabel;
  134. pthread_mutex_lock(&_mutex);
  135. {
  136. abrvLabel = _replacements[fullLabel];
  137. }
  138. pthread_mutex_unlock(&_mutex);
  139. queueThreadLabel = abrvLabel ?: fullLabel;
  140. } else {
  141. queueThreadLabel = logMessage->_threadID;
  142. }
  143. // Now use the thread label in the output
  144. // labelLength > maxQueueLength : truncate
  145. // labelLength < minQueueLength : padding
  146. // : exact
  147. __auto_type minQueueLength = self.minQueueLength;
  148. __auto_type maxQueueLength = self.maxQueueLength;
  149. __auto_type labelLength = [queueThreadLabel length];
  150. if (maxQueueLength > 0 && labelLength > maxQueueLength) {
  151. // Truncate
  152. return [queueThreadLabel substringToIndex:maxQueueLength];
  153. } else if (labelLength < minQueueLength) {
  154. // Padding
  155. return [queueThreadLabel stringByPaddingToLength:minQueueLength
  156. withString:@" "
  157. startingAtIndex:0];
  158. } else {
  159. // Exact
  160. return queueThreadLabel;
  161. }
  162. }
  163. - (NSString *)formatLogMessage:(DDLogMessage *)logMessage {
  164. __auto_type timestamp = [self stringFromDate:logMessage->_timestamp];
  165. __auto_type queueThreadLabel = [self queueThreadLabelForLogMessage:logMessage];
  166. return [NSString stringWithFormat:@"%@ [%@ (QOS:%@)] %@", timestamp, queueThreadLabel, _qos_name(logMessage->_qos), logMessage->_message];
  167. }
  168. @end
  169. #pragma mark - DDAtomicCounter
  170. @interface DDAtomicCounter() {
  171. atomic_int_fast32_t _value;
  172. }
  173. @end
  174. #pragma clang diagnostic push
  175. #pragma clang diagnostic ignored "-Wdeprecated-implementations"
  176. @implementation DDAtomicCounter
  177. #pragma clang diagnostic pop
  178. - (instancetype)initWithDefaultValue:(int32_t)defaultValue {
  179. if ((self = [super init])) {
  180. atomic_init(&_value, defaultValue);
  181. }
  182. return self;
  183. }
  184. - (int32_t)value {
  185. return atomic_load_explicit(&_value, memory_order_relaxed);
  186. }
  187. - (int32_t)increment {
  188. int32_t old = atomic_fetch_add_explicit(&_value, 1, memory_order_relaxed);
  189. return (old + 1);
  190. }
  191. - (int32_t)decrement {
  192. int32_t old = atomic_fetch_sub_explicit(&_value, 1, memory_order_relaxed);
  193. return (old - 1);
  194. }
  195. @end