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