Skip to content

Commit 0380fc5

Browse files
committed
Merge pull request #1274 from geekerzp/objc_log
[Objc] Update objc debug logging
2 parents 5e805c2 + 30fbdd4 commit 0380fc5

File tree

5 files changed

+118
-77
lines changed

5 files changed

+118
-77
lines changed

modules/swagger-codegen/src/main/resources/objc/ApiClient-body.mustache

Lines changed: 47 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -60,24 +60,47 @@ static void (^reachabilityChangeBlock)(int);
6060

6161
#pragma mark - Log Methods
6262

63-
- (void)logResponse:(AFHTTPRequestOperation *)operation
64-
forRequest:(NSURLRequest *)request
65-
error:(NSError*)error {
63+
+ (void)debugLog:(NSString *)method
64+
message:(NSString *)format, ... {
6665
{{classPrefix}}Configuration *config = [{{classPrefix}}Configuration sharedConfig];
66+
if (!config.debug) {
67+
return;
68+
}
6769

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+
}
7275

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.
7383
if (config.loggingFileHanlder) {
7484
[config.loggingFileHanlder seekToEndOfFile];
7585
[config.loggingFileHanlder writeData:[message dataUsingEncoding:NSUTF8StringEncoding]];
7686
}
77-
78-
if ([[{{classPrefix}}Configuration sharedConfig] debug]){
87+
else {
7988
NSLog(@"%@", message);
8089
}
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);
81104
}
82105

83106
#pragma mark - Cache Methods
@@ -171,17 +194,14 @@ static void (^reachabilityChangeBlock)(int);
171194
+(NSNumber*) nextRequestId {
172195
@synchronized(self) {
173196
long nextId = ++requestId;
174-
if([[{{classPrefix}}Configuration sharedConfig] debug])
175-
NSLog(@"got id %ld", nextId);
197+
{{classPrefix}}DebugLog(@"got id %ld", nextId);
176198
return [NSNumber numberWithLong:nextId];
177199
}
178200
}
179201

180202
+(NSNumber*) queueRequest {
181203
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);
185205
[queuedRequests addObject:requestId];
186206
return requestId;
187207
}
@@ -201,9 +221,7 @@ static void (^reachabilityChangeBlock)(int);
201221
}];
202222

203223
if(matchingItems.count == 1) {
204-
if([[{{classPrefix}}Configuration sharedConfig] debug]){
205-
NSLog(@"removing request id %@", requestId);
206-
}
224+
{{classPrefix}}DebugLog(@"removed request id %@", requestId);
207225
[queuedRequests removeObject:requestId];
208226
return YES;
209227
}
@@ -227,26 +245,22 @@ static void (^reachabilityChangeBlock)(int);
227245
reachabilityStatus = status;
228246
switch (status) {
229247
case AFNetworkReachabilityStatusUnknown:
230-
if([[{{classPrefix}}Configuration sharedConfig] debug])
231-
NSLog(@"reachability changed to AFNetworkReachabilityStatusUnknown");
248+
{{classPrefix}}DebugLog(@"reachability changed to AFNetworkReachabilityStatusUnknown");
232249
[{{classPrefix}}ApiClient setOfflineState:true];
233250
break;
234251

235252
case AFNetworkReachabilityStatusNotReachable:
236-
if([[{{classPrefix}}Configuration sharedConfig] debug])
237-
NSLog(@"reachability changed to AFNetworkReachabilityStatusNotReachable");
253+
{{classPrefix}}DebugLog(@"reachability changed to AFNetworkReachabilityStatusNotReachable");
238254
[{{classPrefix}}ApiClient setOfflineState:true];
239255
break;
240256

241257
case AFNetworkReachabilityStatusReachableViaWWAN:
242-
if([[{{classPrefix}}Configuration sharedConfig] debug])
243-
NSLog(@"reachability changed to AFNetworkReachabilityStatusReachableViaWWAN");
258+
{{classPrefix}}DebugLog(@"reachability changed to AFNetworkReachabilityStatusReachableViaWWAN");
244259
[{{classPrefix}}ApiClient setOfflineState:false];
245260
break;
246261

247262
case AFNetworkReachabilityStatusReachableViaWiFi:
248-
if([[{{classPrefix}}Configuration sharedConfig] debug])
249-
NSLog(@"reachability changed to AFNetworkReachabilityStatusReachableViaWiFi");
263+
{{classPrefix}}DebugLog(@"reachability changed to AFNetworkReachabilityStatusReachableViaWiFi");
250264
[{{classPrefix}}ApiClient setOfflineState:false];
251265
break;
252266
default:
@@ -394,9 +408,7 @@ static void (^reachabilityChangeBlock)(int);
394408
AFHTTPRequestOperation *op = [self HTTPRequestOperationWithRequest:request
395409
success:^(AFHTTPRequestOperation *operation, id response) {
396410
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];
400412
NSDictionary *responseHeaders = [[operation response] allHeaderFields];
401413
self.HTTPResponseHeaders = responseHeaders;
402414
completionBlock(response, nil);
@@ -409,9 +421,7 @@ static void (^reachabilityChangeBlock)(int);
409421
userInfo[{{classPrefix}}ResponseObjectErrorKey] = operation.responseObject;
410422
}
411423
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];
415425

