Skip to content

Commit

Permalink
Remove code duplication by creating convenience assertion macros
Browse files Browse the repository at this point in the history
  • Loading branch information
ffried committed Dec 7, 2023
1 parent f270283 commit 135d035
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 153 deletions.
61 changes: 18 additions & 43 deletions Sources/CocoaLumberjack/DDAbstractDatabaseLogger.m
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,7 @@ - (NSUInteger)saveThreshold {
// This is the intended result. Fix it by accessing the ivar directly.
// Great strides have been take to ensure this is safe to do. Plus it's MUCH faster.

NSAssert(![self isOnGlobalLoggingQueue], @"Core architecture requirement failure");
NSAssert(![self isOnInternalLoggerQueue], @"MUST access ivar directly, NOT via self.* syntax.");
DDAbstractLoggerAssertLockedPropertyAccess();

dispatch_queue_t globalLoggingQueue = [DDLog loggingQueue];

Expand Down Expand Up @@ -255,10 +254,8 @@ - (void)setSaveThreshold:(NSUInteger)threshold {
if ([self isOnInternalLoggerQueue]) {
block();
} else {
dispatch_queue_t globalLoggingQueue = [DDLog loggingQueue];
NSAssert(![self isOnGlobalLoggingQueue], @"Core architecture requirement failure");

dispatch_async(globalLoggingQueue, ^{
DDAbstractLoggerAssertNotOnGlobalLoggingQueue();
dispatch_async([DDLog loggingQueue], ^{
dispatch_async(self.loggerQueue, block);
});
}
Expand All @@ -275,8 +272,7 @@ - (NSTimeInterval)saveInterval {
// This is the intended result. Fix it by accessing the ivar directly.
// Great strides have been take to ensure this is safe to do. Plus it's MUCH faster.

NSAssert(![self isOnGlobalLoggingQueue], @"Core architecture requirement failure");
NSAssert(![self isOnInternalLoggerQueue], @"MUST access ivar directly, NOT via self.* syntax.");
DDAbstractLoggerAssertLockedPropertyAccess();

dispatch_queue_t globalLoggingQueue = [DDLog loggingQueue];

Expand Down Expand Up @@ -346,10 +342,8 @@ - (void)setSaveInterval:(NSTimeInterval)interval {
if ([self isOnInternalLoggerQueue]) {
block();
} else {
dispatch_queue_t globalLoggingQueue = [DDLog loggingQueue];
NSAssert(![self isOnGlobalLoggingQueue], @"Core architecture requirement failure");

dispatch_async(globalLoggingQueue, ^{
DDAbstractLoggerAssertNotOnGlobalLoggingQueue();
dispatch_async([DDLog loggingQueue], ^{
dispatch_async(self.loggerQueue, block);
});
}
Expand All @@ -366,14 +360,11 @@ - (NSTimeInterval)maxAge {
// This is the intended result. Fix it by accessing the ivar directly.
// Great strides have been take to ensure this is safe to do. Plus it's MUCH faster.

NSAssert(![self isOnGlobalLoggingQueue], @"Core architecture requirement failure");
NSAssert(![self isOnInternalLoggerQueue], @"MUST access ivar directly, NOT via self.* syntax.");

dispatch_queue_t globalLoggingQueue = [DDLog loggingQueue];
DDAbstractLoggerAssertLockedPropertyAccess();

__block NSTimeInterval result;

dispatch_sync(globalLoggingQueue, ^{
dispatch_sync([DDLog loggingQueue], ^{
dispatch_sync(self.loggerQueue, ^{
result = self->_maxAge;
});
Expand Down Expand Up @@ -443,10 +434,8 @@ - (void)setMaxAge:(NSTimeInterval)interval {
if ([self isOnInternalLoggerQueue]) {
block();
} else {
dispatch_queue_t globalLoggingQueue = [DDLog loggingQueue];
NSAssert(![self isOnGlobalLoggingQueue], @"Core architecture requirement failure");

dispatch_async(globalLoggingQueue, ^{
DDAbstractLoggerAssertNotOnGlobalLoggingQueue();
dispatch_async([DDLog loggingQueue], ^{
dispatch_async(self.loggerQueue, block);
});
}
Expand All @@ -463,14 +452,11 @@ - (NSTimeInterval)deleteInterval {
// This is the intended result. Fix it by accessing the ivar directly.
// Great strides have been take to ensure this is safe to do. Plus it's MUCH faster.

NSAssert(![self isOnGlobalLoggingQueue], @"Core architecture requirement failure");
NSAssert(![self isOnInternalLoggerQueue], @"MUST access ivar directly, NOT via self.* syntax.");

dispatch_queue_t globalLoggingQueue = [DDLog loggingQueue];
DDAbstractLoggerAssertLockedPropertyAccess();

__block NSTimeInterval result;

dispatch_sync(globalLoggingQueue, ^{
dispatch_sync([DDLog loggingQueue], ^{
dispatch_sync(self.loggerQueue, ^{
result = self->_deleteInterval;
});
Expand Down Expand Up @@ -533,10 +519,9 @@ - (void)setDeleteInterval:(NSTimeInterval)interval {
if ([self isOnInternalLoggerQueue]) {
block();
} else {
dispatch_queue_t globalLoggingQueue = [DDLog loggingQueue];
NSAssert(![self isOnGlobalLoggingQueue], @"Core architecture requirement failure");
DDAbstractLoggerAssertNotOnGlobalLoggingQueue();

dispatch_async(globalLoggingQueue, ^{
dispatch_async([DDLog loggingQueue], ^{
dispatch_async(self.loggerQueue, block);
});
}
Expand All @@ -553,14 +538,11 @@ - (BOOL)deleteOnEverySave {
// This is the intended result. Fix it by accessing the ivar directly.
// Great strides have been take to ensure this is safe to do. Plus it's MUCH faster.

NSAssert(![self isOnGlobalLoggingQueue], @"Core architecture requirement failure");
NSAssert(![self isOnInternalLoggerQueue], @"MUST access ivar directly, NOT via self.* syntax.");

dispatch_queue_t globalLoggingQueue = [DDLog loggingQueue];
DDAbstractLoggerAssertLockedPropertyAccess();

__block BOOL result;

dispatch_sync(globalLoggingQueue, ^{
dispatch_sync([DDLog loggingQueue], ^{
dispatch_sync(self.loggerQueue, ^{
result = self->_deleteOnEverySave;
});
Expand All @@ -580,10 +562,8 @@ - (void)setDeleteOnEverySave:(BOOL)flag {
if ([self isOnInternalLoggerQueue]) {
block();
} else {
dispatch_queue_t globalLoggingQueue = [DDLog loggingQueue];
NSAssert(![self isOnGlobalLoggingQueue], @"Core architecture requirement failure");

dispatch_async(globalLoggingQueue, ^{
DDAbstractLoggerAssertNotOnGlobalLoggingQueue();
dispatch_async([DDLog loggingQueue], ^{
dispatch_async(self.loggerQueue, block);
});
}
Expand Down Expand Up @@ -627,17 +607,13 @@ - (void)deleteOldLogEntries {

- (void)didAddLogger {
// If you override me be sure to invoke [super didAddLogger];

[self createSuspendedSaveTimer];

[self createAndStartDeleteTimer];
}

- (void)willRemoveLogger {
// If you override me be sure to invoke [super willRemoveLogger];

[self performSaveAndSuspendSaveTimer];

[self destroySaveTimer];
[self destroyDeleteTimer];
}
Expand All @@ -660,7 +636,6 @@ - (void)flush {
//
// It is called automatically when the application quits,
// or if the developer invokes DDLog's flushLog method prior to crashing or something.

[self performSaveAndSuspendSaveTimer];
}

Expand Down
Loading

0 comments on commit 135d035

Please sign in to comment.