Merge pull request #1274 from geekerzp/objc_log

[Objc] Update objc debug logging
This commit is contained in:
wing328
2015-09-25 11:57:28 +08:00
5 changed files with 118 additions and 77 deletions

View File

@@ -60,24 +60,47 @@ 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]];
}
if ([[{{classPrefix}}Configuration sharedConfig] debug]){
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];
{{classPrefix}}DebugLog(message);
}
#pragma mark - Cache Methods
@@ -171,17 +194,14 @@ static void (^reachabilityChangeBlock)(int);
+(NSNumber*) nextRequestId {
@synchronized(self) {
long nextId = ++requestId;
if([[{{classPrefix}}Configuration sharedConfig] debug])
NSLog(@"got id %ld", nextId);
{{classPrefix}}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);
}
{{classPrefix}}DebugLog(@"added %@ to request queue", requestId);
[queuedRequests addObject:requestId];
return requestId;
}
@@ -201,9 +221,7 @@ static void (^reachabilityChangeBlock)(int);
}];
if(matchingItems.count == 1) {
if([[{{classPrefix}}Configuration sharedConfig] debug]){
NSLog(@"removing request id %@", requestId);
}
{{classPrefix}}DebugLog(@"removed request id %@", requestId);
[queuedRequests removeObject:requestId];
return YES;
}
@@ -227,26 +245,22 @@ static void (^reachabilityChangeBlock)(int);
reachabilityStatus = status;
switch (status) {
case AFNetworkReachabilityStatusUnknown:
if([[{{classPrefix}}Configuration sharedConfig] debug])
NSLog(@"reachability changed to AFNetworkReachabilityStatusUnknown");
{{classPrefix}}DebugLog(@"reachability changed to AFNetworkReachabilityStatusUnknown");
[{{classPrefix}}ApiClient setOfflineState:true];
break;
case AFNetworkReachabilityStatusNotReachable:
if([[{{classPrefix}}Configuration sharedConfig] debug])
NSLog(@"reachability changed to AFNetworkReachabilityStatusNotReachable");
{{classPrefix}}DebugLog(@"reachability changed to AFNetworkReachabilityStatusNotReachable");
[{{classPrefix}}ApiClient setOfflineState:true];
break;
case AFNetworkReachabilityStatusReachableViaWWAN:
if([[{{classPrefix}}Configuration sharedConfig] debug])
NSLog(@"reachability changed to AFNetworkReachabilityStatusReachableViaWWAN");
{{classPrefix}}DebugLog(@"reachability changed to AFNetworkReachabilityStatusReachableViaWWAN");
[{{classPrefix}}ApiClient setOfflineState:false];
break;
case AFNetworkReachabilityStatusReachableViaWiFi:
if([[{{classPrefix}}Configuration sharedConfig] debug])
NSLog(@"reachability changed to AFNetworkReachabilityStatusReachableViaWiFi");
{{classPrefix}}DebugLog(@"reachability changed to AFNetworkReachabilityStatusReachableViaWiFi");
[{{classPrefix}}ApiClient setOfflineState:false];
break;
default:
@@ -394,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);
@@ -409,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;
@@ -471,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);
@@ -581,21 +591,15 @@ static void (^reachabilityChangeBlock)(int);
hasHeaderParams = true;
}
if(offlineState) {
if ([[{{classPrefix}}Configuration sharedConfig] debug]){
NSLog(@"%@ cache forced", resourcePath);
}
{{classPrefix}}DebugLog(@"%@ cache forced", resourcePath);
[request setCachePolicy:NSURLRequestReturnCacheDataDontLoad];
}
else if(!hasHeaderParams && [method isEqualToString:@"GET"] && cacheEnabled) {
if ([[{{classPrefix}}Configuration sharedConfig] debug]){
NSLog(@"%@ cache enabled", resourcePath);
}
{{classPrefix}}DebugLog(@"%@ cache enabled", resourcePath);
[request setCachePolicy:NSURLRequestUseProtocolCachePolicy];
}
else {
if ([[{{classPrefix}}Configuration sharedConfig] debug]){
NSLog(@"%@ cache disabled", resourcePath);
}
{{classPrefix}}DebugLog(@"%@ cache disabled", resourcePath);
[request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
}

View File

@@ -24,6 +24,10 @@
*/
extern NSString *const {{classPrefix}}ResponseObjectErrorKey;
/**
* Log debug message macro
*/
#define {{classPrefix}}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 SWGDebugLog(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,22 +60,47 @@ 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]];
}
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];
SWGDebugLog(message);
}
#pragma mark - Cache Methods
@@ -169,16 +194,14 @@ static void (^reachabilityChangeBlock)(int);
+(NSNumber*) nextRequestId {
@synchronized(self) {
long nextId = ++requestId;
if([[SWGConfiguration sharedConfig] debug])
NSLog(@"got id %ld", nextId);
SWGDebugLog(@"got id %ld", nextId);
return [NSNumber numberWithLong:nextId];
}
}
+(NSNumber*) queueRequest {
NSNumber* requestId = [SWGApiClient nextRequestId];
if([[SWGConfiguration sharedConfig] debug])
NSLog(@"added %@ to request queue", requestId);
SWGDebugLog(@"added %@ to request queue", requestId);
[queuedRequests addObject:requestId];
return requestId;
}
@@ -198,8 +221,7 @@ static void (^reachabilityChangeBlock)(int);
}];
if(matchingItems.count == 1) {
if([[SWGConfiguration sharedConfig] debug])
NSLog(@"removing request id %@", requestId);
SWGDebugLog(@"removed request id %@", requestId);
[queuedRequests removeObject:requestId];
return YES;
}
@@ -223,26 +245,22 @@ static void (^reachabilityChangeBlock)(int);
reachabilityStatus = status;
switch (status) {
case AFNetworkReachabilityStatusUnknown:
if([[SWGConfiguration sharedConfig] debug])
NSLog(@"reachability changed to AFNetworkReachabilityStatusUnknown");
SWGDebugLog(@"reachability changed to AFNetworkReachabilityStatusUnknown");
[SWGApiClient setOfflineState:true];
break;
case AFNetworkReachabilityStatusNotReachable:
if([[SWGConfiguration sharedConfig] debug])
NSLog(@"reachability changed to AFNetworkReachabilityStatusNotReachable");
SWGDebugLog(@"reachability changed to AFNetworkReachabilityStatusNotReachable");
[SWGApiClient setOfflineState:true];
break;
case AFNetworkReachabilityStatusReachableViaWWAN:
if([[SWGConfiguration sharedConfig] debug])
NSLog(@"reachability changed to AFNetworkReachabilityStatusReachableViaWWAN");
SWGDebugLog(@"reachability changed to AFNetworkReachabilityStatusReachableViaWWAN");
[SWGApiClient setOfflineState:false];
break;
case AFNetworkReachabilityStatusReachableViaWiFi:
if([[SWGConfiguration sharedConfig] debug])
NSLog(@"reachability changed to AFNetworkReachabilityStatusReachableViaWiFi");
SWGDebugLog(@"reachability changed to AFNetworkReachabilityStatusReachableViaWiFi");
[SWGApiClient setOfflineState:false];
break;
default:
@@ -390,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);
@@ -405,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;
@@ -467,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);
@@ -577,15 +591,15 @@ static void (^reachabilityChangeBlock)(int);
hasHeaderParams = true;
}
if(offlineState) {
NSLog(@"%@ cache forced", resourcePath);
SWGDebugLog(@"%@ cache forced", resourcePath);
[request setCachePolicy:NSURLRequestReturnCacheDataDontLoad];
}
else if(!hasHeaderParams && [method isEqualToString:@"GET"] && cacheEnabled) {
NSLog(@"%@ cache enabled", resourcePath);
SWGDebugLog(@"%@ cache enabled", resourcePath);
[request setCachePolicy:NSURLRequestUseProtocolCachePolicy];
}
else {
NSLog(@"%@ cache disabled", resourcePath);
SWGDebugLog(@"%@ 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>