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
22 changes: 22 additions & 0 deletions CoreZen.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
objects = {

/* Begin PBXBuildFile section */
DD0000020000000000000001 /* Log.h in Headers */ = {isa = PBXBuildFile; fileRef = DD0000010000000000000001 /* Log.h */; };
DD0000020000000000000002 /* Log.m in Sources */ = {isa = PBXBuildFile; fileRef = DD0000010000000000000002 /* Log.m */; };
DD0000020000000000000004 /* TestLog.m in Sources */ = {isa = PBXBuildFile; fileRef = DD0000010000000000000004 /* TestLog.m */; };
10E3163392F12AF956341355 /* licenses in Resources */ = {isa = PBXBuildFile; fileRef = CF0E6577750FB6561C55DB40 /* licenses */; };
23E8171B92C816C235C1B1B2 /* FMDB-LICENSE.txt in Resources */ = {isa = PBXBuildFile; fileRef = A8C4E7FED8E6976453047D72 /* FMDB-LICENSE.txt */; };
AA0000020000000000000001 /* Database.h in Headers */ = {isa = PBXBuildFile; fileRef = AA0000010000000000000001 /* Database.h */; settings = {ATTRIBUTES = (Public, ); }; };
Expand Down Expand Up @@ -186,6 +189,10 @@
/* End PBXCopyFilesBuildPhase section */

/* Begin PBXFileReference section */
DD0000010000000000000001 /* Log.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = Log.h; sourceTree = "<group>"; };
DD0000010000000000000002 /* Log.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = Log.m; sourceTree = "<group>"; };
DD0000010000000000000003 /* TestLog.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = TestLog.h; sourceTree = "<group>"; };
DD0000010000000000000004 /* TestLog.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = TestLog.m; sourceTree = "<group>"; };
CF0E6577750FB6561C55DB40 /* licenses */ = {isa = PBXFileReference; lastKnownFileType = folder; path = licenses; sourceTree = "<group>"; };
A8C4E7FED8E6976453047D72 /* FMDB-LICENSE.txt */ = {isa = PBXFileReference; lastKnownFileType = text; path = "FMDB-LICENSE.txt"; sourceTree = "<group>"; };
AA0000010000000000000001 /* Database.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = Database.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -500,6 +507,15 @@
/* End PBXFrameworksBuildPhase section */

/* Begin PBXGroup section */
DD0000030000000000000001 /* Logging */ = {
isa = PBXGroup;
children = (
DD0000010000000000000001 /* Log.h */,
DD0000010000000000000002 /* Log.m */,
);
path = Logging;
sourceTree = "<group>";
};
5309C39F2885A1DC00BC0AAE = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -528,6 +544,7 @@
5309C4112885B66000BC0AAE /* Database */,
538E05002885DCD300CE9DE7 /* Domain */,
5309C3EE2885A6C800BC0AAE /* Identifier */,
DD0000030000000000000001 /* Logging */,
5350F8BF2886192E00F8CA68 /* Media */,
5350FB0328889E5F00F8CA68 /* Node */,
5309C3E52885A37900BC0AAE /* Preferences */,
Expand Down Expand Up @@ -703,6 +720,8 @@
538E052D2885FB5400CE9DE7 /* TestIdentifiable.m */,
CC00000128860A0000000006 /* TestTable.h */,
CC00000128860A0000000007 /* TestTable.m */,
DD0000010000000000000003 /* TestLog.h */,
DD0000010000000000000004 /* TestLog.m */,
);
path = Classes;
sourceTree = "<group>";
Expand Down Expand Up @@ -1063,6 +1082,7 @@
5309C4292885D19400BC0AAE /* DatabaseTable.h in Headers */,
AA0000020000000000000004 /* ResultSet.h in Headers */,
AA0000020000000000000006 /* ResultSet+Private.h in Headers */,
DD0000020000000000000001 /* Log.h in Headers */,
5350F8C22886194200F8CA68 /* MediaFile.h in Headers */,
5309C3E82885A39C00BC0AAE /* PreferencesWindowController.h in Headers */,
538E05072885DD1D00CE9DE7 /* DomainCallbacks.h in Headers */,
Expand Down Expand Up @@ -1227,6 +1247,7 @@
5309C3E92885A39C00BC0AAE /* PreferencesWindowController.m in Sources */,
53964F0B288C4C48007B53EC /* MPVPlayerController.m in Sources */,
5331FE622922AD8700C15950 /* WorkQueue.m in Sources */,
DD0000020000000000000002 /* Log.m in Sources */,
5309C4222885CB1400BC0AAE /* DataTransferObject.m in Sources */,
5386706C28BFE16300FB15EB /* MediaPlayerControlsView.m in Sources */,
5396531D288CD19C007B53EC /* MediaPlayerView.m in Sources */,
Expand Down Expand Up @@ -1277,6 +1298,7 @@
CC00000228860A0000000006 /* TestDomainObject.m in Sources */,
538E052E2885FB5400CE9DE7 /* TestIdentifiable.m in Sources */,
CC00000228860A0000000005 /* TestTable.m in Sources */,
DD0000020000000000000004 /* TestLog.m in Sources */,
0313158D2D7374094EFBE6DC /* MediaTests.m in Sources */,
B87FBFD38A588272C385E7BC /* MPVFunctionsTests.m in Sources */,
0E03AE27A6C781F30909CE0A /* MediaPlayerTests.m in Sources */,
Expand Down
3 changes: 2 additions & 1 deletion CoreZen/Categories/NSFileManager+CoreZen.m
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//

