Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions CoreZenTests/Classes/TestLog.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,13 @@

NS_ASSUME_NONNULL_BEGIN

// Everything this process has logged since date, oldest first, CoreZen's messages
// Logs a message that no other entry has, and returns it. A test calls this
// before what it checks, then reads the entries after it with ZENLogEntriesAfter.
NSString *ZENLogMarker(void);

// Everything this process logged after marker, oldest first, CoreZen's messages
// and others'. Reading the log takes a second or two, so a test reads it once.
NSArray<OSLogEntryLog *> *ZENLogEntriesSince(NSDate *date);
NSArray<OSLogEntryLog *> *ZENLogEntriesAfter(NSString *marker);

// The entries CoreZen logged in category, at level, whose message contains text
NSArray<OSLogEntryLog *> *ZENCoreZenLogEntries(NSArray<OSLogEntryLog *> *entries,
Expand Down
48 changes: 37 additions & 11 deletions CoreZenTests/Classes/TestLog.m
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,49 @@

#import "TestLog.h"

NSArray<OSLogEntryLog *> *ZENLogEntriesSince(NSDate *date) {
#import <os/log.h>

NSString *ZENLogMarker(void) {
static os_log_t log;
static dispatch_once_t once;
dispatch_once(&once, ^{
log = os_log_create("com.zdnelson.CoreZenTests", "Marker");
});
NSString *marker = [NSString stringWithFormat:@"Marker %@", NSUUID.UUID.UUIDString];
os_log(log, "%{public}@", marker);
return marker;
}

// A test finds its entries by a marker instead of by the time it started,
// because the unified log keeps time differently from NSDate. It dates entries
// from the system's monotonic clock, converted to the time of day, while NSDate
// reads the time of day itself, which the system speeds up or slows down to
// correct it. An entry logged just after [NSDate date] could then be dated a few
// milliseconds before it. The log's own order doesn't depend on that.
NSArray<OSLogEntryLog *> *ZENLogEntriesAfter(NSString *marker) {
NSError *error = nil;
OSLogStore *store = [OSLogStore storeWithScope:OSLogStoreCurrentProcessIdentifier error:&error];
if (!store) {
NSLog(@"Couldn't open this process's log: %@", error);
OSLogEnumerator *entries = [store entriesEnumeratorWithOptions:0 position:nil predicate:nil error:&error];
if (!entries) {
NSLog(@"Couldn't read this process's log: %@", error);
return @[];
}
OSLogEnumerator *entries = [store entriesEnumeratorWithOptions:0
position:[store positionWithDate:date]
predicate:nil
error:&error];
NSMutableArray<OSLogEntryLog *> *logEntries = [NSMutableArray array];
// Stays nil until the marker
NSMutableArray<OSLogEntryLog *> *logEntries = nil;
for (OSLogEntry *entry in entries) {
// The position is approximate, so it can start a little early
if ([entry isKindOfClass:[OSLogEntryLog class]] && [entry.date compare:date] != NSOrderedAscending) {
[logEntries addObject:(OSLogEntryLog *)entry];
if (![entry isKindOfClass:[OSLogEntryLog class]]) {
continue;
}
OSLogEntryLog *logEntry = (OSLogEntryLog *)entry;
if (logEntries) {
[logEntries addObject:logEntry];
} else if ([logEntry.composedMessage isEqualToString:marker]) {
logEntries = [NSMutableArray array];
}
}
if (!logEntries) {
NSLog(@"Couldn't find \"%@\" in this process's log", marker);
return @[];
}
return logEntries;
}
Expand Down
4 changes: 2 additions & 2 deletions CoreZenTests/DatabaseTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -892,13 +892,13 @@ - (void)testForeignKeysAreNotEnforcedByDefault {
#pragma mark - Logging

- (void)testDatabaseLogsUnderCoreZensSubsystem {
NSDate *start = [NSDate date];
NSString *marker = ZENLogMarker();
ZENDatabaseQueue *queue = [ZENDatabaseQueue databaseQueueWithURL:[self temporaryDatabaseURL]];
[queue fetchSync:^(ZENDatabase *database) {
XCTAssertNil([database executeQuery:@"SELECT name FROM no_such_table;"]);
}];
[queue shutdown];
NSArray<OSLogEntryLog *> *entries = ZENLogEntriesSince(start);
NSArray<OSLogEntryLog *> *entries = ZENLogEntriesAfter(marker);

// Something routine is info, and a failure is an error, with the SQL that failed
XCTAssertEqual(ZENCoreZenLogEntries(entries, @"Database", OSLogEntryLogLevelInfo, @"Opening the database at").count, 1u);
Expand Down
4 changes: 2 additions & 2 deletions CoreZenTests/MediaPlayerTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -252,13 +252,13 @@ - (void)testFileThatIsNotVideoFails {
}

- (void)testMpvErrorsAreLoggedAsErrors {
NSDate *start = [NSDate date];
NSString *marker = ZENLogMarker();
[self.player loadFileURL:[self fileThatIsNotVideo]];
[self waitForStatus:ZENMediaPlayerStatusFailed];

// mpv logs why it couldn't play the file, at its error level. The reason
// itself is private, so it can show as <private>.
NSArray<OSLogEntryLog *> *errors = ZENCoreZenLogEntries(ZENLogEntriesSince(start), @"Player", OSLogEntryLogLevelError, @"mpv [cplayer] error: ");
NSArray<OSLogEntryLog *> *errors = ZENCoreZenLogEntries(ZENLogEntriesAfter(marker), @"Player", OSLogEntryLogLevelError, @"mpv [cplayer] error: ");
XCTAssertGreaterThan(errors.count, 0u);
}

Expand Down