This repository has been archived by the owner on Jan 3, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 54
/
LogController.m
117 lines (99 loc) · 3.95 KB
/
LogController.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
//
// LogController.m
// Get_iPlayer GUI
//
// Created by Thomas Willson on 7/9/14.
//
//
#import "LogController.h"
@implementation LogController
- (id)init
{
//Initialize Log
NSString *version = [NSString stringWithFormat:@"%@", [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"]];
NSLog(@"Get iPlayer Automator %@ Initialized.", version);
log_value = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"Get iPlayer Automator %@ Initialized.", version]];
[self addToLog:@"" :nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(addToLogNotification:) name:@"AddToLog" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(postLog:) name:@"NeedLog" object:nil];
NSString *filePath = [@"~/Library/Application Support/Get iPlayer Automator/log.txt" stringByExpandingTildeInPath];
[[NSFileManager defaultManager] createFileAtPath:filePath
contents:nil
attributes:nil];
fh = [NSFileHandle fileHandleForWritingAtPath:filePath];
[fh seekToEndOfFile];
return self;
}
- (void)showLog:(id)sender
{
[window makeKeyAndOrderFront:self];
//Make sure the log scrolled to the bottom. It might not have if the Log window was not open.
NSAttributedString *temp_log = [[NSAttributedString alloc] initWithAttributedString:[self valueForKey:@"log_value"]];
[log scrollRangeToVisible:NSMakeRange([temp_log length], [temp_log length])];
}
- (void)postLog:(NSNotification *)note
{
NSString *tempLog = [log string];
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc postNotification:[NSNotification notificationWithName:@"Log" object:tempLog]];
}
-(void)addToLog:(NSString *)string
{
[self addToLog:string :nil];
}
-(void)addToLog:(NSString *)string :(id)sender {
//Get Current Log
NSMutableAttributedString *current_log = [[NSMutableAttributedString alloc] initWithAttributedString:log_value];
//Define Return Character for Easy Use
NSAttributedString *return_character = [[NSAttributedString alloc] initWithString:@"\r"];
//Initialize Sender Prefix
NSAttributedString *from_string;
if (sender != nil)
{
from_string = [[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@: ", [sender description]]];
}
else
{
from_string = [[NSAttributedString alloc] initWithString:@""];
}
//Convert String to Attributed String
NSAttributedString *converted_string = [[NSAttributedString alloc] initWithString:string];
//Append the new items to the log.
[current_log appendAttributedString:return_character];
[current_log appendAttributedString:from_string];
[current_log appendAttributedString:converted_string];
//Make the Text White.
[current_log addAttribute:NSForegroundColorAttributeName
value:[NSColor whiteColor]
range:NSMakeRange(0, [current_log length])];
//Update the log.
[self setValue:current_log forKey:@"log_value"];
//Scroll log to bottom only if it is visible.
if ([window isVisible]) {
[log scrollRangeToVisible:NSMakeRange([current_log length], [current_log length])];
}
//Write log out to file.
[fh writeData:[[return_character string] dataUsingEncoding:NSUTF8StringEncoding]];
[fh writeData:[[from_string string] dataUsingEncoding:NSUTF8StringEncoding]];
[fh writeData:[[converted_string string] dataUsingEncoding:NSUTF8StringEncoding]];
}
- (void)addToLogNotification:(NSNotification *)note
{
NSString *logMessage = [note userInfo][@"message"];
[self addToLog:logMessage :[note object]];
}
- (IBAction)copyLog:(id)sender
{
NSString *unattributedLog = [log string];
NSPasteboard *pb = [NSPasteboard generalPasteboard];
NSArray *types = @[NSStringPboardType];
[pb declareTypes:types owner:self];
[pb setString:unattributedLog forType:NSStringPboardType];
}
- (void)dealloc
{
[fh closeFile];
}
@synthesize window;
@synthesize log_value;
@end