Skip to content

Commit 477d04b

Browse files
committed
Add support for logging the elapsed time. Guard against attempts to log non-HTTP request operations.
1 parent 862e84a commit 477d04b

File tree

1 file changed

+22
-5
lines changed

1 file changed

+22
-5
lines changed

Code/Network/RKHTTPRequestOperation.m

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
// limitations under the License.
1919
//
2020

21+
#import <objc/runtime.h>
2122
#import "RKHTTPRequestOperation.h"
2223
#import "RKLog.h"
2324
#import "lcl_RK.h"
@@ -126,9 +127,17 @@ - (void)dealloc
126127
[[NSNotificationCenter defaultCenter] removeObserver:self];
127128
}
128129

130+
static void *RKHTTPRequestOperationStartDate = &RKHTTPRequestOperationStartDate;
131+
129132
- (void)HTTPOperationDidStart:(NSNotification *)notification
130133
{
131134
RKHTTPRequestOperation *operation = [notification object];
135+
136+
if (![operation isKindOfClass:[AFHTTPRequestOperation class]]) {
137+
return;
138+
}
139+
140+
objc_setAssociatedObject(operation, RKHTTPRequestOperationStartDate, [NSDate date], OBJC_ASSOCIATION_RETAIN_NONATOMIC);
132141

133142
if ((_RKlcl_component_level[(__RKlcl_log_symbol(RKlcl_cRestKitNetwork))]) >= (__RKlcl_log_symbol(RKlcl_vTrace))) {
134143
NSString *body = nil;
@@ -147,19 +156,27 @@ - (void)HTTPOperationDidStart:(NSNotification *)notification
147156
- (void)HTTPOperationDidFinish:(NSNotification *)notification
148157
{
149158
RKHTTPRequestOperation *operation = [notification object];
159+
160+
if (![operation isKindOfClass:[AFHTTPRequestOperation class]]) {
161+
return;
162+
}
163+
164+
NSTimeInterval elapsedTime = [[NSDate date] timeIntervalSinceDate:objc_getAssociatedObject(operation, RKHTTPRequestOperationStartDate)];
165+
150166
NSString *statusCodeString = RKStringFromStatusCode([operation.response statusCode]);
151-
NSString *statusCodeFragment = statusCodeString ? [NSString stringWithFormat:@"(%ld %@)", (long)[operation.response statusCode], statusCodeString] : [NSString stringWithFormat:@"(%ld)", (long)[operation.response statusCode]];
167+
NSString *elapsedTimeString = [NSString stringWithFormat:@"[%.04f s]", elapsedTime];
168+
NSString *statusCodeAndElapsedTime = statusCodeString ? [NSString stringWithFormat:@"(%ld %@) %@", (long)[operation.response statusCode], statusCodeString, elapsedTimeString] : [NSString stringWithFormat:@"(%ld) %@", (long)[operation.response statusCode], elapsedTimeString];
152169
if (operation.error) {
153170
if ((_RKlcl_component_level[(__RKlcl_log_symbol(RKlcl_cRestKitNetwork))]) >= (__RKlcl_log_symbol(RKlcl_vTrace))) {
154-
RKLogError(@"%@ '%@' %@:\nerror=%@\nresponse.body=%@", [operation.request HTTPMethod], [[operation.request URL] absoluteString], statusCodeFragment, operation.error, operation.responseString);
171+
RKLogError(@"%@ '%@' %@:\nerror=%@\nresponse.body=%@", [operation.request HTTPMethod], [[operation.request URL] absoluteString], statusCodeAndElapsedTime, operation.error, operation.responseString);
155172
} else {
156-
RKLogError(@"%@ '%@' %@: %@", [operation.request HTTPMethod], [[operation.request URL] absoluteString], statusCodeFragment, operation.error);
173+
RKLogError(@"%@ '%@' %@: %@", [operation.request HTTPMethod], [[operation.request URL] absoluteString], statusCodeAndElapsedTime, operation.error);
157174
}
158175
} else {
159176
if ((_RKlcl_component_level[(__RKlcl_log_symbol(RKlcl_cRestKitNetwork))]) >= (__RKlcl_log_symbol(RKlcl_vTrace))) {
160-
RKLogTrace(@"%@ '%@' %@:\nresponse.headers=%@\nresponse.body=%@", [operation.request HTTPMethod], [[operation.request URL] absoluteString], statusCodeFragment, [operation.response allHeaderFields], RKLogTruncateString(operation.responseString));
177+
RKLogTrace(@"%@ '%@' %@:\nresponse.headers=%@\nresponse.body=%@", [operation.request HTTPMethod], [[operation.request URL] absoluteString], statusCodeAndElapsedTime, [operation.response allHeaderFields], RKLogTruncateString(operation.responseString));
161178
} else {
162-
RKLogInfo(@"%@ '%@' %@", [operation.request HTTPMethod], [[operation.request URL] absoluteString], statusCodeFragment);
179+
RKLogInfo(@"%@ '%@' %@", [operation.request HTTPMethod], [[operation.request URL] absoluteString], statusCodeAndElapsedTime);
163180
}
164181
}
165182
}

0 commit comments

Comments
 (0)