Skip to content

Commit 844cae7

Browse files
committed
Update debug logging in objc client
1 parent 0622838 commit 844cae7

File tree

5 files changed

+109
-84
lines changed

5 files changed

+109
-84
lines changed

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

Lines changed: 43 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -60,27 +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];
67-
6866
if (!config.debug) {
6967
return;
7068
}
7169

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

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.
7783
if (config.loggingFileHanlder) {
7884
[config.loggingFileHanlder seekToEndOfFile];
7985
[config.loggingFileHanlder writeData:[message dataUsingEncoding:NSUTF8StringEncoding]];
8086
}
8187
else {
8288
NSLog(@"%@", message);
8389
}
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);
84104
}
85105

86106
#pragma mark - Cache Methods
@@ -174,17 +194,14 @@ static void (^reachabilityChangeBlock)(int);
174194
+(NSNumber*) nextRequestId {
175195
@synchronized(self) {
176196
long nextId = ++requestId;
177-
if([[{{classPrefix}}Configuration sharedConfig] debug])
178-
NSLog(@"got id %ld", nextId);
197+
DebugLog(@"got id %ld", nextId);
179198
return [NSNumber numberWithLong:nextId];
180199
}
181200
}
182201

183202
+(NSNumber*) queueRequest {
184203
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);
188205
[queuedRequests addObject:requestId];
189206
return requestId;
190207
}
@@ -204,9 +221,7 @@ static void (^reachabilityChangeBlock)(int);
204221
}];
205222

206223
if(matchingItems.count == 1) {
207-
if([[{{classPrefix}}Configuration sharedConfig] debug]){
208-
NSLog(@"removing request id %@", requestId);
209-
}
224+
DebugLog(@"removed request id %@", requestId);
210225
[queuedRequests removeObject:requestId];
211226
return YES;
212227
}
@@ -230,26 +245,22 @@ static void (^reachabilityChangeBlock)(int);
230245
reachabilityStatus = status;
231246
switch (status) {
232247
case AFNetworkReachabilityStatusUnknown:
233-
if([[{{classPrefix}}Configuration sharedConfig] debug])
234-
NSLog(@"reachability changed to AFNetworkReachabilityStatusUnknown");
248+
DebugLog(@"reachability changed to AFNetworkReachabilityStatusUnknown");
235249
[{{classPrefix}}ApiClient setOfflineState:true];
236250
break;
237251

238252
case AFNetworkReachabilityStatusNotReachable:
239-
if([[{{classPrefix}}Configuration sharedConfig] debug])
240-
NSLog(@"reachability changed to AFNetworkReachabilityStatusNotReachable");
253+
DebugLog(@"reachability changed to AFNetworkReachabilityStatusNotReachable");
241254
[{{classPrefix}}ApiClient setOfflineState:true];
242255
break;
243256

244257
case AFNetworkReachabilityStatusReachableViaWWAN:
245-
if([[{{classPrefix}}Configuration sharedConfig] debug])
246-
NSLog(@"reachability changed to AFNetworkReachabilityStatusReachableViaWWAN");
258+
DebugLog(@"reachability changed to AFNetworkReachabilityStatusReachableViaWWAN");
247259
[{{classPrefix}}ApiClient setOfflineState:false];
248260
break;
249261

250262
case AFNetworkReachabilityStatusReachableViaWiFi:
251-
if([[{{classPrefix}}Configuration sharedConfig] debug])
252-
NSLog(@"reachability changed to AFNetworkReachabilityStatusReachableViaWiFi");
263+
DebugLog(@"reachability changed to AFNetworkReachabilityStatusReachableViaWiFi");
253264
[{{classPrefix}}ApiClient setOfflineState:false];
254265
break;
255266
default:
@@ -397,9 +408,7 @@ static void (^reachabilityChangeBlock)(int);
397408
AFHTTPRequestOperation *op = [self HTTPRequestOperationWithRequest:request
398409
success:^(AFHTTPRequestOperation *operation, id response) {
399410
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];
403412
NSDictionary *responseHeaders = [[operation response] allHeaderFields];
404413
self.HTTPResponseHeaders = responseHeaders;
405414
completionBlock(response, nil);
@@ -412,9 +421,7 @@ static void (^reachabilityChangeBlock)(int);
412421
userInfo[{{classPrefix}}ResponseObjectErrorKey] = operation.responseObject;
413422
}
414423
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];
418425

