Nav apraksta

DDDispatchQueueLogFormatter.h 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 <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. * This class provides a log formatter that prints the dispatch_queue label instead of the mach_thread_id.
  24. *
  25. * A log formatter can be added to any logger to format and/or filter its output.
  26. * You can learn more about log formatters here:
  27. * Documentation/CustomFormatters.md
  28. *
  29. * A typical NSLog (or DDTTYLogger) prints detailed info as [<process_id>:<thread_id>].
  30. * For example:
  31. *
  32. * 2011-10-17 20:21:45.435 AppName[19928:5207] Your log message here
  33. *
  34. * Where:
  35. * - 19928 = process id
  36. * - 5207 = thread id (mach_thread_id printed in hex)
  37. *
  38. * When using grand central dispatch (GCD), this information is less useful.
  39. * This is because a single serial dispatch queue may be run on any thread from an internally managed thread pool.
  40. * For example:
  41. *
  42. * 2011-10-17 20:32:31.111 AppName[19954:4d07] Message from my_serial_dispatch_queue
  43. * 2011-10-17 20:32:31.112 AppName[19954:5207] Message from my_serial_dispatch_queue
  44. * 2011-10-17 20:32:31.113 AppName[19954:2c55] Message from my_serial_dispatch_queue
  45. *
  46. * This formatter allows you to replace the standard [box:info] with the dispatch_queue name.
  47. * For example:
  48. *
  49. * 2011-10-17 20:32:31.111 AppName[img-scaling] Message from my_serial_dispatch_queue
  50. * 2011-10-17 20:32:31.112 AppName[img-scaling] Message from my_serial_dispatch_queue
  51. * 2011-10-17 20:32:31.113 AppName[img-scaling] Message from my_serial_dispatch_queue
  52. *
  53. * If the dispatch_queue doesn't have a set name, then it falls back to the thread name.
  54. * If the current thread doesn't have a set name, then it falls back to the mach_thread_id in hex (like normal).
  55. *
  56. * Note: If manually creating your own background threads (via NSThread/alloc/init or NSThread/detachNeThread),
  57. * you can use [[NSThread currentThread] setName:(NSString *)].
  58. **/
  59. @interface DDDispatchQueueLogFormatter : NSObject <DDLogFormatter>
  60. /**
  61. * Standard init method.
  62. * Configure using properties as desired.
  63. **/
  64. - (instancetype)init NS_DESIGNATED_INITIALIZER;
  65. /**
  66. * The minQueueLength restricts the minimum size of the [detail box].
  67. * If the minQueueLength is set to 0, there is no restriction.
  68. *
  69. * For example, say a dispatch_queue has a label of "diskIO":
  70. *
  71. * If the minQueueLength is 0: [diskIO]
  72. * If the minQueueLength is 4: [diskIO]
  73. * If the minQueueLength is 5: [diskIO]
  74. * If the minQueueLength is 6: [diskIO]
  75. * If the minQueueLength is 7: [diskIO ]
  76. * If the minQueueLength is 8: [diskIO ]
  77. *
  78. * The default minQueueLength is 0 (no minimum, so [detail box] won't be padded).
  79. *
  80. * If you want every [detail box] to have the exact same width,
  81. * set both minQueueLength and maxQueueLength to the same value.
  82. **/
  83. @property (assign, atomic) NSUInteger minQueueLength;
  84. /**
  85. * The maxQueueLength restricts the number of characters that will be inside the [detail box].
  86. * If the maxQueueLength is 0, there is no restriction.
  87. *
  88. * For example, say a dispatch_queue has a label of "diskIO":
  89. *
  90. * If the maxQueueLength is 0: [diskIO]
  91. * If the maxQueueLength is 4: [disk]
  92. * If the maxQueueLength is 5: [diskI]
  93. * If the maxQueueLength is 6: [diskIO]
  94. * If the maxQueueLength is 7: [diskIO]
  95. * If the maxQueueLength is 8: [diskIO]
  96. *
  97. * The default maxQueueLength is 0 (no maximum, so [detail box] won't be truncated).
  98. *
  99. * If you want every [detail box] to have the exact same width,
  100. * set both minQueueLength and maxQueueLength to the same value.
  101. **/
  102. @property (assign, atomic) NSUInteger maxQueueLength;
  103. /**
  104. * Sometimes queue labels have long names like "com.apple.main-queue",
  105. * but you'd prefer something shorter like simply "main".
  106. *
  107. * This method allows you to set such preferred replacements.
  108. * The above example is set by default.
  109. *
  110. * To remove/undo a previous replacement, invoke this method with nil for the 'shortLabel' parameter.
  111. **/
  112. - (NSString *)replacementStringForQueueLabel:(NSString *)longLabel;
  113. - (void)setReplacementString:(NSString *)shortLabel forQueueLabel:(NSString *)longLabel;
  114. @end
  115. /**
  116. * Method declarations that make it easier to extend/modify DDDispatchQueueLogFormatter
  117. **/
  118. @interface DDDispatchQueueLogFormatter (OverridableMethods)
  119. - (NSString *)stringFromDate:(NSDate *)date;
  120. - (NSString *)queueThreadLabelForLogMessage:(DDLogMessage *)logMessage;
  121. - (NSString *)formatLogMessage:(DDLogMessage *)logMessage;
  122. @end