Rewrite some classes in Swift.
This commit is contained in:
15 files changed
+175
-300
No files matched your search
@@ -0,0 +1,49 @@
|
||||
//
|
||||
// ActivityWindowController.swift
|
||||
// Subler
|
||||
//
|
||||
// Created by Damiano Galassi on 20/10/2017.
|
||||
//
|
||||
|
||||
import Cocoa
|
||||
|
||||
class ActivityWindowController : NSWindowController, MP42Logging {
|
||||
|
||||
@IBOutlet var logView: NSTextView!
|
||||
let logger: Logger
|
||||
|
||||
@objc init(logger: Logger) {
|
||||
self.logger = logger
|
||||
super.init(window: nil)
|
||||
self.logger.delegate = self
|
||||
_ = self.window;
|
||||
}
|
||||
|
||||
required init?(coder: NSCoder) {
|
||||
fatalError("init(coder:) has not been implemented")
|
||||
}
|
||||
|
||||
override public var windowNibName: NSNib.Name? {
|
||||
return NSNib.Name(rawValue: "SBActivityWindow")
|
||||
}
|
||||
|
||||
override func windowDidLoad() {
|
||||
super.windowDidLoad()
|
||||
}
|
||||
|
||||
func write(toLog string: String) {
|
||||
DispatchQueue.main.async {
|
||||
let attrString = NSAttributedString(string: string)
|
||||
self.logView.textStorage?.append(attrString)
|
||||
}
|
||||
}
|
||||
|
||||
@IBAction func clearLog(_ sender: Any) {
|
||||
logger.clear()
|
||||
if let textStorage = logView.textStorage {
|
||||
textStorage.deleteCharacters(in: NSMakeRange(0, textStorage.length))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
//
|
||||
// ButtonToolbarItem.swift
|
||||
// Subler
|
||||
//
|
||||
// Created by Damiano Galassi on 20/10/2017.
|
||||
//
|
||||
|
||||
import Cocoa
|
||||
|
||||
class ButtonToolbarItem : NSToolbarItem {
|
||||
|
||||
override func validate() {
|
||||
if let target = target {
|
||||
isEnabled = target.validateToolbarItem(self)
|
||||
}
|
||||
}
|
||||
|
||||
override var menuFormRepresentation: NSMenuItem? {
|
||||
get {
|
||||
let menuItem = NSMenuItem(title: label, action: action, keyEquivalent: "")
|
||||
menuItem.target = target
|
||||
menuItem.isEnabled = target?.validateToolbarItem(self) ?? false
|
||||
return menuItem
|
||||
}
|
||||
set (menuItem) {
|
||||
super.menuFormRepresentation = menuItem
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
//
|
||||
// Logger.swift
|
||||
// Subler
|
||||
//
|
||||
// Created by Damiano Galassi on 20/10/2017.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
class Logger : NSObject, MP42Logging {
|
||||
|
||||
var delegate: MP42Logging?
|
||||
|
||||
let fileURL: URL
|
||||
let queue: DispatchQueue
|
||||
|
||||
@objc init(fileURL: URL) {
|
||||
self.fileURL = fileURL
|
||||
self.queue = DispatchQueue(label: "org.subler.LogQueue")
|
||||
}
|
||||
|
||||
private func currentTime() -> String {
|
||||
var currentTime = time(nil)
|
||||
if let localTime = localtime(¤tTime) {
|
||||
let p = localTime.pointee
|
||||
return "\(p.tm_hour):\(p.tm_min):\(p.tm_sec)"
|
||||
} else {
|
||||
return ""
|
||||
}
|
||||
}
|
||||
|
||||
func write(toLog string: String) {
|
||||
let output = "\(currentTime()) \(string)\n"
|
||||
|
||||
_ = fileURL.withUnsafeFileSystemRepresentation {
|
||||
if let file = fopen($0, "a") {
|
||||
fputs(output, file)
|
||||
fclose(file)
|
||||
}
|
||||
}
|
||||
|
||||
if let delegate = delegate {
|
||||
delegate.write(toLog: output)
|
||||
}
|
||||
}
|
||||
|
||||
func writeError(toLog error: Error) {
|
||||
write(toLog: error.localizedDescription)
|
||||
}
|
||||
|
||||
@objc func clear() {
|
||||
try? FileManager.default.removeItem(at: fileURL)
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -9,8 +9,6 @@
|
||||
#import "SBAppDelegate.h"
|
||||
#import "SBDocument.h"
|
||||
#import "SBQueueController.h"
|
||||
#import "SBLogWindowController.h"
|
||||
#import "SBLogger.h"
|
||||
|
||||
#import "Subler-Swift.h"
|
||||
|
||||
@@ -22,7 +20,7 @@
|
||||
@interface SBAppDelegate ()
|
||||
{
|
||||
SBPrefsWindowController *prefController;
|
||||
SBLogWindowController *debugLogController;
|
||||
ActivityWindowController *debugLogController;
|
||||
SBDocumentController *documentController;
|
||||
}
|
||||
@end
|
||||
@@ -45,10 +43,10 @@
|
||||
|
||||
NSString *path = [appSupportPath stringByAppendingPathComponent:@"debugLog.txt"];
|
||||
|
||||
SBLogger *logger = [[SBLogger alloc] initWithLogFile:[NSURL fileURLWithPath:path]];
|
||||
[logger clearLog];
|
||||
Logger *logger = [[Logger alloc] initWithFileURL:[NSURL fileURLWithPath:path]];
|
||||
[logger clear];
|
||||
|
||||
debugLogController = [[SBLogWindowController alloc] initWithLogger:logger];
|
||||
debugLogController = [[ActivityWindowController alloc] initWithLogger:logger];
|
||||
MP42File.globalLogger = logger;
|
||||
|
||||
if ([NSUserDefaults.standardUserDefaults boolForKey:@"SBAdditionalDebugInfo"]) {
|
||||
|
||||
@@ -1,22 +0,0 @@
|
||||
//
|
||||
// SBDebugLogController.h
|
||||
// Subler
|
||||
//
|
||||
// Created by Damiano Galassi on 24/10/14.
|
||||
//
|
||||
//
|
||||
|
||||
#import <Cocoa/Cocoa.h>
|
||||
#import <MP42Foundation/MP42Logging.h>
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
@class SBLogger;
|
||||
|
||||
@interface SBLogWindowController : NSWindowController <MP42Logging>
|
||||
|
||||
- (instancetype)initWithLogger:(SBLogger *)logger;
|
||||
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
||||
@@ -1,54 +0,0 @@
|
||||
//
|
||||
// SBDebugLogController.m
|
||||
// Subler
|
||||
//
|
||||
// Created by Damiano Galassi on 24/10/14.
|
||||
//
|
||||
//
|
||||
|
||||
#import "SBLogWindowController.h"
|
||||
#import "SBLogger.h"
|
||||
|
||||
@interface SBLogWindowController ()
|
||||
|
||||
@property (nonatomic, unsafe_unretained) IBOutlet NSTextView *logView;
|
||||
@property (nonatomic, readonly) SBLogger *logger;
|
||||
|
||||
@end
|
||||
|
||||
@implementation SBLogWindowController
|
||||
|
||||
- (instancetype)init {
|
||||
if ((self = [super initWithWindowNibName:@"SBLogWindow"])) {
|
||||
(void)self.window;
|
||||
}
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
- (instancetype)initWithLogger:(SBLogger *)logger {
|
||||
NSAssert(!_logger, @"Logger is nil");
|
||||
self = [self init];
|
||||
|
||||
if (self) {
|
||||
_logger = logger;
|
||||
_logger.delegate = self;
|
||||
}
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
- (void)writeToLog:(NSString *)string {
|
||||
dispatch_async(dispatch_get_main_queue(), ^{
|
||||
NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString:string];
|
||||
[self.logView.textStorage appendAttributedString:attributedString];
|
||||
});
|
||||
}
|
||||
|
||||
- (IBAction)clearLog:(id)sender {
|
||||
[self.logView.textStorage deleteCharactersInRange:NSMakeRange(0, (self.logView).textStorage.length)];
|
||||
[self.logger clearLog];
|
||||
}
|
||||
|
||||
|
||||
@end
|
||||
@@ -1,23 +0,0 @@
|
||||
//
|
||||
// SBLogger.m
|
||||
// Subler
|
||||
//
|
||||
// Created by Damiano Galassi on 26/10/14.
|
||||
//
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
#import <MP42Foundation/MP42Logging.h>
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
@interface SBLogger : NSObject <MP42Logging>
|
||||
|
||||
@property (atomic, unsafe_unretained, readwrite, nullable) id <MP42Logging> delegate;
|
||||
|
||||
- (instancetype)initWithLogFile:(NSURL *)fileURL;
|
||||
- (void)clearLog;
|
||||
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
||||
@@ -1,79 +0,0 @@
|
||||
//
|
||||
// SBLogger.m
|
||||
// Subler
|
||||
//
|
||||
// Created by Damiano Galassi on 26/10/14.
|
||||
//
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
#import "SBLogger.h"
|
||||
|
||||
@interface SBLogger ()
|
||||
|
||||
@property (nonatomic, readonly) NSURL *fileURL;
|
||||
@property (nonatomic, readonly) dispatch_queue_t queue;
|
||||
|
||||
@end
|
||||
|
||||
@implementation SBLogger
|
||||
|
||||
- (instancetype)initWithLogFile:(NSURL *)fileURL {
|
||||
self = [self init];
|
||||
|
||||
if (self) {
|
||||
_fileURL = [fileURL copy];
|
||||
_queue = dispatch_queue_create("org.subler.LogQueue", DISPATCH_QUEUE_SERIAL);
|
||||
}
|
||||
|
||||
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 @(time);
|
||||
}
|
||||
|
||||
- (void)writeToLog:(NSString *)string {
|
||||
dispatch_sync(self.queue, ^{
|
||||
if (self.fileURL) {
|
||||
FILE *f = fopen(self.fileURL.fileSystemRepresentation, "a");
|
||||
if (f) {
|
||||
fprintf(f, "%s %s", [self currentTime].UTF8String, string.UTF8String);
|
||||
fprintf(f, "\n");
|
||||
fclose(f);
|
||||
}
|
||||
}
|
||||
|
||||
if (self.delegate) {
|
||||
[self.delegate writeToLog:[NSString stringWithFormat:@"%@ %@", [self currentTime], string]];
|
||||
[self.delegate writeToLog:@"\n"];
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
- (void)writeErrorToLog:(NSError *)error {
|
||||
if (error.localizedDescription) {
|
||||
[self writeToLog:error.localizedDescription];
|
||||
}
|
||||
if (error.localizedRecoverySuggestion) {
|
||||
[self writeToLog:error.localizedRecoverySuggestion];
|
||||
}
|
||||
}
|
||||
|
||||
- (void)clearLog {
|
||||
if (self.fileURL) {
|
||||
[[NSFileManager defaultManager] removeItemAtURL:self.fileURL error:nil];
|
||||
}
|
||||
}
|
||||
|
||||
- (void)dealloc {
|
||||
_delegate = nil;
|
||||
}
|
||||
|
||||
@end
|
||||
Reference in new issue
Block a user