Update debug logging in objc client

This commit is contained in:
geekerzp 2015-09-22 16:32:15 +08:00
parent 0622838947
commit 844cae75b7
5 changed files with 109 additions and 84 deletions

View File

@ -60,20 +60,26 @@ static void (^reachabilityChangeBlock)(int);
#pragma mark - Log Methods #pragma mark - Log Methods
- (void)logResponse:(AFHTTPRequestOperation *)operation + (void)debugLog:(NSString *)method
forRequest:(NSURLRequest *)request message:(NSString *)format, ... {
error:(NSError*)error {
{{classPrefix}}Configuration *config = [{{classPrefix}}Configuration sharedConfig]; {{classPrefix}}Configuration *config = [{{classPrefix}}Configuration sharedConfig];
if (!config.debug) { if (!config.debug) {
return; return;
} }
NSString *message = [NSString stringWithFormat:@"\n[DEBUG] Request body \n~BEGIN~\n %@\n~END~\n"\ NSMutableString *message = [NSMutableString stringWithCapacity:1];
"[DEBUG] HTTP Response body \n~BEGIN~\n %@\n~END~\n",
[[NSString alloc] initWithData:request.HTTPBody encoding:NSUTF8StringEncoding],
operation.responseString];
if (method) {
[message appendString:[NSString stringWithFormat:@"%@: ", method]];
}
va_list args;
va_start(args, format);
[message appendString:[[NSString alloc] initWithFormat:format arguments:args]];
// If set logging file handler, log into file,
// otherwise log into console.
if (config.loggingFileHanlder) { if (config.loggingFileHanlder) {
[config.loggingFileHanlder seekToEndOfFile]; [config.loggingFileHanlder seekToEndOfFile];
[config.loggingFileHanlder writeData:[message dataUsingEncoding:NSUTF8StringEncoding]]; [config.loggingFileHanlder writeData:[message dataUsingEncoding:NSUTF8StringEncoding]];
@ -81,6 +87,20 @@ static void (^reachabilityChangeBlock)(int);
else { else {
NSLog(@"%@", message); NSLog(@"%@", message);
} }
va_end(args);
}
- (void)logResponse:(AFHTTPRequestOperation *)operation
forRequest:(NSURLRequest *)request
error:(NSError*)error {
NSString *message = [NSString stringWithFormat:@"\n[DEBUG] HTTP request body \n~BEGIN~\n %@\n~END~\n"\
"[DEBUG] HTTP response body \n~BEGIN~\n %@\n~END~\n",
[[NSString alloc] initWithData:request.HTTPBody encoding:NSUTF8StringEncoding],
operation.responseString];
DebugLog(message);
} }
#pragma mark - Cache Methods #pragma mark - Cache Methods
@ -174,17 +194,14 @@ static void (^reachabilityChangeBlock)(int);
+(NSNumber*) nextRequestId { +(NSNumber*) nextRequestId {
@synchronized(self) { @synchronized(self) {
long nextId = ++requestId; long nextId = ++requestId;
if([[{{classPrefix}}Configuration sharedConfig] debug]) DebugLog(@"got id %ld", nextId);
NSLog(@"got id %ld", nextId);
return [NSNumber numberWithLong:nextId]; return [NSNumber numberWithLong:nextId];
} }
} }
+(NSNumber*) queueRequest { +(NSNumber*) queueRequest {
NSNumber* requestId = [{{classPrefix}}ApiClient nextRequestId]; NSNumber* requestId = [{{classPrefix}}ApiClient nextRequestId];
if([[{{classPrefix}}Configuration sharedConfig] debug]) { DebugLog(@"added %@ to request queue", requestId);
NSLog(@"added %@ to request queue", requestId);
}
[queuedRequests addObject:requestId]; [queuedRequests addObject:requestId];
return requestId; return requestId;
} }
@ -204,9 +221,7 @@ static void (^reachabilityChangeBlock)(int);
}]; }];
if(matchingItems.count == 1) { if(matchingItems.count == 1) {
if([[{{classPrefix}}Configuration sharedConfig] debug]){ DebugLog(@"removed request id %@", requestId);
NSLog(@"removing request id %@", requestId);
}
[queuedRequests removeObject:requestId]; [queuedRequests removeObject:requestId];
return YES; return YES;
} }
@ -230,26 +245,22 @@ static void (^reachabilityChangeBlock)(int);
reachabilityStatus = status; reachabilityStatus = status;
switch (status) { switch (status) {
case AFNetworkReachabilityStatusUnknown: case AFNetworkReachabilityStatusUnknown:
if([[{{classPrefix}}Configuration sharedConfig] debug]) DebugLog(@"reachability changed to AFNetworkReachabilityStatusUnknown");
NSLog(@"reachability changed to AFNetworkReachabilityStatusUnknown");
[{{classPrefix}}ApiClient setOfflineState:true]; [{{classPrefix}}ApiClient setOfflineState:true];
break; break;
case AFNetworkReachabilityStatusNotReachable: case AFNetworkReachabilityStatusNotReachable:
if([[{{classPrefix}}Configuration sharedConfig] debug]) DebugLog(@"reachability changed to AFNetworkReachabilityStatusNotReachable");
NSLog(@"reachability changed to AFNetworkReachabilityStatusNotReachable");
[{{classPrefix}}ApiClient setOfflineState:true]; [{{classPrefix}}ApiClient setOfflineState:true];
break; break;
case AFNetworkReachabilityStatusReachableViaWWAN: case AFNetworkReachabilityStatusReachableViaWWAN:
if([[{{classPrefix}}Configuration sharedConfig] debug]) DebugLog(@"reachability changed to AFNetworkReachabilityStatusReachableViaWWAN");
NSLog(@"reachability changed to AFNetworkReachabilityStatusReachableViaWWAN");
[{{classPrefix}}ApiClient setOfflineState:false]; [{{classPrefix}}ApiClient setOfflineState:false];
break; break;
case AFNetworkReachabilityStatusReachableViaWiFi: case AFNetworkReachabilityStatusReachableViaWiFi:
if([[{{classPrefix}}Configuration sharedConfig] debug]) DebugLog(@"reachability changed to AFNetworkReachabilityStatusReachableViaWiFi");
NSLog(@"reachability changed to AFNetworkReachabilityStatusReachableViaWiFi");
[{{classPrefix}}ApiClient setOfflineState:false]; [{{classPrefix}}ApiClient setOfflineState:false];
break; break;
default: default:
@ -397,9 +408,7 @@ static void (^reachabilityChangeBlock)(int);
AFHTTPRequestOperation *op = [self HTTPRequestOperationWithRequest:request AFHTTPRequestOperation *op = [self HTTPRequestOperationWithRequest:request
success:^(AFHTTPRequestOperation *operation, id response) { success:^(AFHTTPRequestOperation *operation, id response) {
if([self executeRequestWithId:requestId]) { if([self executeRequestWithId:requestId]) {
if([[{{classPrefix}}Configuration sharedConfig] debug]) {
[self logResponse:operation forRequest:request error:nil]; [self logResponse:operation forRequest:request error:nil];
}
NSDictionary *responseHeaders = [[operation response] allHeaderFields]; NSDictionary *responseHeaders = [[operation response] allHeaderFields];
self.HTTPResponseHeaders = responseHeaders; self.HTTPResponseHeaders = responseHeaders;
completionBlock(response, nil); completionBlock(response, nil);
@ -412,8 +421,6 @@ static void (^reachabilityChangeBlock)(int);
userInfo[{{classPrefix}}ResponseObjectErrorKey] = operation.responseObject; userInfo[{{classPrefix}}ResponseObjectErrorKey] = operation.responseObject;
} }
NSError *augmentedError = [error initWithDomain:error.domain code:error.code userInfo:userInfo]; NSError *augmentedError = [error initWithDomain:error.domain code:error.code userInfo:userInfo];
if([[{{classPrefix}}Configuration sharedConfig] debug])
[self logResponse:nil forRequest:request error:augmentedError]; [self logResponse:nil forRequest:request error:augmentedError];
NSDictionary *responseHeaders = [[operation response] allHeaderFields]; NSDictionary *responseHeaders = [[operation response] allHeaderFields];
@ -474,9 +481,9 @@ static void (^reachabilityChangeBlock)(int);
NSError *augmentedError = [error initWithDomain:error.domain code:error.code userInfo:userInfo]; NSError *augmentedError = [error initWithDomain:error.domain code:error.code userInfo:userInfo];
if ([[{{classPrefix}}Configuration sharedConfig] debug]) {
[self logResponse:nil forRequest:request error:augmentedError]; [self logResponse:nil forRequest:request error:augmentedError];
}
NSDictionary *responseHeaders = [[operation response] allHeaderFields]; NSDictionary *responseHeaders = [[operation response] allHeaderFields];
self.HTTPResponseHeaders = responseHeaders; self.HTTPResponseHeaders = responseHeaders;
completionBlock(nil, augmentedError); completionBlock(nil, augmentedError);
@ -584,21 +591,15 @@ static void (^reachabilityChangeBlock)(int);
hasHeaderParams = true; hasHeaderParams = true;
} }
if(offlineState) { if(offlineState) {
if ([[{{classPrefix}}Configuration sharedConfig] debug]){ DebugLog(@"%@ cache forced", resourcePath);
NSLog(@"%@ cache forced", resourcePath);
}
[request setCachePolicy:NSURLRequestReturnCacheDataDontLoad]; [request setCachePolicy:NSURLRequestReturnCacheDataDontLoad];
} }
else if(!hasHeaderParams && [method isEqualToString:@"GET"] && cacheEnabled) { else if(!hasHeaderParams && [method isEqualToString:@"GET"] && cacheEnabled) {
if ([[{{classPrefix}}Configuration sharedConfig] debug]){ DebugLog(@"%@ cache enabled", resourcePath);
NSLog(@"%@ cache enabled", resourcePath);
}
[request setCachePolicy:NSURLRequestUseProtocolCachePolicy]; [request setCachePolicy:NSURLRequestUseProtocolCachePolicy];
} }
else { else {
if ([[{{classPrefix}}Configuration sharedConfig] debug]){ DebugLog(@"%@ cache disabled", resourcePath);
NSLog(@"%@ cache disabled", resourcePath);
}
[request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData]; [request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
} }

View File

@ -24,6 +24,10 @@
*/ */
extern NSString *const {{classPrefix}}ResponseObjectErrorKey; extern NSString *const {{classPrefix}}ResponseObjectErrorKey;
/**
* Log debug message macro
*/
#define DebugLog(format, ...) [{{classPrefix}}ApiClient debugLog:[NSString stringWithFormat:@"%s", __PRETTY_FUNCTION__] message: format, ##__VA_ARGS__];
@interface {{classPrefix}}ApiClient : AFHTTPRequestOperationManager @interface {{classPrefix}}ApiClient : AFHTTPRequestOperationManager
@ -215,4 +219,9 @@ extern NSString *const {{classPrefix}}ResponseObjectErrorKey;
*/ */
- (NSString *) parameterToString: (id) param; - (NSString *) parameterToString: (id) param;
/**
* Log debug message
*/
+(void)debugLog:(NSString *)method message:(NSString *)format, ...;
@end @end

View File

@ -28,6 +28,10 @@
*/ */
extern NSString *const SWGResponseObjectErrorKey; extern NSString *const SWGResponseObjectErrorKey;
/**
* Log debug message macro
*/
#define DebugLog(format, ...) [SWGApiClient debugLog:[NSString stringWithFormat:@"%s", __PRETTY_FUNCTION__] message: format, ##__VA_ARGS__];
@interface SWGApiClient : AFHTTPRequestOperationManager @interface SWGApiClient : AFHTTPRequestOperationManager
@ -219,4 +223,9 @@ extern NSString *const SWGResponseObjectErrorKey;
*/ */
- (NSString *) parameterToString: (id) param; - (NSString *) parameterToString: (id) param;
/**
* Log debug message
*/
+(void)debugLog:(NSString *)method message:(NSString *)format, ...;
@end @end

View File

@ -60,20 +60,26 @@ static void (^reachabilityChangeBlock)(int);
#pragma mark - Log Methods #pragma mark - Log Methods
- (void)logResponse:(AFHTTPRequestOperation *)operation + (void)debugLog:(NSString *)method
forRequest:(NSURLRequest *)request message:(NSString *)format, ... {
error:(NSError*)error {
SWGConfiguration *config = [SWGConfiguration sharedConfig]; SWGConfiguration *config = [SWGConfiguration sharedConfig];
if (!config.debug) { if (!config.debug) {
return; return;
} }
NSString *message = [NSString stringWithFormat:@"\n[DEBUG] Request body \n~BEGIN~\n %@\n~END~\n"\ NSMutableString *message = [NSMutableString stringWithCapacity:1];
"[DEBUG] HTTP Response body \n~BEGIN~\n %@\n~END~\n",
[[NSString alloc] initWithData:request.HTTPBody encoding:NSUTF8StringEncoding],
operation.responseString];
if (method) {
[message appendString:[NSString stringWithFormat:@"%@: ", method]];
}
va_list args;
va_start(args, format);
[message appendString:[[NSString alloc] initWithFormat:format arguments:args]];
// If set logging file handler, log into file,
// otherwise log into console.
if (config.loggingFileHanlder) { if (config.loggingFileHanlder) {
[config.loggingFileHanlder seekToEndOfFile]; [config.loggingFileHanlder seekToEndOfFile];
[config.loggingFileHanlder writeData:[message dataUsingEncoding:NSUTF8StringEncoding]]; [config.loggingFileHanlder writeData:[message dataUsingEncoding:NSUTF8StringEncoding]];
@ -81,6 +87,20 @@ static void (^reachabilityChangeBlock)(int);
else { else {
NSLog(@"%@", message); NSLog(@"%@", message);
} }
va_end(args);
}
- (void)logResponse:(AFHTTPRequestOperation *)operation
forRequest:(NSURLRequest *)request
error:(NSError*)error {
NSString *message = [NSString stringWithFormat:@"\n[DEBUG] HTTP request body \n~BEGIN~\n %@\n~END~\n"\
"[DEBUG] HTTP response body \n~BEGIN~\n %@\n~END~\n",
[[NSString alloc] initWithData:request.HTTPBody encoding:NSUTF8StringEncoding],
operation.responseString];
DebugLog(message);
} }
#pragma mark - Cache Methods #pragma mark - Cache Methods
@ -174,17 +194,14 @@ static void (^reachabilityChangeBlock)(int);
+(NSNumber*) nextRequestId { +(NSNumber*) nextRequestId {
@synchronized(self) { @synchronized(self) {
long nextId = ++requestId; long nextId = ++requestId;
if([[SWGConfiguration sharedConfig] debug]) DebugLog(@"got id %ld", nextId);
NSLog(@"got id %ld", nextId);
return [NSNumber numberWithLong:nextId]; return [NSNumber numberWithLong:nextId];
} }
} }
+(NSNumber*) queueRequest { +(NSNumber*) queueRequest {
NSNumber* requestId = [SWGApiClient nextRequestId]; NSNumber* requestId = [SWGApiClient nextRequestId];
if([[SWGConfiguration sharedConfig] debug]) { DebugLog(@"added %@ to request queue", requestId);
NSLog(@"added %@ to request queue", requestId);
}
[queuedRequests addObject:requestId]; [queuedRequests addObject:requestId];
return requestId; return requestId;
} }
@ -204,9 +221,7 @@ static void (^reachabilityChangeBlock)(int);
}]; }];
if(matchingItems.count == 1) { if(matchingItems.count == 1) {
if([[SWGConfiguration sharedConfig] debug]){ DebugLog(@"removed request id %@", requestId);
NSLog(@"removing request id %@", requestId);
}
[queuedRequests removeObject:requestId]; [queuedRequests removeObject:requestId];
return YES; return YES;
} }
@ -230,26 +245,22 @@ static void (^reachabilityChangeBlock)(int);
reachabilityStatus = status; reachabilityStatus = status;
switch (status) { switch (status) {
case AFNetworkReachabilityStatusUnknown: case AFNetworkReachabilityStatusUnknown:
if([[SWGConfiguration sharedConfig] debug]) DebugLog(@"reachability changed to AFNetworkReachabilityStatusUnknown");
NSLog(@"reachability changed to AFNetworkReachabilityStatusUnknown");
[SWGApiClient setOfflineState:true]; [SWGApiClient setOfflineState:true];
break; break;
case AFNetworkReachabilityStatusNotReachable: case AFNetworkReachabilityStatusNotReachable:
if([[SWGConfiguration sharedConfig] debug]) DebugLog(@"reachability changed to AFNetworkReachabilityStatusNotReachable");
NSLog(@"reachability changed to AFNetworkReachabilityStatusNotReachable");
[SWGApiClient setOfflineState:true]; [SWGApiClient setOfflineState:true];
break; break;
case AFNetworkReachabilityStatusReachableViaWWAN: case AFNetworkReachabilityStatusReachableViaWWAN:
if([[SWGConfiguration sharedConfig] debug]) DebugLog(@"reachability changed to AFNetworkReachabilityStatusReachableViaWWAN");
NSLog(@"reachability changed to AFNetworkReachabilityStatusReachableViaWWAN");
[SWGApiClient setOfflineState:false]; [SWGApiClient setOfflineState:false];
break; break;
case AFNetworkReachabilityStatusReachableViaWiFi: case AFNetworkReachabilityStatusReachableViaWiFi:
if([[SWGConfiguration sharedConfig] debug]) DebugLog(@"reachability changed to AFNetworkReachabilityStatusReachableViaWiFi");
NSLog(@"reachability changed to AFNetworkReachabilityStatusReachableViaWiFi");
[SWGApiClient setOfflineState:false]; [SWGApiClient setOfflineState:false];
break; break;
default: default:
@ -397,9 +408,7 @@ static void (^reachabilityChangeBlock)(int);
AFHTTPRequestOperation *op = [self HTTPRequestOperationWithRequest:request AFHTTPRequestOperation *op = [self HTTPRequestOperationWithRequest:request
success:^(AFHTTPRequestOperation *operation, id response) { success:^(AFHTTPRequestOperation *operation, id response) {
if([self executeRequestWithId:requestId]) { if([self executeRequestWithId:requestId]) {
if([[SWGConfiguration sharedConfig] debug]) {
[self logResponse:operation forRequest:request error:nil]; [self logResponse:operation forRequest:request error:nil];
}
NSDictionary *responseHeaders = [[operation response] allHeaderFields]; NSDictionary *responseHeaders = [[operation response] allHeaderFields];
self.HTTPResponseHeaders = responseHeaders; self.HTTPResponseHeaders = responseHeaders;
completionBlock(response, nil); completionBlock(response, nil);
@ -412,8 +421,6 @@ static void (^reachabilityChangeBlock)(int);
userInfo[SWGResponseObjectErrorKey] = operation.responseObject; userInfo[SWGResponseObjectErrorKey] = operation.responseObject;
} }
NSError *augmentedError = [error initWithDomain:error.domain code:error.code userInfo:userInfo]; NSError *augmentedError = [error initWithDomain:error.domain code:error.code userInfo:userInfo];
if([[SWGConfiguration sharedConfig] debug])
[self logResponse:nil forRequest:request error:augmentedError]; [self logResponse:nil forRequest:request error:augmentedError];
NSDictionary *responseHeaders = [[operation response] allHeaderFields]; NSDictionary *responseHeaders = [[operation response] allHeaderFields];
@ -474,9 +481,9 @@ static void (^reachabilityChangeBlock)(int);
NSError *augmentedError = [error initWithDomain:error.domain code:error.code userInfo:userInfo]; NSError *augmentedError = [error initWithDomain:error.domain code:error.code userInfo:userInfo];
if ([[SWGConfiguration sharedConfig] debug]) {
[self logResponse:nil forRequest:request error:augmentedError]; [self logResponse:nil forRequest:request error:augmentedError];
}
NSDictionary *responseHeaders = [[operation response] allHeaderFields]; NSDictionary *responseHeaders = [[operation response] allHeaderFields];
self.HTTPResponseHeaders = responseHeaders; self.HTTPResponseHeaders = responseHeaders;
completionBlock(nil, augmentedError); completionBlock(nil, augmentedError);
@ -584,21 +591,15 @@ static void (^reachabilityChangeBlock)(int);
hasHeaderParams = true; hasHeaderParams = true;
} }
if(offlineState) { if(offlineState) {
if ([[SWGConfiguration sharedConfig] debug]){ DebugLog(@"%@ cache forced", resourcePath);
NSLog(@"%@ cache forced", resourcePath);
}
[request setCachePolicy:NSURLRequestReturnCacheDataDontLoad]; [request setCachePolicy:NSURLRequestReturnCacheDataDontLoad];
} }
else if(!hasHeaderParams && [method isEqualToString:@"GET"] && cacheEnabled) { else if(!hasHeaderParams && [method isEqualToString:@"GET"] && cacheEnabled) {
if ([[SWGConfiguration sharedConfig] debug]){ DebugLog(@"%@ cache enabled", resourcePath);
NSLog(@"%@ cache enabled", resourcePath);
}
[request setCachePolicy:NSURLRequestUseProtocolCachePolicy]; [request setCachePolicy:NSURLRequestUseProtocolCachePolicy];
} }
else { else {
if ([[SWGConfiguration sharedConfig] debug]){ DebugLog(@"%@ cache disabled", resourcePath);
NSLog(@"%@ cache disabled", resourcePath);
}
[request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData]; [request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
} }

View File

@ -2,6 +2,11 @@
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0"> <plist version="1.0">
<dict> <dict>
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
<key>CFBundleDevelopmentRegion</key> <key>CFBundleDevelopmentRegion</key>
<string>en</string> <string>en</string>
<key>CFBundleDisplayName</key> <key>CFBundleDisplayName</key>