#import "NSFileManager+CoreZen.h"
#import "Log.h"

@implementation NSFileManager (CoreZen)

Expand Down Expand Up @@ -41,7 +42,7 @@ - (NSURL *)zen_applicationSupportDirectory {
NSError *error;
NSURL *url = [self zen_findOrCreateURLForDirectory:NSApplicationSupportDirectory inDomain:NSUserDomainMask appendPathComponent:executableName create:YES error:&error];
if (!url) {
NSLog(@"Unable to find or create application support directory: %@", error);
os_log_error(ZENLogGeneral(), "Couldn't find or create the application support folder: %@", error.localizedDescription);
}
return url;
}
Expand Down
4 changes: 2 additions & 2 deletions CoreZen/Database/Database+Private.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ NSError *ZENDatabaseErrorMake(ZENDatabaseErrorCode code, NSString *description);
// it failed, or a block canceled it), or nil if it can commit
- (NSError *)rollbackError;

// Records a failed statement, which the open transaction must roll back for.
// Logs a failed statement, and records it, so the open transaction rolls back.
// ZENResultSet calls it when stepping through rows fails.
- (void)statementFailed;
- (void)statementFailed:(NSString *)sql;

- (void)setShouldCacheStatements:(BOOL)value;
- (void)setTraceExecution:(BOOL)value;
Expand Down
21 changes: 13 additions & 8 deletions CoreZen/Database/Database.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#import "Database.h"
#import "Database+Private.h"
#import "ResultSet+Private.h"
#import "Log.h"

#import <sqlite3.h>

Expand Down Expand Up @@ -48,24 +49,24 @@ - (BOOL)executeUpdate:(NSString *)sql, ... {
BOOL result = [self.fmDatabase executeUpdate:sql withVAList:args];
va_end(args);
if (!result) {
[self statementFailed];
[self statementFailed:sql];
}
return result;
}

- (BOOL)executeUpdate:(NSString *)sql withArgumentsInArray:(NSArray *)arguments {
BOOL result = [self.fmDatabase executeUpdate:sql withArgumentsInArray:arguments];
if (!result) {
[self statementFailed];
[self statementFailed:sql];
}
return result;
}

#pragma mark - Queries

