Permalink
Browse files

Write test logs to disk

Each test run's output is now saved as a file to the app's Documents directory (accessible via iTunes).
  • Loading branch information...
1 parent 0e36037 commit e82d36e7f5bac9de37146cb61d78653531be719e @snej snej committed Oct 6, 2011
Showing with 117 additions and 62 deletions.
  1. +114 −61 App/BeeTest.m
  2. +2 −0 App/Info.plist
  3. +1 −1 Worker Bee.xcodeproj/project.pbxproj
View
@@ -10,18 +10,22 @@
#import <objc/runtime.h>
+/** Number of most-recent messages to keep in the messages property */
#define kMaxMessageCount 100
@interface BeeTest ()
{
BOOL _running;
+ NSOutputStream* _output;
NSMutableArray* _messages;
NSTimer* _heartbeat;
}
@property (readwrite, retain) NSDate* startTime;
@property (readwrite, retain) NSDate* endTime;
@property (retain) NSString* lastTimestamp;
+- (void) openOutput;
+- (void) closeOutput;
@end
@@ -67,7 +71,8 @@ + (NSString*) displayName {
}
-@synthesize delegate=_delegate, status = _status, startTime = _startTime, endTime = _endTime, error = _error, messages = _messages, lastTimestamp = _lastTimestamp;
+@synthesize delegate=_delegate, status = _status, startTime = _startTime, endTime = _endTime,
+ error = _error, messages = _messages, lastTimestamp = _lastTimestamp;
- (id)init {
@@ -79,6 +84,8 @@ - (id)init {
}
- (void)dealloc {
+ [_output close];
+ [_output release];
[_messages release];
[_lastTimestamp release];
[_startTime release];
@@ -122,6 +129,9 @@ - (NSString*) errorMessage {
}
+#pragma mark - START / STOP:
+
+
- (BOOL) running {
return _running;
}
@@ -135,6 +145,7 @@ - (void) setRunning:(BOOL)run {
self.error = nil;
self.status = nil;
[self clearMessages];
+ [self openOutput];
} else {
self.endTime = [NSDate date];
self.status = nil;
@@ -144,11 +155,109 @@ - (void) setRunning:(BOOL)run {
if (run)
[self setUp];
- else
+ else {
[self tearDown];
+ [self closeOutput];
+ }
+ }
+}
+
+
+- (void) setUp {
+ NSNotificationCenter* nctr = [NSNotificationCenter defaultCenter];
+ [nctr addObserver: self
+ selector: @selector(applicationDidEnterBackground:)
+ name: UIApplicationDidEnterBackgroundNotification
+ object: nil];
+ [nctr addObserver: self
+ selector: @selector(applicationWillEnterForeground:)
+ name: UIApplicationWillEnterForegroundNotification
+ object: nil];
+}
+
+- (void) tearDown {
+ self.heartbeatInterval = 0.0;
+ NSNotificationCenter* nctr = [NSNotificationCenter defaultCenter];
+ [nctr removeObserver: self
+ name:UIApplicationDidEnterBackgroundNotification
+ object: nil];
+ [nctr removeObserver: self
+ name:UIApplicationWillEnterForegroundNotification
+ object: nil];
+}
+
+
+- (void)applicationDidEnterBackground: (NSNotification*)notification
+{
+ [self addTimestamp: @"BACKGROUND"];
+}
+
+
+- (void)applicationWillEnterForeground: (NSNotification*)notification
+{
+ [self addTimestamp: @"FOREGROUND"];
+}
+
+
+#pragma - HEARTBEAT:
+
+- (NSTimeInterval) heartbeatInterval {
+ return _heartbeat ? [_heartbeat timeInterval] : 0.0;
+}
+
+- (void) setHeartbeatInterval: (NSTimeInterval)interval {
+ [_heartbeat invalidate];
+ [_heartbeat release];
+ if (interval > 0) {
+ _heartbeat = [[NSTimer scheduledTimerWithTimeInterval: interval
+ target: self
+ selector: @selector(heartbeat)
+ userInfo: NULL
+ repeats: YES] retain];
+ } else {
+ _heartbeat = nil;
}
}
+
+- (void) heartbeat {
+}
+
+
+#pragma mark - LOGGING:
+
+- (void) openOutput {
+ NSAssert(!_output, @"_output was left open");
+ NSString* filename = [NSString stringWithFormat: @"%@ %@.txt",
+ [self class], [RESTBody JSONObjectWithDate: [NSDate date]]];
+ NSString* docsDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
+ NSUserDomainMask, YES)
+ objectAtIndex: 0];
+ NSString* logPath = [docsDir stringByAppendingPathComponent: filename];
+ NSLog(@"**OPENING %@", logPath);
+ _output = [[NSOutputStream alloc] initToFileAtPath: logPath append: NO];
+ [_output open];
+}
+
+- (void) closeOutput {
+ NSLog(@"** CLOSING %@", [self class]);
+ [_output close];
+ [_output release];
+ _output = nil;
+}
+
+- (void) writeToOutput: (NSString*)message {
+ NSAssert(_output, @"Output isn't open");
+ NSData* data = [message dataUsingEncoding: NSUTF8StringEncoding];
+ NSInteger written = [_output write: data.bytes maxLength: data.length];
+ if (written < 0)
+ NSLog(@"ERROR: Can't write to log: %@", _output.streamError);
+ else
+ NSAssert(written == data.length, @"Only wrote %i bytes of %u", written, data.length);
+ [_output write: (const uint8_t*)"\n" maxLength: 1];
+}
+
+
- (NSString*) shortTimestamp {
static NSDateFormatter* sFormat;
if (!sFormat) {
@@ -180,14 +289,17 @@ - (BOOL) addTimestamp: (NSString*)message {
message = [NSString stringWithFormat: @"---- %@ %@",
self.fullTimestamp, (message ? message : @"")];
[_messages addObject: message];
+ [self writeToOutput: message];
return YES;
}
+
- (void) logMessage:(NSString *)message {
[self addTimestamp: nil];
[_messages addObject: message];
if (_messages.count > kMaxMessageCount)
[_messages removeObjectAtIndex: 0];
+ [self writeToOutput: message];
[_delegate beeTest: self loggedMessage: message];
}
@@ -206,63 +318,4 @@ - (void) clearMessages {
}
-- (void) setUp {
- NSNotificationCenter* nctr = [NSNotificationCenter defaultCenter];
- [nctr addObserver: self
- selector: @selector(applicationDidEnterBackground:)
- name: UIApplicationDidEnterBackgroundNotification
- object: nil];
- [nctr addObserver: self
- selector: @selector(applicationWillEnterForeground:)
- name: UIApplicationWillEnterForegroundNotification
- object: nil];
-}
-
-- (void) tearDown {
- self.heartbeatInterval = 0.0;
- NSNotificationCenter* nctr = [NSNotificationCenter defaultCenter];
- [nctr removeObserver: self
- name:UIApplicationDidEnterBackgroundNotification
- object: nil];
- [nctr removeObserver: self
- name:UIApplicationWillEnterForegroundNotification
- object: nil];
-}
-
-
-- (void)applicationDidEnterBackground: (NSNotification*)notification
-{
- [self addTimestamp: @"BACKGROUND"];
-}
-
-
-- (void)applicationWillEnterForeground: (NSNotification*)notification
-{
- [self addTimestamp: @"FOREGROUND"];
-}
-
-
-- (NSTimeInterval) heartbeatInterval {
- return _heartbeat ? [_heartbeat timeInterval] : 0.0;
-}
-
-- (void) setHeartbeatInterval: (NSTimeInterval)interval {
- [_heartbeat invalidate];
- [_heartbeat release];
- if (interval > 0) {
- _heartbeat = [[NSTimer scheduledTimerWithTimeInterval: interval
- target: self
- selector: @selector(heartbeat)
- userInfo: NULL
- repeats: YES] retain];
- } else {
- _heartbeat = nil;
- }
-}
-
-
-- (void) heartbeat {
-}
-
-
@end
View
@@ -28,6 +28,8 @@
<true/>
<key>NSMainNibFile</key>
<string>Application</string>
+ <key>UIFileSharingEnabled</key>
+ <true/>
<key>UISupportedInterfaceOrientations</key>
<array>
<string>UIInterfaceOrientationPortrait</string>
@@ -35,7 +35,7 @@
/* Begin PBXFileReference section */
27A07486143CCDCD0043BEB6 /* BeeCouchTest.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BeeCouchTest.h; sourceTree = "<group>"; };
27A07487143CCDCD0043BEB6 /* BeeCouchTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BeeCouchTest.m; sourceTree = "<group>"; };
- 27A0748A143CDE6B0043BEB6 /* CreateDocsTest.h */ = {isa = PBXFileReference; fileEncoding = 4; path = CreateDocsTest.h; sourceTree = "<group>"; };
+ 27A0748A143CDE6B0043BEB6 /* CreateDocsTest.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CreateDocsTest.h; sourceTree = "<group>"; };
27A0748B143CDE6B0043BEB6 /* CreateDocsTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CreateDocsTest.m; sourceTree = "<group>"; };
27B28FA114314FB800B86F18 /* Worker Bee.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "Worker Bee.app"; sourceTree = BUILT_PRODUCTS_DIR; };
27B28FA514314FB800B86F18 /* UIKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = UIKit.framework; path = System/Library/Frameworks/UIKit.framework; sourceTree = SDKROOT; };

0 comments on commit e82d36e

Please sign in to comment.