Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

ispdy: introduce logging delegate

  • Loading branch information...
commit 34b368959bc94580aa9742bf0477e5a7a994223b 1 parent 6dd32a8
@indutny indutny authored
Showing with 51 additions and 0 deletions.
  1. +20 −0 include/ispdy.h
  2. +3 −0  src/common.h
  3. +28 −0 src/ispdy.m
View
20 include/ispdy.h
@@ -85,6 +85,15 @@ typedef enum {
} ISpdyCheckStatus;
/**
+ * Log levels
+ */
+typedef enum {
+ kISpdyLogInfo,
+ kISpdyLogWarning,
+ kISpdyLogError
+} ISpdyLogLevel;
+
+/**
* Callback for ping method.
*/
typedef void (^ISpdyPingCallback)(ISpdyPingStatus status, NSTimeInterval rtt);
@@ -287,6 +296,17 @@ typedef void (^ISpdyPingCallback)(ISpdyPingStatus status, NSTimeInterval rtt);
@end
+/**
+ * Delegate for handling connection-level log events
+ */
+@protocol ISpdyLogDelegate
+
+- (void) logSpdyEvents: (ISpdy*) conn
+ level: (ISpdyLogLevel) level
+ message: (NSString*) message;
+
+@end
+
/** ISpdy connection class
*
* Connects to server and holds underlying socket, parsing incoming data and
View
3  src/common.h
@@ -132,6 +132,9 @@ typedef enum {
ISpdyParserDelegate,
ISpdySchedulerDelegate>
+// Invoke delegate logging if present
+- (void) _log: (ISpdyLogLevel) level format: (NSString*) msg, ...;
+
// Get fd out of streams
- (void) _fdWithBlock: (void(^)(CFSocketNativeHandle)) block;
View
28 src/ispdy.m
@@ -25,6 +25,7 @@
#import <dispatch/dispatch.h> // dispatch_queue_t
#import <netinet/in.h> // IPPROTO_TCP
#import <netinet/tcp.h> // TCP_NODELAY
+#import <stdarg.h> // va_start, va_end
#import <string.h> // memmove
#import <sys/socket.h> // setsockopt
#import <errno.h> // errno
@@ -506,6 +507,26 @@ - (void) _connectionDispatchSync: (void (^)()) block {
}
+- (void) _log: (ISpdyLogLevel) level format: (NSString*) format, ... {
+ if (self.delegate == nil)
+ return;
+
+ NSObject* d = (NSObject*) self.delegate;
@georgekola Owner

It would be better to make delegate implement a protocol that includes multiple protocols

@protocol ISpdyDelegate
@end

This way, you do not need the multiple castings

http://stackoverflow.com/questions/10549374/can-a-delegate-property-support-multiple-protocols

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ if (![d respondsToSelector: @selector(logSpdyEvents:level:message:)])
+ return;
+
+ va_list args;
+ va_start(args, format);
+ NSString* str = [[NSString alloc] initWithFormat: format arguments: args];
+ va_end(args);
+
+ id<ISpdyLogDelegate> ld = (id<ISpdyLogDelegate>) d;
+ [self _delegateDispatch: ^{
+ [ld logSpdyEvents: self level: level message: str];
+ }];
+}
+
+
- (void) _fdWithBlock: (void(^)(CFSocketNativeHandle)) block {
CFDataRef data =
CFWriteStreamCopyProperty((__bridge CFWriteStreamRef) out_stream_,
@@ -978,6 +999,13 @@ - (BOOL) _checkPinnedCertificates: (NSStream*) stream {
// NSSocket delegate methods
- (void) stream: (NSStream*) stream handleEvent: (NSStreamEvent) event {
+ if (event == NSStreamEventOpenCompleted) {
+ [self _log: kISpdyLogInfo format: @"NSStream open"];
+ } else if (event == NSStreamEventEndEncountered) {
+ [self _log: kISpdyLogInfo format: @"NSStream end"];
+ } else if (event == NSStreamEventErrorOccurred) {
+ [self _log: kISpdyLogWarning format: @"NSStream error"];
+ }
[self _connectionDispatchSync: ^{
// Already closed, just return
if (in_stream_ == nil || out_stream_ == nil)
Please sign in to comment.
Something went wrong with that request. Please try again.