Skip to content

Commit

Permalink
Adding some debug feedback for an issue that showed up with inverse r…
Browse files Browse the repository at this point in the history
…elationships
  • Loading branch information
Conrad Stoll committed Jun 26, 2014
1 parent 24b201e commit 3c36cdf
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 22 deletions.
14 changes: 14 additions & 0 deletions Source/MMRecord/MMRecordDebugger.h
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,20 @@ typedef NS_ENUM(NSInteger, MMRecordErrorCode) {
- (void)logMessageWithDescription:(NSString *)description
minimumLoggingLevel:(MMRecordLoggingLevel)loggingLevel;

/**
This method can be used to log a helpful message through the debugger.
This message will be handled in a stateless fashion, and will leverage the current logging level
set on MMRecord itself.
@param description The message you want to display in the log.
@param loggingLevel The minimum logging level for the log message.
@warning Messages sent to this method will ONLY be logged if the logging level from MMRecord is
set to MMRecordLoggingLevelAll.
@discussion This method will only log messages when the logging level is set to ALL in order to
prevent excessive messages being sent to the console, and also to ensure that only the highest
level of logging will include messages sent in a stateless fashion.
*/
+ (void)logMessageWithDescription:(NSString *)description;

/**
This is the main error that is associated with this debugger instance. This will be the error
that gets shown in an MMRecord failureBlock.
Expand Down
60 changes: 38 additions & 22 deletions Source/MMRecord/MMRecordDebugger.m
Original file line number Diff line number Diff line change
Expand Up @@ -119,29 +119,38 @@ - (void)logMessageWithDescription:(NSString *)description
minimumLoggingLevel:(MMRecordLoggingLevel)loggingLevel
failureCondition:(BOOL)failureCondition
informationMessage:(BOOL)informationMessage {
BOOL shouldPerformLog = [self shouldPerformLogForLoggingLevel:loggingLevel];
BOOL shouldPerformLog = [[self class] shouldPerformLogForLoggingLevel:loggingLevel
currentLevel:self.loggingLevel];

if (shouldPerformLog) {
[[self class] logMessageWithDescription:description
failureCondition:failureCondition
informationMessage:informationMessage];
}
}

+ (void)logMessageWithDescription:(NSString *)description
failureCondition:(BOOL)failureCondition
informationMessage:(BOOL)informationMessage {
#if MMRecordLumberjack
if (failureCondition) {
MMRLogError(@"%@.", description);
} else if (informationMessage) {
MMRLogInfo(@"%@.", description);
} else {
MMRLogWarn(@"%@.", description);
}
if (failureCondition) {
MMRLogError(@"%@.", description);
} else if (informationMessage) {
MMRLogInfo(@"%@.", description);
} else {
MMRLogWarn(@"%@.", description);
}
#else
NSString *logPrefix = @"--[MMRecord WARNING]-- %@.";

if (failureCondition) {
logPrefix = @"--[MMRecord ERROR]-- %@.";
} else if (informationMessage) {
logPrefix = @"--[MMRecord INFO]-- %@.";
}

NSLog(logPrefix, description);
#endif
NSString *logPrefix = @"--[MMRecord WARNING]-- %@.";

if (failureCondition) {
logPrefix = @"--[MMRecord ERROR]-- %@.";
} else if (informationMessage) {
logPrefix = @"--[MMRecord INFO]-- %@.";
}

NSLog(logPrefix, description);
#endif
}

- (MMRecordLoggingLevel)loggingLevelForErrorCode:(MMRecordErrorCode)errorCode {
Expand All @@ -161,10 +170,8 @@ - (MMRecordLoggingLevel)loggingLevelForErrorCode:(MMRecordErrorCode)errorCode {
return MMRecordLoggingLevelAll;
}

- (BOOL)shouldPerformLogForLoggingLevel:(MMRecordLoggingLevel)loggingLevel {
MMRecordLoggingLevel currentLevel = self.loggingLevel;
MMRecordLoggingLevel minimumLevel = loggingLevel;

+ (BOOL)shouldPerformLogForLoggingLevel:(MMRecordLoggingLevel)minimumLevel
currentLevel:(MMRecordLoggingLevel)currentLevel {
// Any logging level equal to or lower than the current level should be logged.
if (currentLevel >= minimumLevel &&
currentLevel > MMRecordLoggingLevelNone) {
Expand Down Expand Up @@ -213,6 +220,15 @@ - (void)logMessageForCode:(MMRecordErrorCode)errorCode description:(NSString *)d
informationMessage:NO];
}

+ (void)logMessageWithDescription:(NSString *)description {
BOOL shouldPerformLog = [self shouldPerformLogForLoggingLevel:MMRecordLoggingLevelAll
currentLevel:[MMRecord loggingLevel]];

if (shouldPerformLog) {
[self logMessageWithDescription:description failureCondition:NO informationMessage:YES];
}
}

- (NSString *)descriptionForErrorCode:(MMRecordErrorCode)errorCode {
NSString *result = nil;
switch (errorCode) {
Expand Down
6 changes: 6 additions & 0 deletions Source/MMRecord/MMRecordMarshaler.m
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#import "MMRecordMarshaler.h"

#import "MMRecord.h"
#import "MMRecordDebugger.h"
#import "MMRecordProtoRecord.h"
#import "MMRecordRepresentation.h"

Expand Down Expand Up @@ -176,6 +177,11 @@ + (void)establishRelationship:(NSRelationshipDescription *)relationship
if ([relationship isToMany]) {
[self establishToManyRelationship:relationship fromRecord:fromRecord toRecord:toRecord];
} else {
if (relationship.inverseRelationship != nil && [fromRecord valueForKey:[relationship name]] != nil) {
[MMRecordDebugger logMessageWithDescription:
[NSString stringWithFormat:@"Replacing existing value may invalidate an inverse relationship."]];
}

[fromRecord setValue:toRecord forKey:[relationship name]];
}
}
Expand Down

0 comments on commit 3c36cdf

Please sign in to comment.