2626
2727#import < objc/runtime.h>
2828
29+ 30+ #if __has_include(<CocoaLumberjack/DDLogMacros.h>)
31+ #import < CocoaLumberjack/DDLogMacros.h>
32+ // Global log level for the whole library, not per-file.
33+ const DDLogLevel ddLogLevel = DDLogLevelVerbose;
34+ #else
35+ #define DDLogError (...) NSLog (__VA_ARGS__)
36+ #define DDLogWarn (...) NSLog (__VA_ARGS__)
37+ #define DDLogInfo (...) NSLog (__VA_ARGS__)
38+ #define DDLogDebug (...) NSLog (__VA_ARGS__)
39+ #define DDLogVerbose (...) NSLog (__VA_ARGS__)
40+ #endif
41+ 42+ 2943typedef NSString * (^AFHTTPClientLoggerFormatBlock)(AFHTTPRequestOperation *operation, AFHTTPClientLogLevel level);
3044
3145@interface AFHTTPClientLogger ()
3246@property (readwrite , nonatomic ) NSString *baseURLString;
3347@property (readwrite , nonatomic , copy ) AFHTTPClientLoggerFormatBlock requestStartFormatBlock;
3448@property (readwrite , nonatomic , copy ) AFHTTPClientLoggerFormatBlock requestFinishFormatBlock;
49+ @property (readwrite , nonatomic , strong ) NSOperationQueue *notificationHandlerQueue;
50+ @property (readwrite , nonatomic , strong ) id <NSObject > startNotificationObserver;
51+ @property (readwrite , nonatomic , strong ) id <NSObject > finishNotificationObserver;
3552@end
3653
3754#pragma mark -
@@ -40,24 +57,46 @@ @implementation AFHTTPClientLogger
4057
4158- (instancetype )initWithBaseURL : (NSURL *)baseURL {
4259 if ((self = [super init ])) {
43- self.baseURLString = [baseURL absoluteString ];
44- self.level = AFHTTPClientLogLevelInfo;
60+ _baseURLString = [baseURL absoluteString ];
61+ _level = AFHTTPClientLogLevelInfo;
62+ _notificationHandlerQueue = [[NSOperationQueue alloc ] init ];
4563 }
4664
4765 return self;
4866}
4967
5068- (void )dealloc {
51- [[NSNotificationCenter defaultCenter ] removeObserver: self ];
69+ [_notificationHandlerQueue cancelAllOperations ];
70+ [[NSNotificationCenter defaultCenter ] removeObserver: _startNotificationObserver name: AFNetworkingOperationDidStartNotification object: nil ];
71+ [[NSNotificationCenter defaultCenter ] removeObserver: _finishNotificationObserver name: AFNetworkingOperationDidFinishNotification object: nil ];
5272}
5373
5474- (void )setEnabled : (BOOL )enabled {
5575 if (enabled != _enabled) {
5676 if (enabled) {
57- [[NSNotificationCenter defaultCenter ] addObserver: self selector: @selector (operationDidStart: ) name: AFNetworkingOperationDidStartNotification object: nil ];
58- [[NSNotificationCenter defaultCenter ] addObserver: self selector: @selector (operationDidFinish: ) name: AFNetworkingOperationDidFinishNotification object: nil ];
77+ // weakify and strongify
78+ __weak typeof (self) weakSelf = self;
79+ self.startNotificationObserver = [[NSNotificationCenter defaultCenter ] addObserverForName: AFNetworkingOperationDidStartNotification
80+ object: nil
81+ queue: self .notificationHandlerQueue
82+ usingBlock: ^(NSNotification * _Nonnull notification) {
83+ AFHTTPClientLogger *strongSelf = weakSelf;
84+ [strongSelf operationDidStart: notification];
85+ }];
86+ self.finishNotificationObserver = [[NSNotificationCenter defaultCenter ] addObserverForName: AFNetworkingOperationDidFinishNotification
87+ object: nil
88+ queue: self .notificationHandlerQueue
89+ usingBlock: ^(NSNotification * _Nonnull notification) {
90+ AFHTTPClientLogger *strongSelf = weakSelf;
91+ [strongSelf operationDidFinish: notification];
92+ }];
5993 } else {
60- [[NSNotificationCenter defaultCenter ] removeObserver: self ];
94+ if (self.startNotificationObserver ) {
95+ [[NSNotificationCenter defaultCenter ] removeObserver: self .startNotificationObserver name: AFNetworkingOperationDidStartNotification object: nil ];
96+ }
97+ if (self.finishNotificationObserver ) {
98+ [[NSNotificationCenter defaultCenter ] removeObserver: self .finishNotificationObserver name: AFNetworkingOperationDidFinishNotification object: nil ];
99+ }
61100 }
62101
63102 _enabled = enabled;
@@ -73,36 +112,37 @@ - (void)operationDidStart:(NSNotification *)notification {
73112 if (self.requestStartFormatBlock ) {
74113 NSString *formattedString = self.requestStartFormatBlock (operation, self.level );
75114 if (formattedString) {
76- NSLog (@" %@ " , formattedString);
115+ DDLogError (@" %@ " , formattedString);
77116 }
78117 return ;
79118 }
80119
81120 id body = nil ;
82- if ([operation.request HTTPBody ] && self.level <= AFHTTPClientLogLevelDebug) {
83- body = [NSJSONSerialization JSONObjectWithData: [operation.request HTTPBody ] options: NSJSONReadingAllowFragments error: nil ];
84- if (body == nil ) {
121+ if ([operation.request HTTPBody ] && self.level <= AFHTTPClientLogLevelVerbose) {
122+ NSError *error = nil ;
123+ body = [NSJSONSerialization JSONObjectWithData: [operation.request HTTPBody ] options: NSJSONReadingAllowFragments error: &error];
124+ if (error) {
85125 body = [[NSString alloc ] initWithData: [operation.request HTTPBody ] encoding: NSUTF8StringEncoding];
86126 }
87127 }
88128
89129 switch (self.level ) {
90130 case AFHTTPClientLogLevelVerbose:
91131 if (body) {
92- NSLog (@" >> %@ %@ \n %@ \n %@ " , [operation.request HTTPMethod ], [[operation.request URL ] absoluteString ], [operation.request allHTTPHeaderFields ], body);
132+ DDLogVerbose (@" >> %@ %@ \n %@ \n %@ " , [operation.request HTTPMethod ], [[operation.request URL ] absoluteString ], [operation.request allHTTPHeaderFields ], body);
93133 } else {
94- NSLog (@" >> %@ %@ \n %@ " , [operation.request HTTPMethod ], [[operation.request URL ] absoluteString ], [operation.request allHTTPHeaderFields ]);
134+ DDLogVerbose (@" >> %@ %@ \n %@ " , [operation.request HTTPMethod ], [[operation.request URL ] absoluteString ], [operation.request allHTTPHeaderFields ]);
95135 }
96136 break ;
97137 case AFHTTPClientLogLevelDebug:
98138 if (body) {
99- NSLog (@" >> %@ %@ \n %@ " , [operation.request HTTPMethod ], [[operation.request URL ] absoluteString ], body);
139+ DDLogDebug (@" >> %@ %@ \n %@ " , [operation.request HTTPMethod ], [[operation.request URL ] absoluteString ], body);
100140 } else {
101- NSLog (@" >> %@ %@ " , [operation.request HTTPMethod ], [[operation.request URL ] absoluteString ]);
141+ DDLogDebug (@" >> %@ %@ " , [operation.request HTTPMethod ], [[operation.request URL ] absoluteString ]);
102142 }
103143 break ;
104144 case AFHTTPClientLogLevelInfo:
105- NSLog (@" >> %@ %@ " , [operation.request HTTPMethod ], [[operation.request URL ] absoluteString ]);
145+ DDLogInfo (@" >> %@ %@ " , [operation.request HTTPMethod ], [[operation.request URL ] absoluteString ]);
106146 break ;
107147 default :
108148 break ;
@@ -118,54 +158,55 @@ - (void)operationDidFinish:(NSNotification *)notification {
118158 if (self.requestFinishFormatBlock ) {
119159 NSString *formattedString = self.requestFinishFormatBlock (operation, self.level );
120160 if (formattedString) {
121- NSLog (@" %@ " , formattedString);
161+ DDLogError (@" %@ " , formattedString);
122162 }
123163 return ;
124164 }
125165
126166 NSURL *URL = (operation.response ) ? [operation.response URL ] : [operation.request URL ];
167+ id responseObject = operation.responseObject ;
127168
128169 if (operation.error && operation.error .code == NSURLErrorCancelled) {
129170 switch (self.level ) {
130- case AFHTTPClientLogLevelVerbose:
131- NSLog (@" Cancelled %@ : %@ " , [URL absoluteString ], operation.error );
132- break ;
133- case AFHTTPClientLogLevelDebug:
134- case AFHTTPClientLogLevelInfo:
135- NSLog (@" Cancelled %@ : %@ " , [URL absoluteString ], [operation.error localizedDescription ]);
136- break ;
137- default :
138- break ;
171+ case AFHTTPClientLogLevelVerbose:
172+ DDLogVerbose (@" Canceled %@ : %@ " , [URL absoluteString ], operation.error );
173+ break ;
174+ case AFHTTPClientLogLevelDebug:
175+ case AFHTTPClientLogLevelInfo:
176+ DDLogDebug (@" Canceled %@ : %@ " , [URL absoluteString ], [operation.error localizedDescription ]);
177+ break ;
178+ default :
179+ break ;
139180 }
140181 } else if (operation.error ) {
141182 switch (self.level ) {
142183 case AFHTTPClientLogLevelVerbose:
143- case AFHTTPClientLogLevelDebug:
144- NSLog (@" !! %ld %@ : %@ " , (long )[operation.response statusCode ], [URL absoluteString ], operation.error );
184+ DDLogInfo (@" !! %ld %@ : %@ " , (long )[operation.response statusCode ], [URL absoluteString ], operation.error );
145185 break ;
186+ case AFHTTPClientLogLevelDebug:
146187 case AFHTTPClientLogLevelInfo:
147188 case AFHTTPClientLogLevelError:
148- NSLog (@" !! %ld %@ : %@ " , (long )[operation.response statusCode ], [URL absoluteString ], [operation.error localizedDescription ]);
189+ DDLogError (@" !! %ld %@ : %@ " , (long )[operation.response statusCode ], [URL absoluteString ], [operation.error localizedDescription ]);
149190 break ;
150191 }
151192 } else {
152193 switch (self.level ) {
153194 case AFHTTPClientLogLevelVerbose:
154195 if (operation.responseString ) {
155- NSLog (@" << %ld %@ \n %@ \n %@ " , (long )[operation.response statusCode ], [URL absoluteString ], [operation.response allHeaderFields ], operation. responseObject );
196+ DDLogVerbose (@" << %ld %@ \n %@ \n %@ " , (long )[operation.response statusCode ], [URL absoluteString ], [operation.response allHeaderFields ], responseObject);
156197 } else {
157- NSLog (@" << %ld %@ \n %@ " , (long )[operation.response statusCode ], [URL absoluteString ], [operation.response allHeaderFields ]);
198+ DDLogVerbose (@" << %ld %@ \n %@ " , (long )[operation.response statusCode ], [URL absoluteString ], [operation.response allHeaderFields ]);
158199 }
159200 break ;
160201 case AFHTTPClientLogLevelDebug:
161202 if (operation.responseString ) {
162- NSLog (@" << %ld %@ \n %@ " , (long )[operation.response statusCode ], [URL absoluteString ], operation. responseObject );
203+ DDLogDebug (@" << %ld %@ \n %@ " , (long )[operation.response statusCode ], [URL absoluteString ], responseObject);
163204 } else {
164- NSLog (@" << %ld %@ " , (long )[operation.response statusCode ], [URL absoluteString ]);
205+ DDLogDebug (@" << %ld %@ " , (long )[operation.response statusCode ], [URL absoluteString ]);
165206 }
166207 break ;
167208 case AFHTTPClientLogLevelInfo:
168- NSLog (@" << %ld %@ " , (long )[operation.response statusCode ], [URL absoluteString ]);
209+ DDLogInfo (@" << %ld %@ " , (long )[operation.response statusCode ], [URL absoluteString ]);
169210 break ;
170211 default :
171212 break ;
0 commit comments