416426
NSDictionary *responseHeaders = [[operation response] allHeaderFields];
417427
self.HTTPResponseHeaders = responseHeaders;
@@ -471,9 +481,9 @@ static void (^reachabilityChangeBlock)(int);
471481

472482
NSError *augmentedError = [error initWithDomain:error.domain code:error.code userInfo:userInfo];
473483

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+
477487
NSDictionary *responseHeaders = [[operation response] allHeaderFields];
478488
self.HTTPResponseHeaders = responseHeaders;
479489
completionBlock(nil, augmentedError);
@@ -581,21 +591,15 @@ static void (^reachabilityChangeBlock)(int);
581591
hasHeaderParams = true;
582592
}
583593
if(offlineState) {
584-
if ([[{{classPrefix}}Configuration sharedConfig] debug]){
585-
NSLog(@"%@ cache forced", resourcePath);
586-
}
594+
{{classPrefix}}DebugLog(@"%@ cache forced", resourcePath);
587595
[request setCachePolicy:NSURLRequestReturnCacheDataDontLoad];
588596
}
589597
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);
593599
[request setCachePolicy:NSURLRequestUseProtocolCachePolicy];
594600
}
595601
else {
596-
if ([[{{classPrefix}}Configuration sharedConfig] debug]){
597-
NSLog(@"%@ cache disabled", resourcePath);
598-
}
602+
{{classPrefix}}DebugLog(@"%@ cache disabled", resourcePath);
599603
[request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
600604
}
601605

modules/swagger-codegen/src/main/resources/objc/ApiClient-header.mustache

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@
2424
*/
2525
extern NSString *const {{classPrefix}}ResponseObjectErrorKey;
2626

27+
/**
28+
* Log debug message macro
29+
*/
30+
#define {{classPrefix}}DebugLog(format, ...) [{{classPrefix}}ApiClient debugLog:[NSString stringWithFormat:@"%s", __PRETTY_FUNCTION__] message: format, ##__VA_ARGS__];
2731

2832
@interface {{classPrefix}}ApiClient : AFHTTPRequestOperationManager
2933

@@ -215,4 +219,9 @@ extern NSString *const {{classPrefix}}ResponseObjectErrorKey;
215219
*/
216220
- (NSString *) parameterToString: (id) param;
217221

222+
/**
223+
* Log debug message
224+
*/
225+
+(void)debugLog:(NSString *)method message:(NSString *)format, ...;
226+
218227
@end

samples/client/petstore/objc/SwaggerClient/SWGApiClient.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@
2828
*/
2929
extern NSString *const SWGResponseObjectErrorKey;
3030

31+
/**
32+
* Log debug message macro
33+
*/
34+
#define SWGDebugLog(format, ...) [SWGApiClient debugLog:[NSString stringWithFormat:@"%s", __PRETTY_FUNCTION__] message: format, ##__VA_ARGS__];
3135

3236
@interface SWGApiClient : AFHTTPRequestOperationManager
3337

@@ -219,4 +223,9 @@ extern NSString *const SWGResponseObjectErrorKey;
219223
*/
220224
- (NSString *) parameterToString: (id) param;
221225

226+
/**
227+
* Log debug message
228+
*/
229+
+(void)debugLog:(NSString *)method message:(NSString *)format, ...;
230+
222231
@end

samples/client/petstore/objc/SwaggerClient/SWGApiClient.m

Lines changed: 48 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -60,22 +60,47 @@ - (void)setHeaderValue:(NSString*) value
6060

6161
#pragma mark - Log Methods
6262

63-
- (void)logResponse:(AFHTTPRequestOperation *)operation
64-
forRequest:(NSURLRequest *)request
65-
error:(NSError*)error {
63+
+ (void)debugLog:(NSString *)method
64+
message:(NSString *)format, ... {
6665
SWGConfiguration *config = [SWGConfiguration sharedConfig];
66+
if (!config.debug) {
67+
return;
68+
}
6769

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];
7271

72+
if (method) {
73+
[message appendString:[NSString stringWithFormat:@"%@: ", method]];
74+
}
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.
7383
if (config.loggingFileHanlder) {
7484
[config.loggingFileHanlder seekToEndOfFile];
7585
[config.loggingFileHanlder writeData:[message dataUsingEncoding:NSUTF8StringEncoding]];
7686
}
87+
else {
88+
NSLog(@"%@", message);
89+
}
7790

78-
NSLog(@"%@", message);
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+
SWGDebugLog(message);
79104
}
80105

81106
#pragma mark - Cache Methods
@@ -169,16 +194,14 @@ +(unsigned long)requestQueueSize {
169194
+(NSNumber*) nextRequestId {
170195
@synchronized(self) {
171196
long nextId = ++requestId;
172-
if([[SWGConfiguration sharedConfig] debug])
173-
NSLog(@"got id %ld", nextId);
197+
SWGDebugLog(@"got id %ld", nextId);
174198
return [NSNumber numberWithLong:nextId];
175199
}
176200
}
177201

178202
+(NSNumber*) queueRequest {
179203
NSNumber* requestId = [SWGApiClient nextRequestId];
180-
if([[SWGConfiguration sharedConfig] debug])
181-
NSLog(@"added %@ to request queue", requestId);
204+
SWGDebugLog(@"added %@ to request queue", requestId);
182205
[queuedRequests addObject:requestId];
183206
return requestId;
184207
}
@@ -198,8 +221,7 @@ -(Boolean) executeRequestWithId:(NSNumber*) requestId {
198221
}];
199222

200223
if(matchingItems.count == 1) {
201-
if([[SWGConfiguration sharedConfig] debug])
202-
NSLog(@"removing request id %@", requestId);
224+
SWGDebugLog(@"removed request id %@", requestId);
203225
[queuedRequests removeObject:requestId];
204226
return YES;
205227
}
@@ -223,26 +245,22 @@ - (void) configureCacheReachibility {
223245
reachabilityStatus = status;
224246
switch (status) {
225247
case AFNetworkReachabilityStatusUnknown:
226-
if([[SWGConfiguration sharedConfig] debug])
227-
NSLog(@"reachability changed to AFNetworkReachabilityStatusUnknown");
248+
SWGDebugLog(@"reachability changed to AFNetworkReachabilityStatusUnknown");
228249
[SWGApiClient setOfflineState:true];
229250
break;
230251

231252
case AFNetworkReachabilityStatusNotReachable:
232-
if([[SWGConfiguration sharedConfig] debug])
233-
NSLog(@"reachability changed to AFNetworkReachabilityStatusNotReachable");
253+
SWGDebugLog(@"reachability changed to AFNetworkReachabilityStatusNotReachable");
234254
[SWGApiClient setOfflineState:true];
235255
break;
236256

237257
case AFNetworkReachabilityStatusReachableViaWWAN:
238-
if([[SWGConfiguration sharedConfig] debug])
239-
NSLog(@"reachability changed to AFNetworkReachabilityStatusReachableViaWWAN");
258+
SWGDebugLog(@"reachability changed to AFNetworkReachabilityStatusReachableViaWWAN");
240259
[SWGApiClient setOfflineState:false];
241260
break;
242261

243262
case AFNetworkReachabilityStatusReachableViaWiFi:
244-
if([[SWGConfiguration sharedConfig] debug])
245-
NSLog(@"reachability changed to AFNetworkReachabilityStatusReachableViaWiFi");
263+
SWGDebugLog(@"reachability changed to AFNetworkReachabilityStatusReachableViaWiFi");
246264
[SWGApiClient setOfflineState:false];
247265
break;
248266
default:
@@ -390,9 +408,7 @@ - (void) operationWithCompletionBlock: (NSURLRequest *)request
390408
AFHTTPRequestOperation *op = [self HTTPRequestOperationWithRequest:request
391409
success:^(AFHTTPRequestOperation *operation, id response) {
392410
if([self executeRequestWithId:requestId]) {
393-
if([[SWGConfiguration sharedConfig] debug]) {
394-
[self logResponse:operation forRequest:request error:nil];
395-
}
411+
[self logResponse:operation forRequest:request error:nil];
396412
NSDictionary *responseHeaders = [[operation response] allHeaderFields];
397413
self.HTTPResponseHeaders = responseHeaders;
398414
completionBlock(response, nil);
@@ -405,9 +421,7 @@ - (void) operationWithCompletionBlock: (NSURLRequest *)request
405421
userInfo[SWGResponseObjectErrorKey] = operation.responseObject;
406422
}
407423
NSError *augmentedError = [error initWithDomain:error.domain code:error.code userInfo:userInfo];
408-
409-
if([[SWGConfiguration sharedConfig] debug])
410-
[self logResponse:nil forRequest:request error:augmentedError];
424+
[self logResponse:nil forRequest:request error:augmentedError];
411425

412426
NSDictionary *responseHeaders = [[operation response] allHeaderFields];
413427
self.HTTPResponseHeaders = responseHeaders;
@@ -467,9 +481,9 @@ - (void) downloadOperationWithCompletionBlock: (NSURLRequest *)request
467481

468482
NSError *augmentedError = [error initWithDomain:error.domain code:error.code userInfo:userInfo];
469483

470-
if ([[SWGConfiguration sharedConfig] debug]) {
471-
[self logResponse:nil forRequest:request error:augmentedError];
472-
}
484+
485+
[self logResponse:nil forRequest:request error:augmentedError];
486+
473487
NSDictionary *responseHeaders = [[operation response] allHeaderFields];
474488
self.HTTPResponseHeaders = responseHeaders;
475489
completionBlock(nil, augmentedError);
@@ -577,15 +591,15 @@ -(NSNumber*) requestWithCompletionBlock: (NSString*) path
577591
hasHeaderParams = true;
578592
}
579593
if(offlineState) {
580-
NSLog(@"%@ cache forced", resourcePath);
594+
SWGDebugLog(@"%@ cache forced", resourcePath);
581595
[request setCachePolicy:NSURLRequestReturnCacheDataDontLoad];
582596
}
583597
else if(!hasHeaderParams && [method isEqualToString:@"GET"] && cacheEnabled) {
584-
NSLog(@"%@ cache enabled", resourcePath);
598+
SWGDebugLog(@"%@ cache enabled", resourcePath);
585599
[request setCachePolicy:NSURLRequestUseProtocolCachePolicy];
586600
}
587601
else {
588-
NSLog(@"%@ cache disabled", resourcePath);
602+
SWGDebugLog(@"%@ cache disabled", resourcePath);
589603
[request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
590604
}
591605

0 commit comments

Comments
 (0)