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
- (void)logResponse:(AFHTTPRequestOperation *)operation
forRequest:(NSURLRequest *)request
error:(NSError*)error {
+ (void)debugLog:(NSString *)method
message:(NSString *)format, ... {
{{classPrefix}}Configuration *config = [{{classPrefix}}Configuration sharedConfig];
if (!config.debug) {
return;
}
NSString *message = [NSString stringWithFormat:@"\n[DEBUG] 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];
NSMutableString *message = [NSMutableString stringWithCapacity:1];
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) {
[config.loggingFileHanlder seekToEndOfFile];
[config.loggingFileHanlder writeData:[message dataUsingEncoding:NSUTF8StringEncoding]];
@ -81,6 +87,20 @@ static void (^reachabilityChangeBlock)(int);
else {
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
@ -174,17 +194,14 @@ static void (^reachabilityChangeBlock)(int);
+(NSNumber*) nextRequestId {
@synchronized(self) {
long nextId = ++requestId;
if([[{{classPrefix}}Configuration sharedConfig] debug])
NSLog(@"got id %ld", nextId);
DebugLog(@"got id %ld", nextId);
return [NSNumber numberWithLong:nextId];
}
}
+(NSNumber*) queueRequest {
NSNumber* requestId = [{{classPrefix}}ApiClient nextRequestId];
if([[{{classPrefix}}Configuration sharedConfig] debug]) {
NSLog(@"added %@ to request queue", requestId);
}
DebugLog(@"added %@ to request queue", requestId);
[queuedRequests addObject:requestId];
return requestId;
}
@ -204,9 +221,7 @@ static void (^reachabilityChangeBlock)(int);
}];
if(matchingItems.count == 1) {
if([[{{classPrefix}}Configuration sharedConfig] debug]){
NSLog(@"removing request id %@", requestId);
}
DebugLog(@"removed request id %@", requestId);
[queuedRequests removeObject:requestId];
return YES;
}
@ -230,26 +245,22 @@ static void (^reachabilityChangeBlock)(int);
reachabilityStatus = status;
switch (status) {
case AFNetworkReachabilityStatusUnknown:
if([[{{classPrefix}}Configuration sharedConfig] debug])
NSLog(@"reachability changed to AFNetworkReachabilityStatusUnknown");
DebugLog(@"reachability changed to AFNetworkReachabilityStatusUnknown");
[{{classPrefix}}ApiClient setOfflineState:true];
break;
case AFNetworkReachabilityStatusNotReachable:
if([[{{classPrefix}}Configuration sharedConfig] debug])
NSLog(@"reachability changed to AFNetworkReachabilityStatusNotReachable");
DebugLog(@"reachability changed to AFNetworkReachabilityStatusNotReachable");
[{{classPrefix}}ApiClient setOfflineState:true];
break;
case AFNetworkReachabilityStatusReachableViaWWAN:
if([[{{classPrefix}}Configuration sharedConfig] debug])
NSLog(@"reachability changed to AFNetworkReachabilityStatusReachableViaWWAN");
DebugLog(@"reachability changed to AFNetworkReachabilityStatusReachableViaWWAN");
[{{classPrefix}}ApiClient setOfflineState:false];
break;
case AFNetworkReachabilityStatusReachableViaWiFi:
if([[{{classPrefix}}Configuration sharedConfig] debug])
NSLog(@"reachability changed to AFNetworkReachabilityStatusReachableViaWiFi");
DebugLog(@"reachability changed to AFNetworkReachabilityStatusReachableViaWiFi");
[{{classPrefix}}ApiClient setOfflineState:false];
break;
default:
@ -397,9 +408,7 @@ static void (^reachabilityChangeBlock)(int);
AFHTTPRequestOperation *op = [self HTTPRequestOperationWithRequest:request
success:^(AFHTTPRequestOperation *operation, id response) {
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];
self.HTTPResponseHeaders = responseHeaders;
completionBlock(response, nil);
@ -412,9 +421,7 @@ static void (^reachabilityChangeBlock)(int);
userInfo[{{classPrefix}}ResponseObjectErrorKey] = operation.responseObject;
}
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];
self.HTTPResponseHeaders = responseHeaders;
@ -474,9 +481,9 @@ static void (^reachabilityChangeBlock)(int);
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];
self.HTTPResponseHeaders = responseHeaders;
completionBlock(nil, augmentedError);
@ -584,21 +591,15 @@ static void (^reachabilityChangeBlock)(int);
hasHeaderParams = true;
}
if(offlineState) {
if ([[{{classPrefix}}Configuration sharedConfig] debug]){
NSLog(@"%@ cache forced", resourcePath);
}
DebugLog(@"%@ cache forced", resourcePath);
[request setCachePolicy:NSURLRequestReturnCacheDataDontLoad];
}
else if(!hasHeaderParams && [method isEqualToString:@"GET"] && cacheEnabled) {
if ([[{{classPrefix}}Configuration sharedConfig] debug]){
NSLog(@"%@ cache enabled", resourcePath);
}
DebugLog(@"%@ cache enabled", resourcePath);
[request setCachePolicy:NSURLRequestUseProtocolCachePolicy];
}
else {
if ([[{{classPrefix}}Configuration sharedConfig] debug]){
NSLog(@"%@ cache disabled", resourcePath);
}
DebugLog(@"%@ cache disabled", resourcePath);
[request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
}

View File

@ -24,6 +24,10 @@
*/
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
@ -215,4 +219,9 @@ extern NSString *const {{classPrefix}}ResponseObjectErrorKey;
*/
- (NSString *) parameterToString: (id) param;
/**
* Log debug message
*/
+(void)debugLog:(NSString *)method message:(NSString *)format, ...;
@end

View File

@ -28,6 +28,10 @@
*/
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
@ -219,4 +223,9 @@ extern NSString *const SWGResponseObjectErrorKey;
*/
- (NSString *) parameterToString: (id) param;
/**
* Log debug message
*/
+(void)debugLog:(NSString *)method message:(NSString *)format, ...;
@end

View File

@ -60,20 +60,26 @@ static void (^reachabilityChangeBlock)(int);
#pragma mark - Log Methods
- (void)logResponse:(AFHTTPRequestOperation *)operation
forRequest:(NSURLRequest *)request
error:(NSError*)error {
+ (void)debugLog:(NSString *)method
message:(NSString *)format, ... {
SWGConfiguration *config = [SWGConfiguration sharedConfig];
if (!config.debug) {
return;
}
NSString *message = [NSString stringWithFormat:@"\n[DEBUG] 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];
NSMutableString *message = [NSMutableString stringWithCapacity:1];
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) {
[config.loggingFileHanlder seekToEndOfFile];
[config.loggingFileHanlder writeData:[message dataUsingEncoding:NSUTF8StringEncoding]];
@ -81,6 +87,20 @@ static void (^reachabilityChangeBlock)(int);
else {
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
@ -174,17 +194,14 @@ static void (^reachabilityChangeBlock)(int);
+(NSNumber*) nextRequestId {
@synchronized(self) {
long nextId = ++requestId;
if([[SWGConfiguration sharedConfig] debug])
NSLog(@"got id %ld", nextId);
DebugLog(@"got id %ld", nextId);
return [NSNumber numberWithLong:nextId];
}
}
+(NSNumber*) queueRequest {
NSNumber* requestId = [SWGApiClient nextRequestId];
if([[SWGConfiguration sharedConfig] debug]) {
NSLog(@"added %@ to request queue", requestId);
}
DebugLog(@"added %@ to request queue", requestId);
[queuedRequests addObject:requestId];
return requestId;
}
@ -204,9 +221,7 @@ static void (^reachabilityChangeBlock)(int);
}];
if(matchingItems.count == 1) {
if([[SWGConfiguration sharedConfig] debug]){
NSLog(@"removing request id %@", requestId);
}
DebugLog(@"removed request id %@", requestId);
[queuedRequests removeObject:requestId];
return YES;
}
@ -230,26 +245,22 @@ static void (^reachabilityChangeBlock)(int);
reachabilityStatus = status;
switch (status) {
case AFNetworkReachabilityStatusUnknown:
if([[SWGConfiguration sharedConfig] debug])
NSLog(@"reachability changed to AFNetworkReachabilityStatusUnknown");
DebugLog(@"reachability changed to AFNetworkReachabilityStatusUnknown");
[SWGApiClient setOfflineState:true];
break;
case AFNetworkReachabilityStatusNotReachable:
if([[SWGConfiguration sharedConfig] debug])
NSLog(@"reachability changed to AFNetworkReachabilityStatusNotReachable");
DebugLog(@"reachability changed to AFNetworkReachabilityStatusNotReachable");
[SWGApiClient setOfflineState:true];
break;
case AFNetworkReachabilityStatusReachableViaWWAN:
if([[SWGConfiguration sharedConfig] debug])
NSLog(@"reachability changed to AFNetworkReachabilityStatusReachableViaWWAN");
DebugLog(@"reachability changed to AFNetworkReachabilityStatusReachableViaWWAN");
[SWGApiClient setOfflineState:false];
break;
case AFNetworkReachabilityStatusReachableViaWiFi:
if([[SWGConfiguration sharedConfig] debug])
NSLog(@"reachability changed to AFNetworkReachabilityStatusReachableViaWiFi");
DebugLog(@"reachability changed to AFNetworkReachabilityStatusReachableViaWiFi");
[SWGApiClient setOfflineState:false];
break;
default:
@ -397,9 +408,7 @@ static void (^reachabilityChangeBlock)(int);
AFHTTPRequestOperation *op = [self HTTPRequestOperationWithRequest:request
success:^(AFHTTPRequestOperation *operation, id response) {
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];
self.HTTPResponseHeaders = responseHeaders;
completionBlock(response, nil);
@ -412,9 +421,7 @@ static void (^reachabilityChangeBlock)(int);
userInfo[SWGResponseObjectErrorKey] = operation.responseObject;
}
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];
self.HTTPResponseHeaders = responseHeaders;
@ -474,9 +481,9 @@ static void (^reachabilityChangeBlock)(int);
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];
self.HTTPResponseHeaders = responseHeaders;
completionBlock(nil, augmentedError);
@ -584,21 +591,15 @@ static void (^reachabilityChangeBlock)(int);
hasHeaderParams = true;
}
if(offlineState) {
if ([[SWGConfiguration sharedConfig] debug]){
NSLog(@"%@ cache forced", resourcePath);
}
DebugLog(@"%@ cache forced", resourcePath);
[request setCachePolicy:NSURLRequestReturnCacheDataDontLoad];
}
else if(!hasHeaderParams && [method isEqualToString:@"GET"] && cacheEnabled) {
if ([[SWGConfiguration sharedConfig] debug]){
NSLog(@"%@ cache enabled", resourcePath);
}
DebugLog(@"%@ cache enabled", resourcePath);
[request setCachePolicy:NSURLRequestUseProtocolCachePolicy];
}
else {
if ([[SWGConfiguration sharedConfig] debug]){
NSLog(@"%@ cache disabled", resourcePath);
}
DebugLog(@"%@ cache disabled", resourcePath);
[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">
<plist version="1.0">
<dict>
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
<key>CFBundleDevelopmentRegion</key>
<string>en</string>
<key>CFBundleDisplayName</key>