From b17b5d9ebf687241f177c562f7db96f7e5e637cb Mon Sep 17 00:00:00 2001 From: "Hoa V. DINH" Date: Wed, 26 Jun 2013 03:19:20 -0700 Subject: Implemented logging API for SMTP, POP and IMAP --- src/objc/smtp/MCOSMTPSession.h | 9 +++++++ src/objc/smtp/MCOSMTPSession.mm | 58 ++++++++++++++++++++++++++++++++++++++--- 2 files changed, 64 insertions(+), 3 deletions(-) (limited to 'src/objc/smtp') diff --git a/src/objc/smtp/MCOSMTPSession.h b/src/objc/smtp/MCOSMTPSession.h index 08189bed..44d37942 100644 --- a/src/objc/smtp/MCOSMTPSession.h +++ b/src/objc/smtp/MCOSMTPSession.h @@ -64,6 +64,15 @@ */ @property (nonatomic, assign, getter=isUseHeloIPEnabled) BOOL useHeloIPEnabled; +/** + Sets logger callback. The network traffic will be sent to this block. + + [session setConnectionLogger:^(void * connectionID, MCOConnectionLogType type, NSData * data) { + ... + }]; + */ +@property (nonatomic, copy) MCOConnectionLogger connectionLogger; + /** @name Operations */ /** diff --git a/src/objc/smtp/MCOSMTPSession.mm b/src/objc/smtp/MCOSMTPSession.mm index de6b54cd..1538d53a 100644 --- a/src/objc/smtp/MCOSMTPSession.mm +++ b/src/objc/smtp/MCOSMTPSession.mm @@ -17,8 +17,34 @@ #import "MCOAddress.h" #import "MCOSMTPOperation+Private.h" +using namespace mailcore; + +@interface MCOSMTPSession () + +- (void) _logWithSender:(void *)sender connectionType:(MCOConnectionLogType)logType data:(NSData *)data; + +@end + +class MCOSMTPConnectionLoggerBridge : public Object, public ConnectionLogger { +public: + MCOSMTPConnectionLoggerBridge(MCOSMTPSession * session) + { + mSession = session; + } + + virtual void log(void * context, void * sender, ConnectionLogType logType, Data * data) + { + [mSession _logWithSender:sender connectionType:(MCOConnectionLogType)logType data:MCO_TO_OBJC(data)]; + } + +private: + MCOSMTPSession * mSession; +}; + @implementation MCOSMTPSession { mailcore::SMTPAsyncSession * _session; + MCOConnectionLogger _connectionLogger; + MCOSMTPConnectionLoggerBridge * _loggerBridge; } #define nativeType mailcore::SMTPAsyncSession @@ -30,13 +56,16 @@ - (id)init { self = [super init]; - if (self) { - _session = new mailcore::SMTPAsyncSession(); - } + + _session = new mailcore::SMTPAsyncSession(); + _loggerBridge = new MCOSMTPConnectionLoggerBridge(self); + return self; } - (void)dealloc { + MC_SAFE_RELEASE(_loggerBridge); + [_connectionLogger release]; _session->release(); [super dealloc]; } @@ -51,6 +80,24 @@ MCO_OBJC_SYNTHESIZE_SCALAR(NSTimeInterval, time_t, setTimeout, timeout) MCO_OBJC_SYNTHESIZE_BOOL(setCheckCertificateEnabled, isCheckCertificateEnabled) MCO_OBJC_SYNTHESIZE_BOOL(setUseHeloIPEnabled, useHeloIPEnabled) +- (void) setConnectionLogger:(MCOConnectionLogger)connectionLogger +{ + [_connectionLogger release]; + _connectionLogger = [connectionLogger copy]; + + if (_connectionLogger != nil) { + _session->setConnectionLogger(_loggerBridge); + } + else { + _session->setConnectionLogger(NULL); + } +} + +- (MCOConnectionLogger) connectionLogger +{ + return _connectionLogger; +} + #pragma mark - Operations - (MCOSMTPSendOperation *) sendOperationWithData:(NSData *)messageData @@ -69,4 +116,9 @@ MCO_OBJC_SYNTHESIZE_BOOL(setUseHeloIPEnabled, useHeloIPEnabled) return result; } +- (void) _logWithSender:(void *)sender connectionType:(MCOConnectionLogType)logType data:(NSData *)data +{ + _connectionLogger(sender, logType, data); +} + @end -- cgit v1.2.3