|
From: | Andreas Höschler |
Subject: | Re: Logging NSData |
Date: | Fri, 22 Jul 2011 17:03:16 +0200 |
Hi all, I am logging out NSDictionaries that sometimes contain NSData instances as values. On MacOSX NSData::description is called to determine a string that is used in the output. I don't want to see tons of hex values for large NSData objects in the output and therefore wrote the following category:@implementation NSData (SRDataExtension)- (NSString *)description{return [NSString stringWithFormat:@"<data of length %d>", [self length]];}@endThis works great on MacOSX but not under GNUstep. It seems that description is not called under GNUstep when a dictionary is logged out withNSLog(@"dic %@", dic);Any idea?GNUstep uses -descriptionWithLocale:Perhaps we should change that if it's no longer used in OSX?I have implemented- (NSString *)descriptionWithLocale:(id)locale{return [NSString stringWithFormat:@"<data of length %d>", [self length]];}no in my category, but this method isn't used either by NSLog(@"dic %@", dic)!? :-(I checked core/base/Source/NSData.m but it has no implementation for descriptionWithLocale: either!? The following modification in NSPropertyList.m solved my problem: else if ([obj isKindOfClass: [NSData class]]) { if (x == NSPropertyListXMLFormat_v1_0) { [dest appendBytes: "<data>\n" length: 7]; encodeBase64(obj, dest); [dest appendBytes: "</data>\n" length: 8]; } else { NSString *string = [obj description]; [dest appendData:[string dataUsingEncoding:NSASCIIStringEncoding]]; /* the code belows generates all the hey values, quit elog description const unsigned char *src; unsigned char *dst; int length; int i; int j; src = "" bytes]; length = [obj length]; #define num2char(num) ((num) < 0xa ? ((num)+'0') : ((num)+0x57)) j = [dest length]; [dest setLength: j + 2*length+(length > 4 ? (length-1)/4+2 : 2)]; dst = [dest mutableBytes]; dst[j++] = '<'; for (i = 0; i < length; i++, j++) { dst[j++] = num2char((src[i]>>4) & 0x0f); dst[j] = num2char(src[i] & 0x0f); if ((i & 3) == 3 && i < length-1) { // if we've just finished a 32-bit int, print a space dst[++j] = ' '; } } dst[j++] = '>'; */ } Thanks a lot!! Andreas |
[Prev in Thread] | Current Thread | [Next in Thread] |