Permalink
Browse files

Better logging story

- MagicalRecord now correctly detects CocoaLumberjack.
- Updated MagicalRecord to use different log levels when
CocoaLumberjack is available.  When CocoaLumberjack is not available,
all log levels are logged via NSLog when DEBUG is defined and not
logged at all when DEBUG is undefined.
- Since MR_ENABLE_ACTIVE_RECORD_LOGGING doesn't easily work with
CocoaPods and because CocoaLumberjack is a better solution, I've
removed MR_ENABLE_ACTIVE_RECORD_LOGGING entirely.
  • Loading branch information...
1 parent 5d39093 commit 5568b3f9975d6ce1eef54e91d7c13690d0595732 @bachand bachand committed Aug 29, 2013
@@ -54,7 +54,7 @@ - (NSString *) MR_lookupKeyForRelationship:(NSRelationshipDescription *)relation
NSEntityDescription *destinationEntity = [relationshipInfo destinationEntity];
if (destinationEntity == nil)
{
- MRLog(@"Unable to find entity for type '%@'", [self valueForKey:kMagicalRecordImportRelationshipTypeKey]);
+ MRLogWarn(@"Unable to find entity for type '%@'", [self valueForKey:kMagicalRecordImportRelationshipTypeKey]);
return nil;
}
@@ -117,11 +117,11 @@ - (void) MR_addObject:(NSManagedObject *)relatedObject forRelationship:(NSRelati
}
@catch (NSException *exception)
{
- MRLog(@"Adding object for relationship failed: %@\n", relationshipInfo);
- MRLog(@"relatedObject.entity %@", [relatedObject entity]);
- MRLog(@"relationshipInfo.destinationEntity %@", [relationshipInfo destinationEntity]);
- MRLog(@"Add Relationship Selector: %@", addRelatedObjectToSetMessage);
- MRLog(@"perform selector error: %@", exception);
+ MRLogError(@"Adding object for relationship failed: %@\n", relationshipInfo);
+ MRLogError(@"relatedObject.entity %@", [relatedObject entity]);
+ MRLogError(@"relationshipInfo.destinationEntity %@", [relationshipInfo destinationEntity]);
+ MRLogError(@"Add Relationship Selector: %@", addRelatedObjectToSetMessage);
+ MRLogError(@"perform selector error: %@", exception);
}
}
@@ -118,7 +118,7 @@ + (NSArray *) MR_propertiesNamed:(NSArray *)properties
}
else
{
- MRLog(@"Property '%@' not found in %lx properties for %@", propertyName, (unsigned long)[propDict count], NSStringFromClass(self));
+ MRLogWarn(@"Property '%@' not found in %lx properties for %@", propertyName, (unsigned long)[propDict count], NSStringFromClass(self));
}
}
}
@@ -51,9 +51,9 @@ - (void) MR_mergeChangesFromiCloud:(NSNotification *)notification;
{
[self performBlock:^{
- MRLog(@"Merging changes From iCloud %@context%@",
- self == [NSManagedObjectContext MR_defaultContext] ? @"*** DEFAULT *** " : @"",
- ([NSThread isMainThread] ? @" *** on Main Thread ***" : @""));
+ MRLogInfo(@"Merging changes From iCloud %@context%@",
+ self == [NSManagedObjectContext MR_defaultContext] ? @"*** DEFAULT *** " : @"",
+ ([NSThread isMainThread] ? @" *** on Main Thread ***" : @""));
[self mergeChangesFromContextDidSaveNotification:notification];
@@ -67,9 +67,9 @@ - (void) MR_mergeChangesFromiCloud:(NSNotification *)notification;
- (void) MR_mergeChangesFromNotification:(NSNotification *)notification;
{
- MRLog(@"Merging changes to %@context%@",
- self == [NSManagedObjectContext MR_defaultContext] ? @"*** DEFAULT *** " : @"",
- ([NSThread isMainThread] ? @" *** on Main Thread ***" : @""));
+ MRLogInfo(@"Merging changes to %@context%@",
+ self == [NSManagedObjectContext MR_defaultContext] ? @"*** DEFAULT *** " : @"",
+ ([NSThread isMainThread] ? @" *** on Main Thread ***" : @""));
[self mergeChangesFromContextDidSaveNotification:notification];
}
@@ -100,7 +100,7 @@ + (void) MR_setDefaultContext:(NSManagedObjectContext *)moc
[[NSManagedObjectContext MR_defaultContext] MR_observeiCloudChangesInCoordinator:coordinator];
}];
}
- MRLog(@"Set Default Context: %@", defaultManagedObjectContext_);
+ MRLogVerbose(@"Set Default Context: %@", defaultManagedObjectContext_);
}
+ (NSManagedObjectContext *) MR_rootSavingContext;
@@ -119,7 +119,7 @@ + (void) MR_setRootSavingContext:(NSManagedObjectContext *)context;
[context MR_obtainPermanentIDsBeforeSaving];
[rootSavingContext setMergePolicy:NSMergeByPropertyObjectTrumpMergePolicy];
[rootSavingContext MR_setWorkingName:@"BACKGROUND SAVING (ROOT)"];
- MRLog(@"Set Root Saving Context: %@", rootSavingContext);
+ MRLogVerbose(@"Set Root Saving Context: %@", rootSavingContext);
}
+ (void) MR_initializeDefaultContextWithCoordinator:(NSPersistentStoreCoordinator *)coordinator;
@@ -169,7 +169,7 @@ + (NSManagedObjectContext *) MR_contextWithParent:(NSManagedObjectContext *)pare
+ (NSManagedObjectContext *) MR_newMainQueueContext;
{
NSManagedObjectContext *context = [[self alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
- MRLog(@"Created Main Queue Context: %@", context);
+ MRLogVerbose(@"Created Main Queue Context: %@", context);
return context;
}
@@ -183,7 +183,7 @@ + (NSManagedObjectContext *) MR_contextWithStoreCoordinator:(NSPersistentStoreCo
[context setPersistentStoreCoordinator:coordinator];
}];
- MRLog(@"-> Created Context %@", [context MR_workingName]);
+ MRLogVerbose(@"-> Created Context %@", [context MR_workingName]);
}
return context;
}
@@ -205,7 +205,7 @@ - (void) MR_contextWillSave:(NSNotification *)notification
if ([insertedObjects count])
{
- MRLog(@"Context %@ is about to save. Obtaining permanent IDs for new %lu inserted objects", [context MR_workingName], (unsigned long)[insertedObjects count]);
+ MRLogInfo(@"Context %@ is about to save. Obtaining permanent IDs for new %lu inserted objects", [context MR_workingName], (unsigned long)[insertedObjects count]);
NSError *error = nil;
BOOL success = [context obtainPermanentIDsForObjects:[insertedObjects allObjects] error:&error];
if (!success)
@@ -42,7 +42,7 @@ - (void)MR_saveWithOptions:(MRSaveContextOptions)mask completion:(MRSaveCompleti
BOOL saveParentContexts = ((mask & MRSaveParentContexts) == MRSaveParentContexts);
if (![self hasChanges]) {
- MRLog(@"NO CHANGES IN ** %@ ** CONTEXT - NOT SAVING", [self MR_workingName]);
+ MRLogInfo(@"NO CHANGES IN ** %@ ** CONTEXT - NOT SAVING", [self MR_workingName]);
if (completion)
{
@@ -54,9 +54,9 @@ - (void)MR_saveWithOptions:(MRSaveContextOptions)mask completion:(MRSaveCompleti
return;
}
- MRLog(@"→ Saving %@", [self MR_description]);
- MRLog(@"→ Save Parents? %@", @(saveParentContexts));
- MRLog(@"→ Save Synchronously? %@", @(syncSave));
+ MRLogInfo(@"→ Saving %@", [self MR_description]);
+ MRLogInfo(@"→ Save Parents? %@", @(saveParentContexts));
+ MRLogInfo(@"→ Save Synchronously? %@", @(syncSave));
id saveBlock = ^{
NSError *error = nil;
@@ -68,7 +68,7 @@ - (void)MR_saveWithOptions:(MRSaveContextOptions)mask completion:(MRSaveCompleti
}
@catch(NSException *exception)
{
- MRLog(@"Unable to perform save: %@", (id)[exception userInfo] ? : (id)[exception reason]);
+ MRLogError(@"Unable to perform save: %@", (id)[exception userInfo] ? : (id)[exception reason]);
}
@finally
@@ -88,7 +88,7 @@ - (void)MR_saveWithOptions:(MRSaveContextOptions)mask completion:(MRSaveCompleti
}
// Do the completion action if one was specified
else {
- MRLog(@"→ Finished saving: %@", [self MR_description]);
+ MRLogInfo(@"→ Finished saving: %@", [self MR_description]);
if (completion) {
dispatch_async(dispatch_get_main_queue(), ^{
@@ -83,7 +83,7 @@ - (NSPersistentStore *) MR_addSqliteStoreNamed:(id)storeFileName withOptions:(__
// Could not open the database, so... kill it!
[[NSFileManager defaultManager] removeItemAtURL:url error:nil];
- MRLog(@"Removed incompatible model version: %@", [url lastPathComponent]);
+ MRLogInfo(@"Removed incompatible model version: %@", [url lastPathComponent]);
// Try one more time to create the store
store = [self addPersistentStoreWithType:NSSQLiteStoreType
@@ -209,7 +209,7 @@ - (void) MR_addiCloudContainerID:(NSString *)containerID contentNameKey:(NSStrin
}
else
{
- MRLog(@"iCloud is not enabled");
+ MRLogError(@"iCloud is not enabled");
}
[self lock];
@@ -32,22 +32,22 @@ + (void) defaultErrorHandler:(NSError *)error
{
if ([e respondsToSelector:@selector(userInfo)])
{
- MRLog(@"Error Details: %@", [e userInfo]);
+ MRLogError(@"Error Details: %@", [e userInfo]);
}
else
{
- MRLog(@"Error Details: %@", e);
+ MRLogError(@"Error Details: %@", e);
}
}
}
else
{
- MRLog(@"Error: %@", detailedError);
+ MRLogError(@"Error: %@", detailedError);
}
}
- MRLog(@"Error Message: %@", [error localizedDescription]);
- MRLog(@"Error Domain: %@", [error domain]);
- MRLog(@"Recovery Suggestion: %@", [error localizedRecoverySuggestion]);
+ MRLogError(@"Error Message: %@", [error localizedDescription]);
+ MRLogError(@"Error Domain: %@", [error domain]);
+ MRLogError(@"Recovery Suggestion: %@", [error localizedRecoverySuggestion]);
}
+ (void) handleErrors:(NSError *)error
@@ -12,24 +12,26 @@
// enable to use caches for the fetchedResultsControllers (iOS only)
// #define STORE_USE_CACHE
-#ifndef MR_ENABLE_ACTIVE_RECORD_LOGGING
- #ifdef DEBUG
- #define MR_ENABLE_ACTIVE_RECORD_LOGGING 1
+// First, check if we can use Cocoalumberjack for logging
+#if defined(COCOAPODS_POD_AVAILABLE_CocoaLumberjack)
+ #import <CocoaLumberjack/DDLog.h>
+ extern int ddLogLevel;
+ #define MRLogError(...) DDLogError(__VA_ARGS__)
+ #define MRLogWarn(...) DDLogWarn(__VA_ARGS__)
+ #define MRLogInfo(...) DDLogInfo(__VA_ARGS__)
+ #define MRLogVerbose(...) DDLogVerbose(__VA_ARGS__)
+#else
+ // if not, only log in DEBUG
+ #if defined(DEBUG)
+ #define MRLog(...) NSLog(@"%s(%p) %@", __PRETTY_FUNCTION__, self, [NSString stringWithFormat:__VA_ARGS__])
#else
- #define MR_ENABLE_ACTIVE_RECORD_LOGGING 0
+ #define MRLog(...) ((void)0)
#endif
-#endif
-#if MR_ENABLE_ACTIVE_RECORD_LOGGING != 0
- // First, check if we can use Cocoalumberjack for logging
- #ifdef LOG_VERBOSE
- extern int ddLogLevel;
- #define MRLog(...) DDLogVerbose(__VA_ARGS__)
- #else
- #define MRLog(...) NSLog(@"%s(%p) %@", __PRETTY_FUNCTION__, self, [NSString stringWithFormat:__VA_ARGS__])
- #endif
-#else
- #define MRLog(...) ((void)0)
+ #define MRLogError(...) MRLog(__VA_ARGS__)
+ #define MRLogWarn(...) MRLog(__VA_ARGS__)
+ #define MRLogInfo(...) MRLog(__VA_ARGS__)
+ #define MRLogVerbose(...) MRLog(__VA_ARGS__)
#endif
#ifdef NS_BLOCKS_AVAILABLE
View
@@ -106,10 +106,6 @@ MagicalRecord provides a background saving queue so that saving all data is perf
## Logging
MagicalRecord has logging built in to every fetch request and other Core Data operation. When errors occur when fetching or saving data, these errors are captured by MagicalRecord. By default, these logs use NSLog to present logging information. However, if you have CocoaLumberjack installed in your project, MagicalRecord will use CocoaLumberjack and it's configuration to send logs to their proper output.
-All logging in MagicalRecord can be disabled by placing this define preprocessor statement prior to the main import of CoreData+MagicalRecord.h
-
- #define MR_ENABLE_ACTIVE_RECORD_LOGGING 0
-
# Usage
## Setting up the Core Data Stack

0 comments on commit 5568b3f

Please sign in to comment.