Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
2 | pmbaty | 1 | #import "DDMultiFormatter.h" |
2 | |||
3 | /** |
||
4 | * Welcome to Cocoa Lumberjack! |
||
5 | * |
||
6 | * The project page has a wealth of documentation if you have any questions. |
||
7 | * https://github.com/CocoaLumberjack/CocoaLumberjack |
||
8 | * |
||
9 | * If you're new to the project you may wish to read the "Getting Started" page. |
||
10 | * https://github.com/CocoaLumberjack/CocoaLumberjack/wiki/GettingStarted |
||
11 | **/ |
||
12 | |||
13 | #if TARGET_OS_IPHONE |
||
14 | // Compiling for iOS |
||
15 | #if __IPHONE_OS_VERSION_MIN_REQUIRED >= 60000 // iOS 6.0 or later |
||
16 | #define NEEDS_DISPATCH_RETAIN_RELEASE 0 |
||
17 | #else // iOS 5.X or earlier |
||
18 | #define NEEDS_DISPATCH_RETAIN_RELEASE 1 |
||
19 | #endif |
||
20 | #else |
||
21 | // Compiling for Mac OS X |
||
22 | #if MAC_OS_X_VERSION_MIN_REQUIRED >= 1080 // Mac OS X 10.8 or later |
||
23 | #define NEEDS_DISPATCH_RETAIN_RELEASE 0 |
||
24 | #else // Mac OS X 10.7 or earlier |
||
25 | #define NEEDS_DISPATCH_RETAIN_RELEASE 1 |
||
26 | #endif |
||
27 | #endif |
||
28 | |||
29 | |||
30 | @interface DDMultiFormatter () |
||
31 | |||
32 | - (DDLogMessage *)logMessageForLine:(NSString *)line originalMessage:(DDLogMessage *)message; |
||
33 | |||
34 | @end |
||
35 | |||
36 | |||
37 | @implementation DDMultiFormatter { |
||
38 | dispatch_queue_t _queue; |
||
39 | NSMutableArray *_formatters; |
||
40 | } |
||
41 | |||
42 | - (id)init { |
||
43 | self = [super init]; |
||
44 | if (self) { |
||
45 | #if MAC_OS_X_VERSION_MIN_REQUIRED >= 1070 |
||
46 | _queue = dispatch_queue_create("cocoa.lumberjack.multiformatter", DISPATCH_QUEUE_CONCURRENT); |
||
47 | #else |
||
48 | _queue = dispatch_queue_create("cocoa.lumberjack.multiformatter", NULL); |
||
49 | #endif |
||
50 | _formatters = [NSMutableArray new]; |
||
51 | } |
||
52 | |||
53 | return self; |
||
54 | } |
||
55 | |||
56 | #if NEEDS_DISPATCH_RETAIN_RELEASE |
||
57 | - (void)dealloc { |
||
58 | dispatch_release(_queue); |
||
59 | } |
||
60 | #endif |
||
61 | |||
62 | #pragma mark Processing |
||
63 | |||
64 | - (NSString *)formatLogMessage:(DDLogMessage *)logMessage { |
||
65 | __block NSString *line = logMessage->logMsg; |
||
66 | |||
67 | dispatch_sync(_queue, ^{ |
||
68 | for (id<DDLogFormatter> formatter in _formatters) { |
||
69 | DDLogMessage *message = [self logMessageForLine:line originalMessage:logMessage]; |
||
70 | line = [formatter formatLogMessage:message]; |
||
71 | |||
72 | if (!line) { |
||
73 | break; |
||
74 | } |
||
75 | } |
||
76 | }); |
||
77 | |||
78 | return line; |
||
79 | } |
||
80 | |||
81 | - (DDLogMessage *)logMessageForLine:(NSString *)line originalMessage:(DDLogMessage *)message { |
||
82 | DDLogMessage *newMessage = [message copy]; |
||
83 | newMessage->logMsg = line; |
||
84 | return newMessage; |
||
85 | } |
||
86 | |||
87 | #pragma mark Formatters |
||
88 | |||
89 | - (NSArray *)formatters { |
||
90 | __block NSArray *formatters; |
||
91 | |||
92 | dispatch_sync(_queue, ^{ |
||
93 | formatters = [_formatters copy]; |
||
94 | }); |
||
95 | |||
96 | return formatters; |
||
97 | } |
||
98 | |||
99 | - (void)addFormatter:(id<DDLogFormatter>)formatter { |
||
100 | dispatch_barrier_async(_queue, ^{ |
||
101 | [_formatters addObject:formatter]; |
||
102 | }); |
||
103 | } |
||
104 | |||
105 | - (void)removeFormatter:(id<DDLogFormatter>)formatter { |
||
106 | dispatch_barrier_async(_queue, ^{ |
||
107 | [_formatters removeObject:formatter]; |
||
108 | }); |
||
109 | } |
||
110 | |||
111 | - (void)removeAllFormatters { |
||
112 | dispatch_barrier_async(_queue, ^{ |
||
113 | [_formatters removeAllObjects]; |
||
114 | }); |
||
115 | } |
||
116 | |||
117 | - (BOOL)isFormattingWithFormatter:(id<DDLogFormatter>)formatter { |
||
118 | __block BOOL hasFormatter; |
||
119 | |||
120 | dispatch_sync(_queue, ^{ |
||
121 | hasFormatter = [_formatters containsObject:formatter]; |
||
122 | }); |
||
123 | |||
124 | return hasFormatter; |
||
125 | } |
||
126 | |||
127 | @end |