Skip to content

Commit 585d7cf

Browse files
committed
Fixing "dispatch_get_current_queue() deprecated" warning. Fixes issue CocoaLumberjack#62. Fixes issue CocoaLumberjack#65.
1 parent 290fef9 commit 585d7cf

File tree

4 files changed

+244
-204
lines changed

4 files changed

+244
-204
lines changed

Lumberjack/DDFileLogger.m

Lines changed: 85 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -522,118 +522,119 @@ - (void)dealloc
522522

523523
- (unsigned long long)maximumFileSize
524524
{
525+
__block unsigned long long result;
526+
527+
dispatch_block_t block = ^{
528+
result = maximumFileSize;
529+
};
530+
525531
// The design of this method is taken from the DDAbstractLogger implementation.
526532
// For extensive documentation please refer to the DDAbstractLogger implementation.
527533

528-
// Note: The internal implementation should access the maximumFileSize variable directly,
529-
// but if we forget to do this, then this method should at least work properly.
534+
// Note: The internal implementation MUST access the maximumFileSize variable directly,
535+
// This method is designed explicitly for external access.
536+
//
537+
// Using "self." syntax to go through this method will cause immediate deadlock.
538+
// This is the intended result. Fix it by accessing the ivar directly.
539+
// Great strides have been take to ensure this is safe to do. Plus it's MUCH faster.
530540

531-
dispatch_queue_t currentQueue = dispatch_get_current_queue();
532-
if (currentQueue == loggerQueue)
533-
{
534-
return maximumFileSize;
535-
}
536-
else
537-
{
538-
dispatch_queue_t globalLoggingQueue = [DDLog loggingQueue];
539-
NSAssert(currentQueue != globalLoggingQueue, @"Core architecture requirement failure");
540-
541-
__block unsigned long long result;
542-
543-
dispatch_sync(globalLoggingQueue, ^{
544-
dispatch_sync(loggerQueue, ^{
545-
result = maximumFileSize;
546-
});
547-
});
548-
549-
return result;
550-
}
541+
NSAssert(![self isOnGlobalLoggingQueue], @"Core architecture requirement failure");
542+
NSAssert(![self isOnInternalLoggerQueue], @"MUST access ivar directly, NOT via self.* syntax.");
543+
544+
dispatch_queue_t globalLoggingQueue = [DDLog loggingQueue];
545+
546+
dispatch_sync(globalLoggingQueue, ^{
547+
dispatch_sync(loggerQueue, block);
548+
});
549+
550+
return result;
551551
}
552552

553553
- (void)setMaximumFileSize:(unsigned long long)newMaximumFileSize
554554
{
555-
// The design of this method is taken from the DDAbstractLogger implementation.
556-
// For documentation please refer to the DDAbstractLogger implementation.
557-
558555
dispatch_block_t block = ^{ @autoreleasepool {
559556

560557
maximumFileSize = newMaximumFileSize;
561558
[self maybeRollLogFileDueToSize];
562559

563560
}};
564561

565-
dispatch_queue_t currentQueue = dispatch_get_current_queue();
566-
if (currentQueue == loggerQueue)
567-
{
568-
block();
569-
}
570-
else
571-
{
572-
dispatch_queue_t globalLoggingQueue = [DDLog loggingQueue];
573-
NSAssert(currentQueue != globalLoggingQueue, @"Core architecture requirement failure");
574-
575-
dispatch_async(globalLoggingQueue, ^{
576-
dispatch_async(loggerQueue, block);
577-
});
578-
}
562+
// The design of this method is taken from the DDAbstractLogger implementation.
563+
// For extensive documentation please refer to the DDAbstractLogger implementation.
564+
565+
// Note: The internal implementation MUST access the maximumFileSize variable directly,
566+
// This method is designed explicitly for external access.
567+
//
568+
// Using "self." syntax to go through this method will cause immediate deadlock.
569+
// This is the intended result. Fix it by accessing the ivar directly.
570+
// Great strides have been take to ensure this is safe to do. Plus it's MUCH faster.
571+
572+
NSAssert(![self isOnGlobalLoggingQueue], @"Core architecture requirement failure");
573+
NSAssert(![self isOnInternalLoggerQueue], @"MUST access ivar directly, NOT via self.* syntax.");
574+
575+
dispatch_queue_t globalLoggingQueue = [DDLog loggingQueue];
576+
577+
dispatch_async(globalLoggingQueue, ^{
578+
dispatch_async(loggerQueue, block);
579+
});
579580
}
580581

