DDDispatchQueueLogFormatter.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. // Software License Agreement (BSD License)
  2. //
  3. // Copyright (c) 2010-2019, 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 <Foundation/Foundation.h>
  16. // Disable legacy macros
  17. #ifndef DD_LEGACY_MACROS
  18. #define DD_LEGACY_MACROS 0
  19. #endif
  20. #import <CocoaLumberjack/DDLog.h>
  21. /**
  22. * Log formatter mode
  23. */
  24. typedef NS_ENUM(NSUInteger, DDDispatchQueueLogFormatterMode){
  25. /**
  26. * This is the default option, means the formatter can be reused between multiple loggers and therefore is thread-safe.
  27. * There is, of course, a performance cost for the thread-safety
  28. */
  29. DDDispatchQueueLogFormatterModeShareble = 0,
  30. /**
  31. * If the formatter will only be used by a single logger, then the thread-safety can be removed
  32. * @note: there is an assert checking if the formatter is added to multiple loggers and the mode is non-shareble
  33. */
  34. DDDispatchQueueLogFormatterModeNonShareble,
  35. };
  36. /**
  37. * This class provides a log formatter that prints the dispatch_queue label instead of the mach_thread_id.
  38. *
  39. * A log formatter can be added to any logger to format and/or filter its output.
  40. * You can learn more about log formatters here:
  41. * Documentation/CustomFormatters.md
  42. *
  43. * A typical `NSLog` (or `DDTTYLogger`) prints detailed info as `[<process_id>:<thread_id>]`.
  44. * For example:
  45. *
  46. * `2011-10-17 20:21:45.435 AppName[19928:5207] Your log message here`
  47. *
  48. * Where:
  49. * `- 19928 = process id`
  50. * `- 5207 = thread id (mach_thread_id printed in hex)`
  51. *
  52. * When using grand central dispatch (GCD), this information is less useful.
  53. * This is because a single serial dispatch queue may be run on any thread from an internally managed thread pool.
  54. * For example:
  55. *
  56. * `2011-10-17 20:32:31.111 AppName[19954:4d07] Message from my_serial_dispatch_queue`
  57. * `2011-10-17 20:32:31.112 AppName[19954:5207] Message from my_serial_dispatch_queue`
  58. * `2011-10-17 20:32:31.113 AppName[19954:2c55] Message from my_serial_dispatch_queue`
  59. *
  60. * This formatter allows you to replace the standard `[box:info]` with the dispatch_queue name.
  61. * For example:
  62. *
  63. * `2011-10-17 20:32:31.111 AppName[img-scaling] Message from my_serial_dispatch_queue`
  64. * `2011-10-17 20:32:31.112 AppName[img-scaling] Message from my_serial_dispatch_queue`
  65. * `2011-10-17 20:32:31.113 AppName[img-scaling] Message from my_serial_dispatch_queue`
  66. *
  67. * If the dispatch_queue doesn't have a set name, then it falls back to the thread name.
  68. * If the current thread doesn't have a set name, then it falls back to the mach_thread_id in hex (like normal).
  69. *
  70. * Note: If manually creating your own background threads (via `NSThread/alloc/init` or `NSThread/detachNeThread`),
  71. * you can use `[[NSThread currentThread] setName:(NSString *)]`.
  72. **/
  73. @interface DDDispatchQueueLogFormatter : NSObject <DDLogFormatter>
  74. /**
  75. * Standard init method.
  76. * Configure using properties as desired.
  77. **/
  78. - (instancetype)init NS_DESIGNATED_INITIALIZER;
  79. /**
  80. * Initializer with ability to set the queue mode
  81. *
  82. * @param mode choose between DDDispatchQueueLogFormatterModeShareble and DDDispatchQueueLogFormatterModeNonShareble, depending if the formatter is shared between several loggers or not
  83. */
  84. - (instancetype)initWithMode:(DDDispatchQueueLogFormatterMode)mode;
  85. /**
  86. * The minQueueLength restricts the minimum size of the [detail box].
  87. * If the minQueueLength is set to 0, there is no restriction.
  88. *
  89. * For example, say a dispatch_queue has a label of "diskIO":
  90. *
  91. * If the minQueueLength is 0: [diskIO]
  92. * If the minQueueLength is 4: [diskIO]
  93. * If the minQueueLength is 5: [diskIO]
  94. * If the minQueueLength is 6: [diskIO]
  95. * If the minQueueLength is 7: [diskIO ]
  96. * If the minQueueLength is 8: [diskIO ]
  97. *
  98. * The default minQueueLength is 0 (no minimum, so [detail box] won't be padded).
  99. *
  100. * If you want every [detail box] to have the exact same width,
  101. * set both minQueueLength and maxQueueLength to the same value.
  102. **/
  103. @property (assign, atomic) NSUInteger minQueueLength;
  104. /**
  105. * The maxQueueLength restricts the number of characters that will be inside the [detail box].
  106. * If the maxQueueLength is 0, there is no restriction.
  107. *
  108. * For example, say a dispatch_queue has a label of "diskIO":
  109. *
  110. * If the maxQueueLength is 0: [diskIO]
  111. * If the maxQueueLength is 4: [disk]
  112. * If the maxQueueLength is 5: [diskI]
  113. * If the maxQueueLength is 6: [diskIO]
  114. * If the maxQueueLength is 7: [diskIO]
  115. * If the maxQueueLength is 8: [diskIO]
  116. *
  117. * The default maxQueueLength is 0 (no maximum, so [detail box] won't be truncated).
  118. *
  119. * If you want every [detail box] to have the exact same width,
  120. * set both minQueueLength and maxQueueLength to the same value.
  121. **/
  122. @property (assign, atomic) NSUInteger maxQueueLength;
  123. /**
  124. * Sometimes queue labels have long names like "com.apple.main-queue",
  125. * but you'd prefer something shorter like simply "main".
  126. *
  127. * This method allows you to set such preferred replacements.
  128. * The above example is set by default.
  129. *
  130. * To remove/undo a previous replacement, invoke this method with nil for the 'shortLabel' parameter.
  131. **/
  132. - (NSString *)replacementStringForQueueLabel:(NSString *)longLabel;
  133. /**
  134. * See the `replacementStringForQueueLabel:` description
  135. */
  136. - (void)setReplacementString:(NSString *)shortLabel forQueueLabel:(NSString *)longLabel;
  137. @end
  138. /**
  139. * Category on `DDDispatchQueueLogFormatter` to make method declarations easier to extend/modify
  140. **/
  141. @interface DDDispatchQueueLogFormatter (OverridableMethods)
  142. /**
  143. * Date formatter default configuration
  144. */
  145. - (void)configureDateFormatter:(NSDateFormatter *)dateFormatter;
  146. /**
  147. * Formatter method to transfrom from date to string
  148. */
  149. - (NSString *)stringFromDate:(NSDate *)date;
  150. /**
  151. * Method to compute the queue thread label
  152. */
  153. - (NSString *)queueThreadLabelForLogMessage:(DDLogMessage *)logMessage;
  154. /**
  155. * The actual method that formats a message (transforms a `DDLogMessage` model into a printable string)
  156. */
  157. - (NSString *)formatLogMessage:(DDLogMessage *)logMessage;
  158. @end
  159. #pragma mark - DDAtomicCounter
  160. @protocol DDAtomicCountable <NSObject>
  161. - (instancetype)initWithDefaultValue:(int32_t)defaultValue;
  162. - (int32_t)increment;
  163. - (int32_t)decrement;
  164. - (int32_t)value;
  165. @end
  166. @interface DDAtomicCounter: NSObject<DDAtomicCountable>
  167. @end