419426
NSDictionary *responseHeaders = [[operation response] allHeaderFields];
420427
self.HTTPResponseHeaders = responseHeaders;
@@ -474,9 +481,9 @@ static void (^reachabilityChangeBlock)(int);
474481

475482
NSError *augmentedError = [error initWithDomain:error.domain code:error.code userInfo:userInfo];
476483

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+
480487
NSDictionary *responseHeaders = [[operation response] allHeaderFields];
481488
self.HTTPResponseHeaders = responseHeaders;
482489
completionBlock(nil, augmentedError);
@@ -584,21 +591,15 @@ static void (^reachabilityChangeBlock)(int);
584591
hasHeaderParams = true;
585592
}
586593
if(offlineState) {
587-
if ([[{{classPrefix}}Configuration sharedConfig] debug]){
588-
NSLog(@"%@ cache forced", resourcePath);
589-
}
594+
DebugLog(@"%@ cache forced", resourcePath);
590595
[request setCachePolicy:NSURLRequestReturnCacheDataDontLoad];
591596
}
592597
else if(!hasHeaderParams && [method isEqualToString:@"GET"] && cacheEnabled) {
593-
if ([[{{classPrefix}}Configuration sharedConfig] debug]){
594-
NSLog(@"%@ cache enabled", resourcePath);
595-
}
598+
DebugLog(@"%@ cache enabled", resourcePath);
596599
[request setCachePolicy:NSURLRequestUseProtocolCachePolicy];
597600
}
598601
else {
599-
if ([[{{classPrefix}}Configuration sharedConfig] debug]){
600-
NSLog(@"%@ cache disabled", resourcePath);
601-
}
602+
DebugLog(@"%@ cache disabled", resourcePath);
602603
[request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
603604
}
604605

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 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 DebugLog(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: 43 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -60,27 +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];
67-
6866
if (!config.debug) {
6967
return;
7068
}
7169

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

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.
7783
if (config.loggingFileHanlder) {
7884
[config.loggingFileHanlder seekToEndOfFile];
7985
[config.loggingFileHanlder writeData:[message dataUsingEncoding:NSUTF8StringEncoding]];
8086
}
8187
else {
8288
NSLog(@"%@", message);
8389
}
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);
84104
}
85105

86106
#pragma mark - Cache Methods
@@ -174,17 +194,14 @@ +(unsigned long)requestQueueSize {
174194
+(NSNumber*) nextRequestId {
175195
@synchronized(self) {
176196
long nextId = ++requestId;
177-
if([[SWGConfiguration sharedConfig] debug])
178-
NSLog(@"got id %ld", nextId);
197+
DebugLog(@"got id %ld", nextId);
179198
return [NSNumber numberWithLong:nextId];
180199
}
181200
}
182201

183202
+(NSNumber*) queueRequest {
184203
NSNumber* requestId = [SWGApiClient nextRequestId];
185-
if([[SWGConfiguration sharedConfig] debug]) {
186-
NSLog(@"added %@ to request queue", requestId);
187-
}
204+
DebugLog(@"added %@ to request queue", requestId);
188205
[queuedRequests addObject:requestId];
189206
return requestId;
190207
}
@@ -204,9 +221,7 @@ -(Boolean) executeRequestWithId:(NSNumber*) requestId {
204221
}];
205222