- (ZENResultSet *)wrapResultSet:(FMResultSet *)rs {
- (ZENResultSet *)wrapResultSet:(FMResultSet *)rs sql:(NSString *)sql {
if (!rs) {
[self statementFailed];
[self statementFailed:sql];
return nil;
}
return [ZENResultSet resultSetWithFMResultSet:rs database:self];
Expand All @@ -76,18 +77,18 @@ - (ZENResultSet *)executeQuery:(NSString *)sql, ... {
va_start(args, sql);
FMResultSet *rs = [self.fmDatabase executeQuery:sql withVAList:args];
va_end(args);
return [self wrapResultSet:rs];
return [self wrapResultSet:rs sql:sql];
}

- (ZENResultSet *)executeQuery:(NSString *)sql withArgumentsInArray:(NSArray *)arguments {
return [self wrapResultSet:[self.fmDatabase executeQuery:sql withArgumentsInArray:arguments]];
return [self wrapResultSet:[self.fmDatabase executeQuery:sql withArgumentsInArray:arguments] sql:sql];
}

#pragma mark - Transactions

- (void)cancelTransaction {
if (!self.inTransaction) {
NSLog(@"WARNING: cancelTransaction called outside a transaction");
os_log(ZENLogDatabase(), "cancelTransaction was called outside a transaction, so it did nothing");
return;
}
if (!_rollbackError) {
Expand All @@ -106,7 +107,11 @@ - (NSError *)rollbackError {
return _rollbackError;
}

- (void)statementFailed {
- (void)statementFailed:(NSString *)sql {
// FMDB's own logging of this is off, so the message goes under CoreZen's
// subsystem. The statement's values are bound separately, so it holds only SQL.
os_log_error(ZENLogDatabase(), "A statement failed: %{public}@\n%{public}@", self.fmDatabase.lastErrorMessage, sql);

// Outside a transaction a failed statement has nothing to undo
if (!_rollbackError && self.inTransaction) {
NSString *description = [NSString stringWithFormat:@"A statement failed: %@", self.fmDatabase.lastErrorMessage];
Expand Down
30 changes: 18 additions & 12 deletions CoreZen/Database/DatabaseQueue.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#import "DatabaseQueue+Private.h"
#import "Database+Private.h"
#import "ResultSet.h"
#import "Log.h"
#import "WorkQueue.h"

#import <stdatomic.h>
Expand Down Expand Up @@ -48,7 +49,6 @@ - (void)internalInit:(NSString *)queueLabel {
- (instancetype)initInMemoryWithOptions:(ZENDatabaseQueueOptions)options {
self = [super init];
if (self) {
NSLog(@"Database in memory");
_databaseURL = nil;
_options = options;

Expand All @@ -72,7 +72,6 @@ + (instancetype)databaseQueueInMemoryWithOptions:(ZENDatabaseQueueOptions)option
- (instancetype)initWithURL:(NSURL *)URL options:(ZENDatabaseQueueOptions)options {
self = [super init];
if (self) {
NSLog(@"Database at: %@", URL);
_databaseURL = URL;
_options = options;

Expand All @@ -98,7 +97,7 @@ - (void)shutdown:(ZENDatabaseBlock)updateBlock {
// A second shutdown can't run its block: the first closes the database, and
// the work queue would run the block on the caller's thread, not in turn
if (atomic_exchange(&_shutdownStarted, true)) {
NSLog(@"WARNING: Database queue %@ was already shut down, so its database is closed", self.workQueue.label);
os_log(ZENLogDatabase(), "Database queue %@ was already shut down, so this shutdown did nothing", self.workQueue.label);
return;
}

Expand All @@ -123,16 +122,18 @@ - (ZENDatabase *)database {
if (_database == nil) {
FMDatabase *fmdb;
if (self.databaseURL) {
NSLog(@"Opening database at %@", self.databaseURL);
os_log_info(ZENLogDatabase(), "Opening the database at %@", self.databaseURL.path);
fmdb = [FMDatabase databaseWithURL:self.databaseURL];
} else {
// No path makes a private in-memory database, which lasts until it closes
NSLog(@"Opening database in memory");
os_log_info(ZENLogDatabase(), "Opening a database in memory");
fmdb = [FMDatabase databaseWithPath:nil];
}

// CoreZen logs failed statements itself, along with the rest of its messages
fmdb.logsErrors = NO;
if (![fmdb open]) {
NSLog(@"ERROR: Failed to open database at %@", self.databaseURL);
os_log_error(ZENLogDatabase(), "Couldn't open the database at %@: %{public}@", self.databaseURL.path, fmdb.lastErrorMessage);
return nil;
}

Expand All @@ -156,8 +157,8 @@ - (void)configureDatabase:(ZENDatabase *)database {
NSString *mode = [rs next] ? [rs stringForColumnIndex:0] : nil;
[rs close];
if (![mode isEqualToString:@"wal"]) {
NSLog(@"WARNING: %@ couldn't switch to a write-ahead log, so it keeps its %@ journal",
self.databaseURL.lastPathComponent, mode ?: @"current");
os_log(ZENLogDatabase(), "%@ couldn't switch to a write-ahead log, so it keeps its %{public}@ journal",
self.databaseURL.lastPathComponent, mode ?: @"current");
}
}

Expand Down Expand Up @@ -194,8 +195,8 @@ - (NSError *)runTransaction:(ZENDatabaseBlock)updateBlock {
}

if (![database beginTransaction]) {
NSLog(@"ERROR: Couldn't begin a transaction, so each of its statements commits on its own: %@",
database.lastErrorMessage);
os_log_error(ZENLogDatabase(), "Couldn't begin a transaction, so each of its statements commits on its own: %{public}@",
database.lastErrorMessage);
}

updateBlock(database);
Expand All @@ -206,13 +207,18 @@ - (NSError *)runTransaction:(ZENDatabaseBlock)updateBlock {
}
NSError *rollbackError = database.rollbackError;
if (rollbackError) {
NSLog(@"Rolling back a transaction: %@", rollbackError.localizedDescription);
// A canceled transaction is rolled back on purpose; anything else is a failure
if (rollbackError.code == ZENDatabaseErrorTransactionCanceled) {
os_log_info(ZENLogDatabase(), "Rolling back a transaction: %{public}@", rollbackError.localizedDescription);
} else {
os_log_error(ZENLogDatabase(), "Rolling back a transaction: %{public}@", rollbackError.localizedDescription);
}
[database rollback];
return rollbackError;
}
if (![database commit]) {
NSString *description = [NSString stringWithFormat:@"Couldn't commit the transaction: %@", database.lastErrorMessage];
NSLog(@"ERROR: %@, so rolling it back", description);
os_log_error(ZENLogDatabase(), "%{public}@, so rolling it back", description);
// A failed commit can leave the transaction open, which would swallow the next one
if (database.inTransaction) {
[database rollback];
Expand Down
7 changes: 4 additions & 3 deletions CoreZen/Database/DatabaseSchema.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#import "Database+Private.h"
#import "ResultSet.h"
#import "ObjectIdentifier+Private.h"
#import "Log.h"

@interface ZENDatabaseSchema ()

Expand Down Expand Up @@ -49,7 +50,7 @@ - (void)initializeDatabase:(ZENDatabase *)database {
BOOL versionChanged = [self updateDatabaseSchema:database];
NSError *rollbackError = database.rollbackError;
if (rollbackError) {
NSLog(@"ERROR: Rolling back the schema update: %@", rollbackError.localizedDescription);
os_log_error(ZENLogDatabase(), "Rolling back the schema update: %{public}@", rollbackError.localizedDescription);
}
if (ownsTransaction) {
if (versionChanged && !rollbackError) {
Expand Down Expand Up @@ -98,7 +99,7 @@ - (BOOL)updateDatabaseSchema:(ZENDatabase *)database {

BOOL versionChanged = (schemaVersion != startingVersion);
if (versionChanged) {
NSLog(@"Schema updated from version %lu to %lu", startingVersion, schemaVersion);
os_log_info(ZENLogDatabase(), "Schema updated from version %lu to %lu", startingVersion, schemaVersion);
NSString *statement = [NSString stringWithFormat:@"PRAGMA user_version = %lu", schemaVersion];
[database executeUpdate:statement];
}
Expand All @@ -125,7 +126,7 @@ - (void)initializeIdentifiers:(ZENDatabase *)database {
[rs close];
}
if (largestTableName) {
NSLog(@"Largest identifier is %lli (in %@)", largestIdentifier, largestTableName);
os_log_info(ZENLogDatabase(), "Largest identifier is %lli (in %{public}@)", largestIdentifier, largestTableName);
ZENRaiseLargestObjectIdentifier(largestIdentifier);
}
}
Expand Down
2 changes: 1 addition & 1 deletion CoreZen/Database/ResultSet.m
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ - (BOOL)next {
NSError *error = nil;
BOOL hasRow = [self.fmResultSet nextWithError:&error];
if (!hasRow && error) {
[self.database statementFailed];
[self.database statementFailed:self.fmResultSet.query ?: @""];
}
return hasRow;
}
Expand Down
4 changes: 3 additions & 1 deletion CoreZen/Domain/DomainObject.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#import "DomainObject.h"
#import "DomainCommon.h"
#import "WorkQueue.h"
#import "Log.h"

@interface ZENDomainObject ()

Expand Down Expand Up @@ -61,7 +62,8 @@ + (void)asyncInit:(NSArray *)objects
}
dispatch_group_leave(group);
} else {
NSLog(@"WARNING: %@ called its performAsyncInit: completion more than once", domainObject);
os_log(ZENLogDomain(), "%{public}@ %lld called its performAsyncInit: completion more than once",
NSStringFromClass([domainObject class]), domainObject.identifier);
}
}];
});
Expand Down
10 changes: 6 additions & 4 deletions CoreZen/Domain/ObjectRepository.m
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#import "Database+Private.h"
#import "ResultSet.h"
#import "DomainCommon.h"
#import "Log.h"

#import <os/lock.h>

Expand Down Expand Up @@ -167,7 +168,8 @@ - (void)cacheAndStartAsyncInit:(NSArray *)objects {
if (error) {
// Drop the object, so fetches leave it out. A later fetch creates it
// again from the database and retries its init.
NSLog(@"Async init failed for %@: %@", object, error);
os_log(ZENLogDomain(), "Async init failed for %{public}@ %lld, so fetches leave it out: %@",
NSStringFromClass([object class]), object.identifier, error.localizedDescription);
if ([self.cache cachedObject:object.identifier] == object) {
[self.cache removeObject:object.identifier];
}
Expand Down Expand Up @@ -331,7 +333,7 @@ - (void)write:(BOOL (^)(ZENDatabase *database))write
error = ZENDatabaseErrorMake(ZENDatabaseErrorWriteFailed, @"The table couldn't write the object");
}
if (error) {
NSLog(@"ERROR: A write to %@ wasn't saved: %@", [[self.table class] tableName], error.localizedDescription);
os_log_error(ZENLogDomain(), "A write to %{public}@ wasn't saved: %{public}@", [[self.table class] tableName], error.localizedDescription);
} else {
// Still on the database queue, so the cache keeps step with the
// database for the next block that runs there
Expand Down Expand Up @@ -463,7 +465,7 @@ + (void)performWrites:(void(NS_NOESCAPE ^)(ZENRepositoryWriteBatch *batch))build
description = [NSString stringWithFormat:@"A write batch has more than one write for object %lld in %@", write.identifier, tableName];
}
if (description) {
NSLog(@"ERROR: %@", description);
os_log_error(ZENLogDomain(), "%{public}@", description);
ZENCallAsyncCompletionBlockOnThreadPool(completion, ZENDatabaseErrorMake(ZENDatabaseErrorWriteFailed, description));
return;
}
Expand Down Expand Up @@ -491,7 +493,7 @@ + (void)performWrites:(void(NS_NOESCAPE ^)(ZENRepositoryWriteBatch *batch))build
error = writeError;
}
if (error) {
NSLog(@"ERROR: A write batch wasn't saved: %@", error.localizedDescription);
os_log_error(ZENLogDomain(), "A write batch wasn't saved: %{public}@", error.localizedDescription);
ZENCallAsyncCompletionBlockOnThreadPool(completion, error);
return;
}
Expand Down
Loading