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