DDDispatchQueueLogFormatter.h 6.4 KB

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