Reworked the logger a bit.

This commit is contained in:
Damiano Galassi committed 2014-10-26 14:56:42 +01:00
1 parent 7d1cc39ebf
commit c868d74636
11 files changed
+167 -59

No files matched your search

+2 -2
View File
@@ -9,7 +9,7 @@
#import <Cocoa/Cocoa.h>
@class SBPrefsController;
@class SBDebugLogController;
@class SBLogWindowController;
@interface SBDocumentController : NSDocumentController
@@ -17,7 +17,7 @@
@interface SBAppDelegate : NSObject {
SBPrefsController *prefController;
SBDebugLogController *debugLogController;
SBLogWindowController *debugLogController;
SBDocumentController *documentController;
}
+6 -5
View File
@@ -11,7 +11,8 @@
#import "SBPresetManager.h"
#import "SBQueueController.h"
#import "SBPrefsController.h"
#import "SBDebugLogController.h"
#import "SBLogWindowController.h"
#import "SBLogger.h"
#import <MP42Foundation/MP42File.h>
@@ -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];
-19
View File
@@ -1,19 +0,0 @@
//
// SBDebugLogController.h
// Subler
//
// Created by Damiano Galassi on 24/10/14.
//
//
#import <Cocoa/Cocoa.h>
@interface SBDebugLogController : NSWindowController {
NSTextView *_logView;
NSURL *_fileURL;
}
- (instancetype)initWithLogFile:(NSURL *)fileURL;
- (void)log:(NSString *)string;
@end
+21
View File
@@ -0,0 +1,21 @@
//
// SBDebugLogController.h
// Subler
//
// Created by Damiano Galassi on 24/10/14.
//
//
#import <Cocoa/Cocoa.h>
#import <MP42Foundation/MP42Logging.h>
@class SBLogger;
@interface SBLogWindowController : NSWindowController <MP42Logging> {
NSTextView *_logView;
SBLogger *_logger;
}
- (instancetype)initWithLogger:(SBLogger *)logger;
@end
@@ -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
+24
View File
@@ -0,0 +1,24 @@
//
// SBLogger.m
// Subler
//
// Created by Damiano Galassi on 26/10/14.
//
//
#import <Foundation/Foundation.h>
#import <MP42Foundation/MP42Logging.h>
@interface SBLogger : NSObject <MP42Logging> {
NSURL *_fileURL;
id <MP42Logging> _delegate;
}
@property (assign, readwrite) id <MP42Logging> delegate;
- (instancetype)initWithLogFile:(NSURL *)fileURL;
- (void)writeToLog:(NSString *)string;
- (void)clearLog;
@end
+71
View File
@@ -0,0 +1,71 @@
//
// SBLogger.m
// Subler
//
// Created by Damiano Galassi on 26/10/14.
//
//
#import <Foundation/Foundation.h>
#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