581582
- (NSTimeInterval)rollingFrequency
582583
{
584+
__block NSTimeInterval result;
585+
586+
dispatch_block_t block = ^{
587+
result = rollingFrequency;
588+
};
589+
583590
// The design of this method is taken from the DDAbstractLogger implementation.
584-
// For documentation please refer to the DDAbstractLogger implementation.
591+
// For extensive documentation please refer to the DDAbstractLogger implementation.
585592

586593
// Note: The internal implementation should access the rollingFrequency variable directly,
587-
// but if we forget to do this, then this method should at least work properly.
594+
// This method is designed explicitly for external access.
595+
//
596+
// Using "self." syntax to go through this method will cause immediate deadlock.
597+
// This is the intended result. Fix it by accessing the ivar directly.
598+
// Great strides have been take to ensure this is safe to do. Plus it's MUCH faster.
588599

589-
dispatch_queue_t currentQueue = dispatch_get_current_queue();
590-
if (currentQueue == loggerQueue)
591-
{
592-
return rollingFrequency;
593-
}
594-
else
595-
{
596-
dispatch_queue_t globalLoggingQueue = [DDLog loggingQueue];
597-
NSAssert(currentQueue != globalLoggingQueue, @"Core architecture requirement failure");
598-
599-
__block NSTimeInterval result;
600-
601-
dispatch_sync(globalLoggingQueue, ^{
602-
dispatch_sync(loggerQueue, ^{
603-
result = rollingFrequency;
604-
});
605-
});
606-
607-
return result;
608-
}
600+
NSAssert(![self isOnGlobalLoggingQueue], @"Core architecture requirement failure");
601+
NSAssert(![self isOnInternalLoggerQueue], @"MUST access ivar directly, NOT via self.* syntax.");
602+
603+
dispatch_queue_t globalLoggingQueue = [DDLog loggingQueue];
604+
605+
dispatch_sync(globalLoggingQueue, ^{
606+
dispatch_sync(loggerQueue, block);
607+
});
608+
609+
return result;
609610
}
610611

611612
- (void)setRollingFrequency:(NSTimeInterval)newRollingFrequency
612613
{
613-
// The design of this method is taken from the DDAbstractLogger implementation.
614-
// For documentation please refer to the DDAbstractLogger implementation.
615-
616614
dispatch_block_t block = ^{ @autoreleasepool {
617615

618616
rollingFrequency = newRollingFrequency;
619617
[self maybeRollLogFileDueToAge];
620-
621618
}};
622619

623-
dispatch_queue_t currentQueue = dispatch_get_current_queue();
624-
if (currentQueue == loggerQueue)
625-
{
626-
block();
627-
}
628-
else
629-
{
630-
dispatch_queue_t globalLoggingQueue = [DDLog loggingQueue];
631-
NSAssert(currentQueue != globalLoggingQueue, @"Core architecture requirement failure");
632-
633-
dispatch_async(globalLoggingQueue, ^{
634-
dispatch_async(loggerQueue, block);
635-
});
636-
}
620+
// The design of this method is taken from the DDAbstractLogger implementation.
621+
// For extensive documentation please refer to the DDAbstractLogger implementation.
622+
623+
// Note: The internal implementation should access the rollingFrequency variable directly,
624+
// This method is designed explicitly for external access.
625+
//
626+
// Using "self." syntax to go through this method will cause immediate deadlock.
627+
// This is the intended result. Fix it by accessing the ivar directly.
628+
// Great strides have been take to ensure this is safe to do. Plus it's MUCH faster.
629+
630+
NSAssert(![self isOnGlobalLoggingQueue], @"Core architecture requirement failure");
631+
NSAssert(![self isOnInternalLoggerQueue], @"MUST access ivar directly, NOT via self.* syntax.");
632+
633+
dispatch_queue_t globalLoggingQueue = [DDLog loggingQueue];
634+
635+
dispatch_async(globalLoggingQueue, ^{
636+
dispatch_async(loggerQueue, block);
637+
});
637638
}
638639

639640
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@@ -691,24 +692,23 @@ - (void)rollLogFile
691692
{
692693
// This method is public.
693694
// We need to execute the rolling on our logging thread/queue.
694-
//
695-
// The design of this method is taken from the DDAbstractLogger implementation.
696-
// For documentation please refer to the DDAbstractLogger implementation.
697695

698696
dispatch_block_t block = ^{ @autoreleasepool {
699697

700698
[self rollLogFileNow];
701699
}};
702700

703-
dispatch_queue_t currentQueue = dispatch_get_current_queue();
704-
if (currentQueue == loggerQueue)
701+
// The design of this method is taken from the DDAbstractLogger implementation.
702+
// For extensive documentation please refer to the DDAbstractLogger implementation.
703+
704+
if ([self isOnInternalLoggerQueue])
705705
{
706706
block();
707707
}
708708
else
709709
{
710710
dispatch_queue_t globalLoggingQueue = [DDLog loggingQueue];
711-
NSAssert(currentQueue != globalLoggingQueue, @"Core architecture requirement failure");
711+
NSAssert(![self isOnGlobalLoggingQueue], @"Core architecture requirement failure");
712712

713713
dispatch_async(globalLoggingQueue, ^{
714714
dispatch_async(loggerQueue, block);

Lumberjack/DDLog.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -594,4 +594,8 @@ typedef int DDLogMessageOptions;
594594
- (id <DDLogFormatter>)logFormatter;
595595
- (void)setLogFormatter:(id <DDLogFormatter>)formatter;
596596

597+
// For thread-safety assertions
598+
- (BOOL)isOnGlobalLoggingQueue;
599+
- (BOOL)isOnInternalLoggerQueue;
600+
597601
@end

0 commit comments

Comments
 (0)