Use NSLog consistently for debug output

git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@34920 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
Richard Frith-MacDonald 2012-03-11 11:00:08 +00:00
parent 87c3b97c1c
commit 6908b4724d
5 changed files with 53 additions and 60 deletions

View file

@ -1,3 +1,12 @@
2012-03-11 Richard Frith-Macdonald <rfm@gnu.org>
* Source/NSURLHandle.m:
* Source/GSHTTPURLHandle.m:
* Source/NSURLProtocol.m:
* Headers/Foundation/NSURLProtocol.h:
Tweak handling of debug settings to consistently write debug output
using NSLog.
2012-03-09 Fred Kiefer <FredKiefer@gmx.de> 2012-03-09 Fred Kiefer <FredKiefer@gmx.de>
* Source/NSXMLNode.m (-copyWithZone:): Don't set object value of * Source/NSXMLNode.m (-copyWithZone:): Don't set object value of

View file

@ -113,7 +113,7 @@ extern "C" {
* structure for the subclasses. * structure for the subclasses.
* </p> * </p>
* <p>You never instantiate NSURLProtocol yourself ... it should only * <p>You never instantiate NSURLProtocol yourself ... it should only
* ever be done by other classes within mthe URL loading system. * ever be done by other classes within the URL loading system.
* </p> * </p>
*/ */
@interface NSURLProtocol : NSObject @interface NSURLProtocol : NSObject

View file

@ -228,56 +228,21 @@ static NSLock *urlLock = nil;
static Class sslClass = 0; static Class sslClass = 0;
static NSLock *debugLock = nil; static void
static NSString *debugFile; debugRead(GSHTTPURLHandle *handle, NSData *data)
static void debugRead(GSHTTPURLHandle *handle, NSData *data)
{ {
NSString *s; int len = (int)[data length];
int d; const char *ptr = (const char*)[data bytes];
[debugLock lock]; NSLog(@"Read for %p of %d bytes -'%*.*s'", handle, len, len, len, ptr);
#if defined(__MINGW__)
d = _wopen((const unichar*)[debugFile fileSystemRepresentation],
O_WRONLY|O_CREAT|O_APPEND, 0644);
#else
d = open([debugFile fileSystemRepresentation],
O_WRONLY|O_CREAT|O_APPEND, 0644);
#endif
if (d >= 0)
{
s = [NSString stringWithFormat: @"\nRead for %p at %@ %u bytes - '",
handle, [NSDate date], [data length]];
write(d, [s cString], [s cStringLength]);
write(d, [data bytes], [data length]);
write(d, "'", 1);
close(d);
}
[debugLock unlock];
} }
static void debugWrite(GSHTTPURLHandle *handle, NSData *data) static void
debugWrite(GSHTTPURLHandle *handle, NSData *data)
{ {
NSString *s; int len = (int)[data length];
int d; const char *ptr = (const char*)[data bytes];
[debugLock lock]; NSLog(@"Write for %p of %d bytes -'%*.*s'", handle, len, len, len, ptr);
#if defined(__MINGW__)
d = _wopen((const unichar*)[debugFile fileSystemRepresentation],
O_WRONLY|O_CREAT|O_APPEND, 0644);
#else
d = open([debugFile fileSystemRepresentation],
O_WRONLY|O_CREAT|O_APPEND, 0644);
#endif
if (d >= 0)
{
s = [NSString stringWithFormat: @"\nWrite for %p at %@ %u bytes - '",
handle, [NSDate date], [data length]];
write(d, [s cString], [s cStringLength]);
write(d, [data bytes], [data length]);
write(d, "'", 1);
close(d);
}
[debugLock unlock];
} }
+ (NSURLHandle*) cachedHandleForURL: (NSURL*)newUrl + (NSURLHandle*) cachedHandleForURL: (NSURL*)newUrl
@ -310,12 +275,6 @@ static void debugWrite(GSHTTPURLHandle *handle, NSData *data)
urlCache = [NSMutableDictionary new]; urlCache = [NSMutableDictionary new];
urlOrder = [NSMutableArray new]; urlOrder = [NSMutableArray new];
urlLock = [GSLazyLock new]; urlLock = [GSLazyLock new];
debugLock = [GSLazyLock new];
debugFile = [NSString stringWithFormat: @"%@/GSHTTP.%d",
NSTemporaryDirectory(),
[[NSProcessInfo processInfo] processIdentifier]];
IF_NO_GC([debugFile retain];)
#if !defined(__MINGW__) #if !defined(__MINGW__)
sslClass = [NSFileHandle sslClass]; sslClass = [NSFileHandle sslClass];
#endif #endif
@ -352,6 +311,7 @@ static void debugWrite(GSHTTPURLHandle *handle, NSData *data)
{ {
if ((self = [super initWithURL: newUrl cached: cached]) != nil) if ((self = [super initWithURL: newUrl cached: cached]) != nil)
{ {
debug = GSDebugSet(@"NSURLHandle");
dat = [NSMutableData new]; dat = [NSMutableData new];
pageInfo = [NSMutableDictionary new]; pageInfo = [NSMutableDictionary new];
wProperties = NSCreateMapTable(writeKeyCallBacks, wProperties = NSCreateMapTable(writeKeyCallBacks,
@ -554,7 +514,7 @@ static void debugWrite(GSHTTPURLHandle *handle, NSData *data)
/* /*
* Send request to server. * Send request to server.
*/ */
if (debug == YES) debugWrite(self, buf); if (YES == debug) debugWrite(self, buf);
[sock writeInBackgroundAndNotify: buf]; [sock writeInBackgroundAndNotify: buf];
RELEASE(buf); RELEASE(buf);
RELEASE(s); RELEASE(s);
@ -574,7 +534,7 @@ static void debugWrite(GSHTTPURLHandle *handle, NSData *data)
if (debug) if (debug)
NSLog(@"%@ %p %s", NSStringFromSelector(_cmd), self, keepalive?"K":""); NSLog(@"%@ %p %s", NSStringFromSelector(_cmd), self, keepalive?"K":"");
d = [dict objectForKey: NSFileHandleNotificationDataItem]; d = [dict objectForKey: NSFileHandleNotificationDataItem];
if (debug == YES) debugRead(self, d); if (YES == debug) debugRead(self, d);
readCount = [d length]; readCount = [d length];
if (connectionState == idle) if (connectionState == idle)
@ -584,7 +544,7 @@ static void debugWrite(GSHTTPURLHandle *handle, NSData *data)
* it should just be the connection being closed by the other * it should just be the connection being closed by the other
* end because of a timeout etc. * end because of a timeout etc.
*/ */
if (debug == YES && [d length] != 0) if (YES == debug && [d length] != 0)
{ {
NSLog(@"%@ %p %s Unexpected data (%*.*s) from remote!", NSLog(@"%@ %p %s Unexpected data (%*.*s) from remote!",
NSStringFromSelector(_cmd), self, keepalive?"K":"", NSStringFromSelector(_cmd), self, keepalive?"K":"",
@ -596,7 +556,7 @@ static void debugWrite(GSHTTPURLHandle *handle, NSData *data)
} }
else if ([parser parse: d] == NO && [parser isComplete] == NO) else if ([parser parse: d] == NO && [parser isComplete] == NO)
{ {
if (debug == YES) if (YES == debug)
{ {
NSLog(@"HTTP parse failure - %@", parser); NSLog(@"HTTP parse failure - %@", parser);
} }
@ -826,7 +786,7 @@ static void debugWrite(GSHTTPURLHandle *handle, NSData *data)
* lost in the network or the remote end received it and * lost in the network or the remote end received it and
* the response was lost. * the response was lost.
*/ */
if (debug == YES) if (YES == debug)
{ {
NSLog(@"HTTP response not received - %@", parser); NSLog(@"HTTP response not received - %@", parser);
} }
@ -856,7 +816,7 @@ static void debugWrite(GSHTTPURLHandle *handle, NSData *data)
if (debug) if (debug)
NSLog(@"%@ %p %s", NSStringFromSelector(_cmd), self, keepalive?"K":""); NSLog(@"%@ %p %s", NSStringFromSelector(_cmd), self, keepalive?"K":"");
d = [dict objectForKey: NSFileHandleNotificationDataItem]; d = [dict objectForKey: NSFileHandleNotificationDataItem];
if (debug == YES) debugRead(self, d); if (YES == debug) debugRead(self, d);
if ([d length] > 0) if ([d length] > 0)
{ {
@ -1012,7 +972,7 @@ static void debugWrite(GSHTTPURLHandle *handle, NSData *data)
object: sock]; object: sock];
buf = [cmd dataUsingEncoding: NSASCIIStringEncoding]; buf = [cmd dataUsingEncoding: NSASCIIStringEncoding];
if (debug == YES) debugWrite(self, buf); if (YES == debug) debugWrite(self, buf);
[sock writeInBackgroundAndNotify: buf]; [sock writeInBackgroundAndNotify: buf];
when = [NSDate alloc]; when = [NSDate alloc];

View file

@ -548,6 +548,14 @@ static Class NSURLHandleClass = 0;
return [self availableResourceData]; return [self availableResourceData];
} }
/* Private method ... subclasses override this to enable debug to be
* turned off and on.
*/
- (void) setDebug: (BOOL)aFlag
{
return;
}
/** /**
* Returns the current status of the handle. * Returns the current status of the handle.
*/ */

View file

@ -536,6 +536,14 @@ static NSURLProtocol *placeholder = nil;
return this->request; return this->request;
} }
/* This method is here so that it's safe to set debug on any NSURLProtocol
* even if the concrete subclass doesn't actually support debug logging.
*/
- (void) setDebug: (BOOL)flag
{
return;
}
@end @end
@ -615,6 +623,15 @@ static NSURLProtocol *placeholder = nil;
[super dealloc]; [super dealloc];
} }
- (id) init
{
if (nil != (self = [super init]))
{
_debug = GSDebugSet(@"NSURLProtocol");
}
return self;
}
- (void) setDebug: (BOOL)flag - (void) setDebug: (BOOL)flag
{ {
_debug = flag; _debug = flag;
@ -656,7 +673,6 @@ static NSURLProtocol *placeholder = nil;
_statusCode = 0; /* No status returned yet. */ _statusCode = 0; /* No status returned yet. */
_isLoading = YES; _isLoading = YES;
_complete = NO; _complete = NO;
_debug = GSDebugSet(@"NSHTTPURLProtocol");
/* Perform a redirect if the path is empty. /* Perform a redirect if the path is empty.
* As per MacOs-X documentation. * As per MacOs-X documentation.