@@ -60,27 +60,47 @@ static void (^reachabilityChangeBlock)(int);
60
60
61
61
#pragma mark - Log Methods
62
62
63
- - (void)logResponse:(AFHTTPRequestOperation *)operation
64
- forRequest:(NSURLRequest *)request
65
- error:(NSError*)error {
63
+ + (void)debugLog:(NSString *)method
64
+ message:(NSString *)format, ... {
66
65
{{classPrefix} }Configuration *config = [{ {classPrefix} }Configuration sharedConfig];
67
-
68
66
if (!config.debug) {
69
67
return;
70
68
}
71
69
72
- NSString *message = [NSString stringWithFormat:@"\n[DEBUG] Request body \n~BEGIN~\n %@\n~END~\n"\
73
- "[DEBUG] HTTP Response body \n~BEGIN~\n %@\n~END~\n",
74
- [[NSString alloc] initWithData:request.HTTPBody encoding:NSUTF8StringEncoding],
75
- operation.responseString];
70
+ NSMutableString *message = [NSMutableString stringWithCapacity:1];
71
+
72
+ if (method) {
73
+ [message appendString:[NSString stringWithFormat:@" %@: " , method]];
74
+ }
76
75
76
+ va_list args;
77
+ va_start(args, format);
78
+
79
+ [message appendString:[[NSString alloc] initWithFormat:format arguments:args]];
80
+
81
+ // If set logging file handler, log into file,
82
+ // otherwise log into console.
77
83
if (config.loggingFileHanlder) {
78
84
[config.loggingFileHanlder seekToEndOfFile];
79
85
[config.loggingFileHanlder writeData:[message dataUsingEncoding:NSUTF8StringEncoding]];
80
86
}
81
87
else {
82
88
NSLog(@" %@" , message);
83
89
}
90
+
91
+ va_end(args);
92
+ }
93
+
94
+ - (void)logResponse:(AFHTTPRequestOperation *)operation
95
+ forRequest:(NSURLRequest *)request
96
+ error:(NSError*)error {
97
+
98
+ NSString *message = [NSString stringWithFormat:@" \n [DEBUG] HTTP request body \n ~BEGIN~\n %@\n ~END~\n " \
99
+ " [DEBUG] HTTP response body \n ~BEGIN~\n %@\n ~END~\n " ,
100
+ [[NSString alloc] initWithData:request.HTTPBody encoding:NSUTF8StringEncoding],
101
+ operation.responseString];
102
+
103
+ DebugLog(message);
84
104
}
85
105
86
106
#pragma mark - Cache Methods
@@ -174,17 +194,14 @@ static void (^reachabilityChangeBlock)(int);
174
194
+(NSNumber*) nextRequestId {
175
195
@synchronized(self) {
176
196
long nextId = ++requestId;
177
- if ([[{{classPrefix} }Configuration sharedConfig] debug])
178
- NSLog(@"got id %ld", nextId);
197
+ DebugLog(@" got id %ld" , nextId);
179
198
return [NSNumber numberWithLong:nextId];
180
199
}
181
200
}
182
201
183
202
+(NSNumber*) queueRequest {
184
203
NSNumber* requestId = [{{classPrefix} }ApiClient nextRequestId];
185
- if([[{ {classPrefix} }Configuration sharedConfig] debug]) {
186
- NSLog(@" added %@ to request queue" , requestId);
187
- }
204
+ DebugLog(@"added %@ to request queue", requestId);
188
205
[queuedRequests addObject:requestId];
189
206
return requestId;
190
207
}
@@ -204,9 +221,7 @@ static void (^reachabilityChangeBlock)(int);
204
221
}];
205
222
206
223
if(matchingItems.count == 1) {
207
- if ([[{{classPrefix} }Configuration sharedConfig] debug]){
208
- NSLog(@" removing request id %@" , requestId);
209
- }
224
+ DebugLog(@" removed request id %@" , requestId);
210
225
[queuedRequests removeObject:requestId];
211
226
return YES;
212
227
}
@@ -230,26 +245,22 @@ static void (^reachabilityChangeBlock)(int);
230
245
reachabilityStatus = status;
231
246
switch (status) {
232
247
case AFNetworkReachabilityStatusUnknown:
233
- if ([[{{classPrefix} }Configuration sharedConfig] debug])
234
- NSLog(@"reachability changed to AFNetworkReachabilityStatusUnknown");
248
+ DebugLog(@" reachability changed to AFNetworkReachabilityStatusUnknown" );
235
249
[{{classPrefix} }ApiClient setOfflineState:true];
236
250
break;
237
251
238
252
case AFNetworkReachabilityStatusNotReachable:
239
- if([[{ {classPrefix} }Configuration sharedConfig] debug])
240
- NSLog(@"reachability changed to AFNetworkReachabilityStatusNotReachable");
253
+ DebugLog(@"reachability changed to AFNetworkReachabilityStatusNotReachable");
241
254
[{ {classPrefix} }ApiClient setOfflineState:true];
242
255
break;
243
256
244
257
case AFNetworkReachabilityStatusReachableViaWWAN:
245
- if([[{ {classPrefix} }Configuration sharedConfig] debug])
246
- NSLog(@"reachability changed to AFNetworkReachabilityStatusReachableViaWWAN");
258
+ DebugLog(@"reachability changed to AFNetworkReachabilityStatusReachableViaWWAN");
247
259
[{ {classPrefix} }ApiClient setOfflineState:false];
248
260
break;
249
261
250
262
case AFNetworkReachabilityStatusReachableViaWiFi:
251
- if([[{ {classPrefix} }Configuration sharedConfig] debug])
252
- NSLog(@"reachability changed to AFNetworkReachabilityStatusReachableViaWiFi");
263
+ DebugLog(@"reachability changed to AFNetworkReachabilityStatusReachableViaWiFi");
253
264
[{ {classPrefix} }ApiClient setOfflineState:false];
254
265
break;
255
266
default:
@@ -397,9 +408,7 @@ static void (^reachabilityChangeBlock)(int);
397
408
AFHTTPRequestOperation *op = [self HTTPRequestOperationWithRequest:request
398
409
success:^(AFHTTPRequestOperation *operation, id response) {
399
410
if ([self executeRequestWithId:requestId]) {
400
- if ([[{{classPrefix} }Configuration sharedConfig] debug]) {
401
- [self logResponse:operation forRequest:request error:nil];
402
- }
411
+ [self logResponse:operation forRequest:request error:nil];
403
412
NSDictionary *responseHeaders = [[operation response] allHeaderFields];
404
413
self.HTTPResponseHeaders = responseHeaders;
405
414
completionBlock(response, nil);
@@ -412,9 +421,7 @@ static void (^reachabilityChangeBlock)(int);
412
421
userInfo[{{classPrefix} }ResponseObjectErrorKey] = operation.responseObject;
413
422
}
414
423
NSError *augmentedError = [error initWithDomain:error.domain code:error.code userInfo:userInfo];
415
-
416
- if([[{ {classPrefix} }Configuration sharedConfig] debug])
417
- [self logResponse:nil forRequest:request error:augmentedError];
424
+ [self logResponse:nil forRequest:request error:augmentedError];
418
425
419
426
NSDictionary *responseHeaders = [[operation response] allHeaderFields];
420
427
self.HTTPResponseHeaders = responseHeaders;
@@ -474,9 +481,9 @@ static void (^reachabilityChangeBlock)(int);
474
481
475
482
NSError *augmentedError = [error initWithDomain:error.domain code:error.code userInfo:userInfo];
476
483
477
- if ([[ { {classPrefix } }Configuration sharedConfig] debug]) {
478
- [self logResponse:nil forRequest:request error:augmentedError];
479
- }
484
+
485
+ [self logResponse:nil forRequest:request error:augmentedError];
486
+
480
487
NSDictionary *responseHeaders = [[operation response] allHeaderFields];
481
488
self.HTTPResponseHeaders = responseHeaders;
482
489
completionBlock(nil, augmentedError);
@@ -584,21 +591,15 @@ static void (^reachabilityChangeBlock)(int);
584
591
hasHeaderParams = true ;
585
592
}
586
593
if(offlineState) {
587
- if ([[{{classPrefix} }Configuration sharedConfig] debug]){
588
- NSLog(@" %@ cache forced" , resourcePath);
589
- }
594
+ DebugLog(@" %@ cache forced" , resourcePath);
590
595
[request setCachePolicy:NSURLRequestReturnCacheDataDontLoad];
591
596
}
592
597
else if(!hasHeaderParams && [method isEqualToString:@"GET"] && cacheEnabled) {
593
- if ([[{{classPrefix} }Configuration sharedConfig] debug]){
594
- NSLog(@" %@ cache enabled" , resourcePath);
595
- }
598
+ DebugLog(@" %@ cache enabled" , resourcePath);
596
599
[request setCachePolicy:NSURLRequestUseProtocolCachePolicy];
597
600
}
598
601
else {
599
- if ([[{{classPrefix} }Configuration sharedConfig] debug]){
600
- NSLog(@" %@ cache disabled" , resourcePath);
601
- }
602
+ DebugLog(@" %@ cache disabled" , resourcePath);
602
603
[request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
603
604
}
604
605
0 commit comments