Skip to content

Commit f8b05bd

Browse files
committed
Upgrading to version 1.6 of CocoaLumberjack
1 parent 271fad0 commit f8b05bd

File tree

4 files changed

+87
-9
lines changed

4 files changed

+87
-9
lines changed

Vendor/CocoaLumberjack/DDLog.h

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -427,9 +427,22 @@ NSString *DDExtractFileNameWithoutExtension(const char *filePath, BOOL copy);
427427
* The formatter may also optionally filter the log message by returning nil,
428428
* in which case the logger will not log the message.
429429
**/
430-
431430
- (NSString *)formatLogMessage:(DDLogMessage *)logMessage;
432431

432+
@optional
433+
434+
/**
435+
* A single formatter instance can be added to multiple loggers.
436+
* These methods provides hooks to notify the formatter of when it's added/removed.
437+
*
438+
* This is primarily for thread-safety.
439+
* If a formatter is explicitly not thread-safe, it may wish to throw an exception if added to multiple loggers.
440+
* Or if a formatter has potentially thread-unsafe code (e.g. NSDateFormatter),
441+
* it could possibly use these hooks to switch to thread-safe versions of the code.
442+
**/
443+
- (void)didAddToLogger:(id <DDLogger>)logger;
444+
- (void)willRemoveFromLogger:(id <DDLogger>)logger;
445+
433446
@end
434447

435448
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Vendor/CocoaLumberjack/DDLog.m

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1022,11 +1022,21 @@ - (void)setLogFormatter:(id <DDLogFormatter>)logFormatter
10221022
{
10231023
// The design of this method is documented extensively in the logFormatter message (above in code).
10241024

1025-
dispatch_block_t block = ^{
1026-
if (formatter != logFormatter) {
1025+
dispatch_block_t block = ^{ @autoreleasepool {
1026+
1027+
if (formatter != logFormatter)
1028+
{
1029+
if ([formatter respondsToSelector:@selector(willRemoveFromLogger:)]) {
1030+
[formatter willRemoveFromLogger:self];
1031+
}
1032+
10271033
formatter = logFormatter;
1034+
1035+
if ([formatter respondsToSelector:@selector(didAddToLogger:)]) {
1036+
[formatter didAddToLogger:self];
1037+
}
10281038
}
1029-
};
1039+
}};
10301040

10311041
dispatch_queue_t currentQueue = dispatch_get_current_queue();
10321042
if (currentQueue == loggerQueue)

Vendor/CocoaLumberjack/Extensions/DispatchQueueLogFormatter.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,13 @@
5353
{
5454
@protected
5555

56-
NSDateFormatter *dateFormatter;
56+
NSString *dateFormatString;
5757

5858
@private
5959

60+
int32_t atomicLoggerCount;
61+
NSDateFormatter *threadUnsafeDateFormatter; // Use [self stringFromDate]
62+
6063
OSSpinLock lock;
6164

6265
NSUInteger _minQueueLength; // _prefix == Only access via atomic property

Vendor/CocoaLumberjack/Extensions/DispatchQueueLogFormatter.m

Lines changed: 56 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#import "DispatchQueueLogFormatter.h"
2+
#import <libkern/OSAtomic.h>
23

34
/**
45
* Welcome to Cocoa Lumberjack!
@@ -21,9 +22,10 @@ - (id)init
2122
{
2223
if ((self = [super init]))
2324
{
24-
dateFormatter = [[NSDateFormatter alloc] init];
25-
[dateFormatter setFormatterBehavior:NSDateFormatterBehavior10_4];
26-
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss:SSS"];
25+
dateFormatString = @"yyyy-MM-dd HH:mm:ss:SSS";
26+
27+
atomicLoggerCount = 0;
28+
threadUnsafeDateFormatter = nil;
2729

2830
_minQueueLength = 0;
2931
_maxQueueLength = 0;
@@ -73,6 +75,46 @@ - (void)setReplacementString:(NSString *)shortLabel forQueueLabel:(NSString *)lo
7375
#pragma mark DDLogFormatter
7476
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
7577

78+
- (NSString *)stringFromDate:(NSDate *)date
79+
{
80+
int32_t loggerCount = OSAtomicAdd32(0, &atomicLoggerCount);
81+
82+
if (loggerCount <= 1)
83+
{
84+
// Single-threaded mode.
85+
86+
if (threadUnsafeDateFormatter == nil)
87+
{
88+
threadUnsafeDateFormatter = [[NSDateFormatter alloc] init];
89+
[threadUnsafeDateFormatter setFormatterBehavior:NSDateFormatterBehavior10_4];
90+
[threadUnsafeDateFormatter setDateFormat:dateFormatString];
91+
}
92+
93+
return [threadUnsafeDateFormatter stringFromDate:date];
94+
}
95+
else
96+
{
97+
// Multi-threaded mode.
98+
// NSDateFormatter is NOT thread-safe.
99+
100+
NSString *key = @"DispatchQueueLogFormatter_NSDateFormatter";
101+
102+
NSMutableDictionary *threadDictionary = [[NSThread currentThread] threadDictionary];
103+
NSDateFormatter *dateFormatter = [threadDictionary objectForKey:key];
104+
105+
if (dateFormatter == nil)
106+
{
107+
dateFormatter = [[NSDateFormatter alloc] init];
108+
[dateFormatter setFormatterBehavior:NSDateFormatterBehavior10_4];
109+
[dateFormatter setDateFormat:dateFormatString];
110+
111+
[threadDictionary setObject:dateFormatter forKey:key];
112+
}
113+
114+
return [dateFormatter stringFromDate:date];
115+
}
116+
}
117+
76118
- (NSString *)queueThreadLabelForLogMessage:(DDLogMessage *)logMessage
77119
{
78120
// As per the DDLogFormatter contract, this method is always invoked on the same thread/dispatch_queue
@@ -180,10 +222,20 @@ - (NSString *)queueThreadLabelForLogMessage:(DDLogMessage *)logMessage
180222

181223
- (NSString *)formatLogMessage:(DDLogMessage *)logMessage
182224
{
183-
NSString *timestamp = [dateFormatter stringFromDate:(logMessage->timestamp)];
225+
NSString *timestamp = [self stringFromDate:(logMessage->timestamp)];
184226
NSString *queueThreadLabel = [self queueThreadLabelForLogMessage:logMessage];
185227

186228
return [NSString stringWithFormat:@"%@ [%@] %@", timestamp, queueThreadLabel, logMessage->logMsg];
187229
}
188230

231+
- (void)didAddToLogger:(id <DDLogger>)logger
232+
{
233+
OSAtomicIncrement32(&atomicLoggerCount);
234+
}
235+
236+
- (void)willRemoveFromLogger:(id <DDLogger>)logger
237+
{
238+
OSAtomicDecrement32(&atomicLoggerCount);
239+
}
240+
189241
@end

0 commit comments

Comments
 (0)