From e287d086e2f06356ca77cab0862f5d4e5a9c5e72 Mon Sep 17 00:00:00 2001 From: Andrew Barnert Date: Fri, 5 Oct 2012 11:52:58 -0700 Subject: [PATCH] Added a way to truncate descriptions of long data descriptors. - Added one method, `+[AEMObjectRenderer setDataDescriptionTruncation:]`. When set to `maxBytes`, this affects the description of any descriptor that describes as hex bytes, so that only the first `maxBytes` will be displayed, followed by an ellipsis. - This is primarily useful when debugging AE interfaces that send large hunks of binary data around, such as iTunes cover art. The first few bytes of the artwork's data object are enough to tell whether you're getting a PNG or a PICT; the remaining 200K chars do nothing but make the logs harder to read and search. - Since the data descriptor may be embedded in some other object, the most reasonable place to put the truncation is inside its `description` (or, rather, in `+[formatObject:indent:result:]`, the recursive function that does the actual formatting underlying `description`). - Truncation is off by default, because sometimes you _do_ want all 200K (e.g., so you can parse the logs, unhexlify the bytes, save them as a file, and see if they're the PICT you expected). --- .../trunk/src/Appscript/objectrenderer.h | 2 ++ .../trunk/src/Appscript/objectrenderer.m | 26 +++++++++++++++++-- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/objc-appscript/trunk/src/Appscript/objectrenderer.h b/objc-appscript/trunk/src/Appscript/objectrenderer.h index dfe840a..36c7f13 100644 --- a/objc-appscript/trunk/src/Appscript/objectrenderer.h +++ b/objc-appscript/trunk/src/Appscript/objectrenderer.h @@ -12,5 +12,7 @@ +(NSString *)formatObject:(id)obj; ++(void)setDataDescriptionTruncation:(NSUInteger)maxBytes; + @end diff --git a/objc-appscript/trunk/src/Appscript/objectrenderer.m b/objc-appscript/trunk/src/Appscript/objectrenderer.m index 13de80d..4765b50 100644 --- a/objc-appscript/trunk/src/Appscript/objectrenderer.m +++ b/objc-appscript/trunk/src/Appscript/objectrenderer.m @@ -5,6 +5,7 @@ #import "objectrenderer.h" +static NSUInteger dataDescriptionTruncation = (NSUInteger)-1; @implementation AEMObjectRenderer @@ -103,8 +104,26 @@ +(void)formatObject:(id)obj indent:(NSString *)indent result:(NSMutableString *) [self formatObject: [obj description] indent: @"" result: result]; [result appendString: @"]"]; - } else - [result appendFormat: @"%@", obj]; + } else if ([obj isKindOfClass:[NSAppleEventDescriptor class]]) { + NSString *s = [obj description]; + if ([s hasPrefix:@" dataDescriptionTruncation) { + s = [NSString stringWithFormat:@"%@%d:$%@...$%@", + [bits objectAtIndex:0], + (int)length / 2, + [d substringToIndex:dataDescriptionTruncation * 2], + [bits objectAtIndex:2]]; + } + } + } + [result appendString:s]; + } else { + [result appendFormat:@"%@", obj]; + } } @@ -117,5 +136,8 @@ +(NSString *)formatObject:(id)obj { return result; } ++(void)setDataDescriptionTruncation:(NSUInteger)maxBytes { + dataDescriptionTruncation = maxBytes; +} @end