206223
if(matchingItems.count == 1) {
207-
if([[SWGConfiguration sharedConfig] debug]){
208-
NSLog(@"removing request id %@", requestId);
209-
}
224+
DebugLog(@"removed request id %@", requestId);
210225
[queuedRequests removeObject:requestId];
211226
return YES;
212227
}
@@ -230,26 +245,22 @@ - (void) configureCacheReachibility {
230245
reachabilityStatus = status;
231246
switch (status) {
232247
case AFNetworkReachabilityStatusUnknown:
233-
if([[SWGConfiguration sharedConfig] debug])
234-
NSLog(@"reachability changed to AFNetworkReachabilityStatusUnknown");
248+
DebugLog(@"reachability changed to AFNetworkReachabilityStatusUnknown");
235249
[SWGApiClient setOfflineState:true];
236250
break;
237251

238252
case AFNetworkReachabilityStatusNotReachable:
239-
if([[SWGConfiguration sharedConfig] debug])
240-
NSLog(@"reachability changed to AFNetworkReachabilityStatusNotReachable");
253+
DebugLog(@"reachability changed to AFNetworkReachabilityStatusNotReachable");
241254
[SWGApiClient setOfflineState:true];
242255
break;
243256

244257
case AFNetworkReachabilityStatusReachableViaWWAN:
245-
if([[SWGConfiguration sharedConfig] debug])
246-
NSLog(@"reachability changed to AFNetworkReachabilityStatusReachableViaWWAN");
258+
DebugLog(@"reachability changed to AFNetworkReachabilityStatusReachableViaWWAN");
247259
[SWGApiClient setOfflineState:false];
248260
break;
249261

250262
case AFNetworkReachabilityStatusReachableViaWiFi:
251-
if([[SWGConfiguration sharedConfig] debug])
252-
NSLog(@"reachability changed to AFNetworkReachabilityStatusReachableViaWiFi");
263+
DebugLog(@"reachability changed to AFNetworkReachabilityStatusReachableViaWiFi");
253264
[SWGApiClient setOfflineState:false];
254265
break;
255266
default:
@@ -397,9 +408,7 @@ - (void) operationWithCompletionBlock: (NSURLRequest *)request
397408
AFHTTPRequestOperation *op = [self HTTPRequestOperationWithRequest:request
398409
success:^(AFHTTPRequestOperation *operation, id response) {
399410
if([self executeRequestWithId:requestId]) {
400-
if([[SWGConfiguration sharedConfig] debug]) {
401-
[self logResponse:operation forRequest:request error:nil];
402-
}
411+
[self logResponse:operation forRequest:request error:nil];
403412
NSDictionary *responseHeaders = [[operation response] allHeaderFields];
404413
self.HTTPResponseHeaders = responseHeaders;
405414
completionBlock(response, nil);
@@ -412,9 +421,7 @@ - (void) operationWithCompletionBlock: (NSURLRequest *)request
412421
userInfo[SWGResponseObjectErrorKey] = operation.responseObject;
413422
}
414423
NSError *augmentedError = [error initWithDomain:error.domain code:error.code userInfo:userInfo];
415-
416-
if([[SWGConfiguration sharedConfig] debug])
417-
[self logResponse:nil forRequest:request error:augmentedError];
424+
[self logResponse:nil forRequest:request error:augmentedError];
418425

419426
NSDictionary *responseHeaders = [[operation response] allHeaderFields];
420427
self.HTTPResponseHeaders = responseHeaders;
@@ -474,9 +481,9 @@ - (void) downloadOperationWithCompletionBlock: (NSURLRequest *)request
474481

475482
NSError *augmentedError = [error initWithDomain:error.domain code:error.code userInfo:userInfo];
476483

477-
if ([[SWGConfiguration sharedConfig] debug]) {
478-
[self logResponse:nil forRequest:request error:augmentedError];
479-
}
484+
485+
[self logResponse:nil forRequest:request error:augmentedError];
486+
480487
NSDictionary *responseHeaders = [[operation response] allHeaderFields];
481488
self.HTTPResponseHeaders = responseHeaders;
482489
completionBlock(nil, augmentedError);
@@ -584,21 +591,15 @@ -(NSNumber*) requestWithCompletionBlock: (NSString*) path
584591
hasHeaderParams = true;
585592
}
586593
if(offlineState) {
587-
if ([[SWGConfiguration sharedConfig] debug]){
588-
NSLog(@"%@ cache forced", resourcePath);
589-
}
594+
DebugLog(@"%@ cache forced", resourcePath);
590595
[request setCachePolicy:NSURLRequestReturnCacheDataDontLoad];
591596
}
592597
else if(!hasHeaderParams && [method isEqualToString:@"GET"] && cacheEnabled) {
593-
if ([[SWGConfiguration sharedConfig] debug]){
594-
NSLog(@"%@ cache enabled", resourcePath);
595-
}
598+
DebugLog(@"%@ cache enabled", resourcePath);
596599
[request setCachePolicy:NSURLRequestUseProtocolCachePolicy];
597600
}
598601
else {
599-
if ([[SWGConfiguration sharedConfig] debug]){
600-
NSLog(@"%@ cache disabled", resourcePath);
601-
}
602+
DebugLog(@"%@ cache disabled", resourcePath);
602603
[request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
603604
}
604605

0 commit comments

Comments
 (0)