diff --git a/Classes/SBAppDelegate.h b/Classes/SBAppDelegate.h index 42b89e1..6b3ab05 100755 --- a/Classes/SBAppDelegate.h +++ b/Classes/SBAppDelegate.h @@ -9,7 +9,7 @@ #import @class SBPrefsController; -@class SBDebugLogController; +@class SBLogWindowController; @interface SBDocumentController : NSDocumentController @@ -17,7 +17,7 @@ @interface SBAppDelegate : NSObject { SBPrefsController *prefController; - SBDebugLogController *debugLogController; + SBLogWindowController *debugLogController; SBDocumentController *documentController; } diff --git a/Classes/SBAppDelegate.m b/Classes/SBAppDelegate.m index 0702614..7ffa938 100755 --- a/Classes/SBAppDelegate.m +++ b/Classes/SBAppDelegate.m @@ -11,7 +11,8 @@ #import "SBPresetManager.h" #import "SBQueueController.h" #import "SBPrefsController.h" -#import "SBDebugLogController.h" +#import "SBLogWindowController.h" +#import "SBLogger.h" #import @@ -29,11 +30,11 @@ NSUserDomainMask, YES) firstObject] stringByAppendingPathComponent:@"Subler/debugLog.txt"]; - debugLogController = [[SBDebugLogController alloc] initWithLogFile:[NSURL fileURLWithPath:path]]; + SBLogger *logger = [[SBLogger alloc] initWithLogFile:[NSURL fileURLWithPath:path]]; + [logger clearLog]; - [MP42File redirectLogUsingBlock:^(NSString *text) { - [debugLogController log:text]; - }]; + debugLogController = [[SBLogWindowController alloc] initWithLogger:logger]; + [MP42File setGlobalLogger:logger]; if ([[NSUserDefaults standardUserDefaults] valueForKey:@"SBShowQueueWindow"]) [[SBQueueController sharedManager] showWindow:self]; diff --git a/Classes/SBDebugLogController.h b/Classes/SBDebugLogController.h deleted file mode 100644 index 5f844ff..0000000 --- a/Classes/SBDebugLogController.h +++ /dev/null @@ -1,19 +0,0 @@ -// -// SBDebugLogController.h -// Subler -// -// Created by Damiano Galassi on 24/10/14. -// -// - -#import - -@interface SBDebugLogController : NSWindowController { - NSTextView *_logView; - NSURL *_fileURL; -} - -- (instancetype)initWithLogFile:(NSURL *)fileURL; -- (void)log:(NSString *)string; - -@end diff --git a/Classes/SBLogWindowController.h b/Classes/SBLogWindowController.h new file mode 100644 index 0000000..c486db4 --- /dev/null +++ b/Classes/SBLogWindowController.h @@ -0,0 +1,21 @@ +// +// SBDebugLogController.h +// Subler +// +// Created by Damiano Galassi on 24/10/14. +// +// + +#import +#import + +@class SBLogger; + +@interface SBLogWindowController : NSWindowController { + NSTextView *_logView; + SBLogger *_logger; +} + +- (instancetype)initWithLogger:(SBLogger *)logger; + +@end diff --git a/Classes/SBDebugLogController.m b/Classes/SBLogWindowController.m similarity index 55% rename from Classes/SBDebugLogController.m rename to Classes/SBLogWindowController.m index aa355ef..638e733 100644 --- a/Classes/SBDebugLogController.m +++ b/Classes/SBLogWindowController.m @@ -6,46 +6,42 @@ // // -#import "SBDebugLogController.h" +#import "SBLogWindowController.h" +#import "SBLogger.h" -@interface SBDebugLogController () +@interface SBLogWindowController () @property (assign) IBOutlet NSTextView *logView; -@property (readonly) NSURL *fileURL; +@property (readonly) SBLogger *logger; @end -@implementation SBDebugLogController +@implementation SBLogWindowController @synthesize logView = _logView; -@synthesize fileURL = _fileURL; +@synthesize logger = _logger; - (instancetype)init { - if ((self = [super initWithWindowNibName:@"SBDebugLogWindow"])) { + if ((self = [super initWithWindowNibName:@"SBLogWindow"])) { [self window]; } return self; } -- (instancetype)initWithLogFile:(NSURL *)fileURL { +- (instancetype)initWithLogger:(SBLogger *)logger { + NSAssert(!_logger, @"Logger is nil"); self = [self init]; if (self) { - _fileURL = [fileURL copy]; - [[NSFileManager defaultManager] removeItemAtURL:_fileURL error:nil]; + _logger = [logger retain]; + _logger.delegate = self; } return self; } -- (void)log:(NSString *)string { - if (self.fileURL) { - FILE *f = fopen([self.fileURL fileSystemRepresentation], "a"); - fprintf(f, "%s", [string UTF8String]); - fclose(f); - } - +- (void)writeToLog:(NSString *)string { dispatch_async(dispatch_get_main_queue(), ^{ NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString:string]; [[self.logView textStorage] appendAttributedString:attributedString]; @@ -55,6 +51,15 @@ - (IBAction)clearLog:(id)sender { [[self.logView textStorage] deleteCharactersInRange:NSMakeRange(0, [[self.logView textStorage] length])]; + [self.logger clearLog]; +} + +- (void)dealloc +{ + [_logger release]; + _logger = nil; + + [super dealloc]; } @end diff --git a/Classes/SBLogger.h b/Classes/SBLogger.h new file mode 100644 index 0000000..ffccfaf --- /dev/null +++ b/Classes/SBLogger.h @@ -0,0 +1,24 @@ +// +// SBLogger.m +// Subler +// +// Created by Damiano Galassi on 26/10/14. +// +// + +#import +#import + +@interface SBLogger : NSObject { + NSURL *_fileURL; + id _delegate; +} + +@property (assign, readwrite) id delegate; + +- (instancetype)initWithLogFile:(NSURL *)fileURL; + +- (void)writeToLog:(NSString *)string; +- (void)clearLog; + +@end diff --git a/Classes/SBLogger.m b/Classes/SBLogger.m new file mode 100644 index 0000000..6406a4a --- /dev/null +++ b/Classes/SBLogger.m @@ -0,0 +1,71 @@ +// +// SBLogger.m +// Subler +// +// Created by Damiano Galassi on 26/10/14. +// +// + +#import +#import "SBLogger.h" + +@interface SBLogger () + +@property (readonly) NSURL *fileURL; + +@end + +@implementation SBLogger + +@synthesize fileURL = _fileURL; +@synthesize delegate = _delegate; + +- (instancetype)initWithLogFile:(NSURL *)fileURL { + self = [self init]; + + if (self) { + _fileURL = [fileURL copy]; + } + + return self; +} + +- (NSString *)currentTime { + time_t _now = time(NULL); + struct tm *now = localtime(&_now); + char time[512]; + + snprintf(time, sizeof(time), "[%02d:%02d:%02d]", now->tm_hour, now->tm_min, now->tm_sec); + + return [NSString stringWithUTF8String:time]; +} + +- (void)writeToLog:(NSString *)string { + if (self.fileURL) { + FILE *f = fopen([self.fileURL fileSystemRepresentation], "a"); + fprintf(f, "%s %s", [[self currentTime] UTF8String], [string UTF8String]); + fclose(f); + } + + if (self.delegate) { + [self.delegate writeToLog:[NSString stringWithFormat:@"%@ %@", [self currentTime], string]]; + } +} + +- (void)clearLog { + if (self.fileURL) { + [[NSFileManager defaultManager] removeItemAtURL:self.fileURL error:nil]; + } +} + +- (void)dealloc +{ + [_fileURL release]; + _fileURL = nil; + + _delegate = nil; + + [super dealloc]; +} + +@end diff --git a/English.lproj/Queue.xib b/English.lproj/Queue.xib index 41d485c..06f713c 100755 --- a/English.lproj/Queue.xib +++ b/English.lproj/Queue.xib @@ -35,7 +35,7 @@ - + diff --git a/English.lproj/QueueItem.xib b/English.lproj/QueueItem.xib index 68c4821..d3f749f 100644 --- a/English.lproj/QueueItem.xib +++ b/English.lproj/QueueItem.xib @@ -103,14 +103,14 @@ - + - + diff --git a/English.lproj/SBDebugLogWindow.xib b/English.lproj/SBLogWindow.xib similarity index 98% rename from English.lproj/SBDebugLogWindow.xib rename to English.lproj/SBLogWindow.xib index 7f104b4..801e528 100644 --- a/English.lproj/SBDebugLogWindow.xib +++ b/English.lproj/SBLogWindow.xib @@ -1,11 +1,10 @@ - - + diff --git a/Subler.xcodeproj/project.pbxproj b/Subler.xcodeproj/project.pbxproj index dfaa86a..67c4ef1 100755 --- a/Subler.xcodeproj/project.pbxproj +++ b/Subler.xcodeproj/project.pbxproj @@ -13,6 +13,7 @@ 8D15AC320486D014006FF6A4 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 2A37F4B0FDCFA73011CA2CEA /* main.m */; settings = {ATTRIBUTES = (); }; }; 8D15AC340486D014006FF6A4 /* Cocoa.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 1058C7A7FEA54F5311CA2CBB /* Cocoa.framework */; }; A90B023B10F3290A000416F3 /* Prefs.xib in Resources */ = {isa = PBXBuildFile; fileRef = A90B023910F3290A000416F3 /* Prefs.xib */; }; + A917286119FD288B00348DF8 /* SBLogger.m in Sources */ = {isa = PBXBuildFile; fileRef = A917286019FD288B00348DF8 /* SBLogger.m */; }; A91F622D0FB0A56F00B96B33 /* libicucore.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = A91F622C0FB0A56F00B96B33 /* libicucore.dylib */; }; A925AA8518378D8A00BE84D4 /* MP42Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = A925AA8218378D7900BE84D4 /* MP42Foundation.framework */; }; A925AA8818378DD000BE84D4 /* MP42Foundation.framework in CopyFiles */ = {isa = PBXBuildFile; fileRef = A925AA8218378D7900BE84D4 /* MP42Foundation.framework */; }; @@ -57,8 +58,8 @@ A9BC410818D9ABC000F8C92C /* QueueItem.xib in Resources */ = {isa = PBXBuildFile; fileRef = A9BC410618D9ABC000F8C92C /* QueueItem.xib */; }; A9C055370F6D395B00AF15B5 /* FileImport.xib in Resources */ = {isa = PBXBuildFile; fileRef = A9C055360F6D395B00AF15B5 /* FileImport.xib */; }; A9C4259318DB6D1F00E99DB8 /* stopTemplate.pdf in Resources */ = {isa = PBXBuildFile; fileRef = A9C4259218DB6D1F00E99DB8 /* stopTemplate.pdf */; }; - A9C5FA6E19FA22590051F3FF /* SBDebugLogController.m in Sources */ = {isa = PBXBuildFile; fileRef = A9C5FA6C19FA22590051F3FF /* SBDebugLogController.m */; }; - A9C5FA7419FA2FEC0051F3FF /* SBDebugLogWindow.xib in Resources */ = {isa = PBXBuildFile; fileRef = A9C5FA7219FA2FEC0051F3FF /* SBDebugLogWindow.xib */; }; + A9C5FA6E19FA22590051F3FF /* SBLogWindowController.m in Sources */ = {isa = PBXBuildFile; fileRef = A9C5FA6C19FA22590051F3FF /* SBLogWindowController.m */; }; + A9C5FA7419FA2FEC0051F3FF /* SBLogWindow.xib in Resources */ = {isa = PBXBuildFile; fileRef = A9C5FA7219FA2FEC0051F3FF /* SBLogWindow.xib */; }; A9C828F5195950C3004C4718 /* SBQueuePreferences.m in Sources */ = {isa = PBXBuildFile; fileRef = A9C828F4195950C3004C4718 /* SBQueuePreferences.m */; }; A9C9E34413FBC1EB00CA3B4E /* EncodeWorking.png in Resources */ = {isa = PBXBuildFile; fileRef = A9C9E34113FBC1EB00CA3B4E /* EncodeWorking.png */; }; A9D47B5817B239E200900BCA /* EncodeCanceled.png in Resources */ = {isa = PBXBuildFile; fileRef = A9D47B5417B239E200900BCA /* EncodeCanceled.png */; }; @@ -152,6 +153,8 @@ 8D15AC370486D014006FF6A4 /* Subler.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Subler.app; sourceTree = BUILT_PRODUCTS_DIR; }; 948058B5106E07990037A911 /* libz.dylib */ = {isa = PBXFileReference; lastKnownFileType = "compiled.mach-o.dylib"; name = libz.dylib; path = /usr/lib/libz.dylib; sourceTree = ""; }; A90B023A10F3290A000416F3 /* English */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = English; path = English.lproj/Prefs.xib; sourceTree = ""; }; + A917286019FD288B00348DF8 /* SBLogger.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = SBLogger.m; path = Classes/SBLogger.m; sourceTree = ""; }; + A917286419FD289F00348DF8 /* SBLogger.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = SBLogger.h; path = Classes/SBLogger.h; sourceTree = ""; }; A91F622C0FB0A56F00B96B33 /* libicucore.dylib */ = {isa = PBXFileReference; lastKnownFileType = "compiled.mach-o.dylib"; name = libicucore.dylib; path = /usr/lib/libicucore.dylib; sourceTree = ""; }; A925AA7B18378D7800BE84D4 /* MP42Foundation.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = MP42Foundation.xcodeproj; path = MP42Foundation/MP42Foundation.xcodeproj; sourceTree = ""; }; A936E03C130BE10200CA5527 /* MAAttachedWindow.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = MAAttachedWindow.h; path = Others/MAAttachedWindow.h; sourceTree = ""; }; @@ -219,9 +222,9 @@ A9C0603F168315C500FE3D98 /* libbz2.dylib */ = {isa = PBXFileReference; lastKnownFileType = "compiled.mach-o.dylib"; name = libbz2.dylib; path = usr/lib/libbz2.dylib; sourceTree = SDKROOT; }; A9C0957F0F7B6C8C00170CAF /* AudioToolbox.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = AudioToolbox.framework; path = /System/Library/Frameworks/AudioToolbox.framework; sourceTree = ""; }; A9C4259218DB6D1F00E99DB8 /* stopTemplate.pdf */ = {isa = PBXFileReference; lastKnownFileType = image.pdf; name = stopTemplate.pdf; path = images/stopTemplate.pdf; sourceTree = ""; }; - A9C5FA6B19FA22590051F3FF /* SBDebugLogController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = SBDebugLogController.h; path = Classes/SBDebugLogController.h; sourceTree = ""; }; - A9C5FA6C19FA22590051F3FF /* SBDebugLogController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = SBDebugLogController.m; path = Classes/SBDebugLogController.m; sourceTree = ""; }; - A9C5FA7319FA2FEC0051F3FF /* English */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = English; path = English.lproj/SBDebugLogWindow.xib; sourceTree = ""; }; + A9C5FA6B19FA22590051F3FF /* SBLogWindowController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = SBLogWindowController.h; path = Classes/SBLogWindowController.h; sourceTree = ""; }; + A9C5FA6C19FA22590051F3FF /* SBLogWindowController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = SBLogWindowController.m; path = Classes/SBLogWindowController.m; sourceTree = ""; }; + A9C5FA7319FA2FEC0051F3FF /* English */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = English; path = English.lproj/SBLogWindow.xib; sourceTree = ""; }; A9C8182C0F74D6E300019BFF /* QuickTime.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = QuickTime.framework; path = /System/Library/Frameworks/QuickTime.framework; sourceTree = ""; }; A9C828F3195950C3004C4718 /* SBQueuePreferences.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = SBQueuePreferences.h; path = Classes/SBQueuePreferences.h; sourceTree = ""; }; A9C828F4195950C3004C4718 /* SBQueuePreferences.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = SBQueuePreferences.m; path = Classes/SBQueuePreferences.m; sourceTree = ""; }; @@ -352,8 +355,10 @@ A9AB484817F0AEA2000AA72E /* SBAppDelegate.m */, CD71FF1A12F1C46900F0A98D /* MetadataImporters */, A9FD5D150F3C692400817716 /* PropertyView */, - A9C5FA6B19FA22590051F3FF /* SBDebugLogController.h */, - A9C5FA6C19FA22590051F3FF /* SBDebugLogController.m */, + A9C5FA6B19FA22590051F3FF /* SBLogWindowController.h */, + A9C5FA6C19FA22590051F3FF /* SBLogWindowController.m */, + A917286419FD289F00348DF8 /* SBLogger.h */, + A917286019FD288B00348DF8 /* SBLogger.m */, ); name = Classes; sourceTree = ""; @@ -440,7 +445,7 @@ A9EEA5F618D654B9008B54B4 /* QueueOptions.xib */, A9BC410618D9ABC000F8C92C /* QueueItem.xib */, A90B023910F3290A000416F3 /* Prefs.xib */, - A9C5FA7219FA2FEC0051F3FF /* SBDebugLogWindow.xib */, + A9C5FA7219FA2FEC0051F3FF /* SBLogWindow.xib */, ); name = NIBs; sourceTree = ""; @@ -646,7 +651,7 @@ 1DDD582D0DA1D0D100B32029 /* MainMenu.xib in Resources */, A9D47B5917B239E200900BCA /* EncodeCanceled@2x.png in Resources */, A984DB740F52DD4B001A20B2 /* dsa_pub.pem in Resources */, - A9C5FA7419FA2FEC0051F3FF /* SBDebugLogWindow.xib in Resources */, + A9C5FA7419FA2FEC0051F3FF /* SBLogWindow.xib in Resources */, A9C055370F6D395B00AF15B5 /* FileImport.xib in Resources */, A95C881D0F827BFB00F51A22 /* ToolbarRemoveTemplate.png in Resources */, A90B023B10F3290A000416F3 /* Prefs.xib in Resources */, @@ -713,7 +718,7 @@ A9B46ABF12B79D1C0040517A /* SBDocument.m in Sources */, CD71FEA312F1B5AF00F0A98D /* SBMetadataSearchController.m in Sources */, CD71FF1D12F1C4A900F0A98D /* TheTVDB.m in Sources */, - A9C5FA6E19FA22590051F3FF /* SBDebugLogController.m in Sources */, + A9C5FA6E19FA22590051F3FF /* SBLogWindowController.m in Sources */, A9D73C5218BF3F1100C6FFA6 /* SBQueue.m in Sources */, A9AB485717F0AEAC000AA72E /* SBSoundViewController.m in Sources */, A9C828F5195950C3004C4718 /* SBQueuePreferences.m in Sources */, @@ -724,6 +729,7 @@ A982D0C0140AB66400B92357 /* SBQueueItem.m in Sources */, A9AF6A7518D0496400FAAB08 /* SBQueueAction.m in Sources */, CDC06FCE1747494D003C1D0A /* iTunesStore.m in Sources */, + A917286119FD288B00348DF8 /* SBLogger.m in Sources */, CD03087417576A550093A0BE /* TheMovieDB3.m in Sources */, A9EEA5F218D65490008B54B4 /* SBOptionsViewController.m in Sources */, CD03087917576AE50093A0BE /* MetadataImporter.m in Sources */, @@ -847,12 +853,12 @@ name = FileImport.xib; sourceTree = ""; }; - A9C5FA7219FA2FEC0051F3FF /* SBDebugLogWindow.xib */ = { + A9C5FA7219FA2FEC0051F3FF /* SBLogWindow.xib */ = { isa = PBXVariantGroup; children = ( A9C5FA7319FA2FEC0051F3FF /* English */, ); - name = SBDebugLogWindow.xib; + name = SBLogWindow.xib; sourceTree = ""; }; A9EEA5F618D654B9008B54B4 /* QueueOptions.xib */ = {