From eec8122b954c619042ad19973eccd34322112254 Mon Sep 17 00:00:00 2001 From: Ricardo Pereira Date: Tue, 2 May 2017 14:58:58 +0100 Subject: [PATCH 1/4] Log history --- Source/ARTLog+Private.h | 6 ++++-- Source/ARTLog.m | 45 ++++++++++++++++++++++++++++++++++++++--- Spec/Utilities.swift | 21 +++++++++++++++++++ 3 files changed, 67 insertions(+), 5 deletions(-) diff --git a/Source/ARTLog+Private.h b/Source/ARTLog+Private.h index d69180df9..43207e7e7 100644 --- a/Source/ARTLog+Private.h +++ b/Source/ARTLog+Private.h @@ -13,7 +13,7 @@ ART_ASSUME_NONNULL_BEGIN -@interface ARTLogLine : NSObject +@interface ARTLogLine : NSObject @property(nonatomic, readonly, strong) NSDate *date; @property(nonatomic, readonly) ARTLogLevel level; @@ -27,9 +27,11 @@ ART_ASSUME_NONNULL_BEGIN @interface ARTLog () -@property(readonly, getter=getCaptured) __GENERIC(NSArray, ARTLogLine *) *captured; +@property (readonly) NSArray *captured; +@property (readonly) NSArray *history; - (instancetype)initCapturingOutput:(BOOL)capturing; +- (instancetype)initCapturingOutput:(BOOL)capturing historyLines:(NSUInteger)historyLines; @end diff --git a/Source/ARTLog.m b/Source/ARTLog.m index c98ab0717..624840db6 100644 --- a/Source/ARTLog.m +++ b/Source/ARTLog.m @@ -43,23 +43,54 @@ - (NSString *)toString { return [NSString stringWithFormat:@"%s: %@", logLevelName(self.level), self.message]; } +- (NSString *)description { + return [self toString]; +} + +#pragma mark - NSCoding + +- (id)initWithCoder:(NSCoder *)decoder { + self = [super init]; + if (!self) { + return nil; + } + _date = [decoder decodeObjectForKey:@"date"]; + _level = [[decoder decodeObjectForKey:@"level"] unsignedIntValue]; + _message = [decoder decodeObjectForKey:@"message"]; + return self; +} + +- (void)encodeWithCoder:(NSCoder *)encoder { + [encoder encodeObject:self.date forKey:@"date"]; + [encoder encodeObject:[NSNumber numberWithUnsignedInteger:self.level] forKey:@"level"]; + [encoder encodeObject:self.message forKey:@"message"]; +} + @end @implementation ARTLog { - NSMutableArray *_captured; + NSMutableArray *_captured; + NSMutableArray *_history; + NSUInteger _historyLines; } - (instancetype)init { - return [self initCapturingOutput:false]; + return [self initCapturingOutput:true]; } - (instancetype)initCapturingOutput:(BOOL)capturing { + return [self initCapturingOutput:true historyLines:10]; +} + +- (instancetype)initCapturingOutput:(BOOL)capturing historyLines:(NSUInteger)historyLines { if (self = [super init]) { // Default self->_logLevel = ARTLogLevelWarn; if (capturing) { self->_captured = [[NSMutableArray alloc] init]; } + _history = [[NSMutableArray alloc] init]; + _historyLines = historyLines; } return self; } @@ -72,9 +103,17 @@ - (void)log:(NSString *)message level:(ARTLogLevel)level { [_captured addObject:logLine]; } } + [_history insertObject:logLine atIndex:0]; + if (_history.count > _historyLines) { + [_history removeLastObject]; + } +} + +- (NSArray *)history { + return _history; } -- (NSArray *)getCaptured { +- (NSArray *)captured { if (!_captured) { [NSException raise:NSInternalInconsistencyException format:@"tried to get captured output in non-capturing instance; use initCapturingOutput:true if you want captured output."]; } diff --git a/Spec/Utilities.swift b/Spec/Utilities.swift index 56eebb694..75033f347 100644 --- a/Spec/Utilities.swift +++ b/Spec/Utilities.swift @@ -386,6 +386,27 @@ class Utilities: QuickSpec { } } } + + context("Logger") { + + it("should have a history of logs") { + let options = AblyTests.commonAppSetup() + let realtime = ARTRealtime(options: options) + let channel = realtime.channels.get("foo") + + waitUntil(timeout: testTimeout) { done in + channel.attach { error in + expect(error).to(beNil()) + done() + } + } + + expect(realtime.logger.history).to(haveCount(10)) + expect(realtime.logger.history.map{ $0.message }.first).to(contain("channel state transitions to 2 - Attached")) + expect(realtime.logger.history.filter{ $0.message.containsString("realtime state transitions to 2 - Connected") }).to(haveCount(1)) + } + + } } } } From d7e1b701d7bb0108bed3cb0fae3f9902a190e53a Mon Sep 17 00:00:00 2001 From: Ricardo Pereira Date: Tue, 2 May 2017 19:15:21 +0100 Subject: [PATCH 2/4] fixup! Log history --- Spec/Utilities.swift | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Spec/Utilities.swift b/Spec/Utilities.swift index 75033f347..0b0976de9 100644 --- a/Spec/Utilities.swift +++ b/Spec/Utilities.swift @@ -79,6 +79,7 @@ class Utilities: QuickSpec { it("should handle and emit the invalid data error") { let options = AblyTests.commonAppSetup() let realtime = ARTRealtime(options: options) + defer { realtime.close() } let channel = realtime.channels.get("foo") waitUntil(timeout: testTimeout) { done in channel.publish("test", data: NSDate()) { error in @@ -105,6 +106,7 @@ class Utilities: QuickSpec { it("should ignore invalid transport message") { let options = AblyTests.commonAppSetup() let realtime = ARTRealtime(options: options) + defer { realtime.close() } let channel = realtime.channels.get("foo") // Garbage values (whatever is on the heap) @@ -392,6 +394,7 @@ class Utilities: QuickSpec { it("should have a history of logs") { let options = AblyTests.commonAppSetup() let realtime = ARTRealtime(options: options) + defer { realtime.close() } let channel = realtime.channels.get("foo") waitUntil(timeout: testTimeout) { done in From c3afa25b3ad9ddcbf83804592558805156e4ee63 Mon Sep 17 00:00:00 2001 From: Ricardo Pereira Date: Wed, 3 May 2017 08:15:36 +0100 Subject: [PATCH 3/4] fixup! Log history --- Source/ARTLog.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Source/ARTLog.m b/Source/ARTLog.m index 624840db6..06e3df402 100644 --- a/Source/ARTLog.m +++ b/Source/ARTLog.m @@ -79,7 +79,7 @@ - (instancetype)init { } - (instancetype)initCapturingOutput:(BOOL)capturing { - return [self initCapturingOutput:true historyLines:10]; + return [self initCapturingOutput:true historyLines:100]; } - (instancetype)initCapturingOutput:(BOOL)capturing historyLines:(NSUInteger)historyLines { From 71260ee80ba3049305c2853dc2a9b01dd1e31973 Mon Sep 17 00:00:00 2001 From: Ricardo Pereira Date: Wed, 3 May 2017 13:21:39 +0100 Subject: [PATCH 4/4] fixup! Log history --- Spec/Utilities.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Spec/Utilities.swift b/Spec/Utilities.swift index 0b0976de9..55439cc53 100644 --- a/Spec/Utilities.swift +++ b/Spec/Utilities.swift @@ -404,7 +404,7 @@ class Utilities: QuickSpec { } } - expect(realtime.logger.history).to(haveCount(10)) + expect(realtime.logger.history.count).toNot(beGreaterThan(100)) expect(realtime.logger.history.map{ $0.message }.first).to(contain("channel state transitions to 2 - Attached")) expect(realtime.logger.history.filter{ $0.message.containsString("realtime state transitions to 2 - Connected") }).to(haveCount(1)) }