DispatchQueueLogFormatter.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #import <Foundation/Foundation.h>
  2. #import <libkern/OSAtomic.h>
  3. #import "DDLog.h"
  4. /**
  5. * Welcome to Cocoa Lumberjack!
  6. *
  7. * The project page has a wealth of documentation if you have any questions.
  8. * https://github.com/robbiehanson/CocoaLumberjack
  9. *
  10. * If you're new to the project you may wish to read the "Getting Started" page.
  11. * https://github.com/robbiehanson/CocoaLumberjack/wiki/GettingStarted
  12. *
  13. *
  14. * This class provides a log formatter that prints the dispatch_queue label instead of the mach_thread_id.
  15. *
  16. * A log formatter can be added to any logger to format and/or filter its output.
  17. * You can learn more about log formatters here:
  18. * https://github.com/robbiehanson/CocoaLumberjack/wiki/CustomFormatters
  19. *
  20. * A typical NSLog (or DDTTYLogger) prints detailed info as [<process_id>:<thread_id>].
  21. * For example:
  22. *
  23. * 2011-10-17 20:21:45.435 AppName[19928:5207] Your log message here
  24. *
  25. * Where:
  26. * - 19928 = process id
  27. * - 5207 = thread id (mach_thread_id printed in hex)
  28. *
  29. * When using grand central dispatch (GCD), this information is less useful.
  30. * This is because a single serial dispatch queue may be run on any thread from an internally managed thread pool.
  31. * For example:
  32. *
  33. * 2011-10-17 20:32:31.111 AppName[19954:4d07] Message from my_serial_dispatch_queue
  34. * 2011-10-17 20:32:31.112 AppName[19954:5207] Message from my_serial_dispatch_queue
  35. * 2011-10-17 20:32:31.113 AppName[19954:2c55] Message from my_serial_dispatch_queue
  36. *
  37. * This formatter allows you to replace the standard [box:info] with the dispatch_queue name.
  38. * For example:
  39. *
  40. * 2011-10-17 20:32:31.111 AppName[img-scaling] Message from my_serial_dispatch_queue
  41. * 2011-10-17 20:32:31.112 AppName[img-scaling] Message from my_serial_dispatch_queue
  42. * 2011-10-17 20:32:31.113 AppName[img-scaling] Message from my_serial_dispatch_queue
  43. *
  44. * If the dispatch_queue doesn't have a set name, then it falls back to the thread name.
  45. * If the current thread doesn't have a set name, then it falls back to the mach_thread_id in hex (like normal).
  46. *
  47. * Note: If manually creating your own background threads (via NSThread/alloc/init or NSThread/detachNeThread),
  48. * you can use [[NSThread currentThread] setName:(NSString *)].
  49. **/
  50. @interface DispatchQueueLogFormatter : NSObject <DDLogFormatter> {
  51. @protected
  52. NSString *dateFormatString;
  53. }
  54. /**
  55. * Standard init method.
  56. * Configure using properties as desired.
  57. **/
  58. - (id)init;
  59. /**
  60. * The minQueueLength restricts the minimum size of the [detail box].
  61. * If the minQueueLength is set to 0, there is no restriction.
  62. *
  63. * For example, say a dispatch_queue has a label of "diskIO":
  64. *
  65. * If the minQueueLength is 0: [diskIO]
  66. * If the minQueueLength is 4: [diskIO]
  67. * If the minQueueLength is 5: [diskIO]
  68. * If the minQueueLength is 6: [diskIO]
  69. * If the minQueueLength is 7: [diskIO ]
  70. * If the minQueueLength is 8: [diskIO ]
  71. *
  72. * The default minQueueLength is 0 (no minimum, so [detail box] won't be padded).
  73. *
  74. * If you want every [detail box] to have the exact same width,
  75. * set both minQueueLength and maxQueueLength to the same value.
  76. **/
  77. @property (assign) NSUInteger minQueueLength;
  78. /**
  79. * The maxQueueLength restricts the number of characters that will be inside the [detail box].
  80. * If the maxQueueLength is 0, there is no restriction.
  81. *
  82. * For example, say a dispatch_queue has a label of "diskIO":
  83. *
  84. * If the maxQueueLength is 0: [diskIO]
  85. * If the maxQueueLength is 4: [disk]
  86. * If the maxQueueLength is 5: [diskI]
  87. * If the maxQueueLength is 6: [diskIO]
  88. * If the maxQueueLength is 7: [diskIO]
  89. * If the maxQueueLength is 8: [diskIO]
  90. *
  91. * The default maxQueueLength is 0 (no maximum, so [detail box] won't be truncated).
  92. *
  93. * If you want every [detail box] to have the exact same width,
  94. * set both minQueueLength and maxQueueLength to the same value.
  95. **/
  96. @property (assign) NSUInteger maxQueueLength;
  97. /**
  98. * Sometimes queue labels have long names like "com.apple.main-queue",
  99. * but you'd prefer something shorter like simply "main".
  100. *
  101. * This method allows you to set such preferred replacements.
  102. * The above example is set by default.
  103. *
  104. * To remove/undo a previous replacement, invoke this method with nil for the 'shortLabel' parameter.
  105. **/
  106. - (NSString *)replacementStringForQueueLabel:(NSString *)longLabel;
  107. - (void)setReplacementString:(NSString *)shortLabel forQueueLabel:(NSString *)longLabel;
  108. @end