-
Notifications
You must be signed in to change notification settings - Fork 127
/
MFLogViewerController.m
259 lines (225 loc) · 9.93 KB
/
MFLogViewerController.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
//
// MFLogViewerController.m
// MacFusion2
//
// Created by Michael Gorbach on 3/24/08.
// Copyright 2008 Michael Gorbach. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#import "MFLogViewerController.h"
#import "MFLogReader.h"
#import "MFClient.h"
#import "MFClientFS.h"
#import "MFLogging.h"
#import "MFPreferences.h"
#define kMessageSearchCategoryTag 0
#define kSenderSearchCategoryTag 1
#define kSubsystemSearchCategoryTag 2
#define kUUIDSearchCategoryTag 3
#define kAllSearchCategoryTag 4
static NSString *kSearchToolbarItemIdentifier = @"searchItem";
static NSString *kFSFilterToolbarItemIdentifier = @"fsFilterItem";
static NSString *kAutoscrollToolbarItemIdentifier = @"autoscroll";
@interface MFLogViewerController(PrivateAPI)
@property(readwrite, retain) NSPredicate *filterPredicate;
@property(readwrite, retain) NSPredicate *searchPredicate;
@end
@implementation MFLogViewerController
- (id)initWithWindowNibName:(NSString *)name {
if (self = [super initWithWindowNibName:name]) {
_logReader = [MFLogReader sharedReader];
[_logReader start];
self.filterPredicate = nil;
self.searchPredicate = nil;
}
return self;
}
+ (NSSet *)keyPathsForValuesAffectingFullLogPredicate {
return [NSSet setWithObjects:@"filterPredicate", @"searchPredicate", nil];
}
- (void)windowDidLoad {
[logTableView setIntercellSpacing:NSMakeSize(0, 0)];
[logTableView bind:@"logMessages" toObject:logArrayController withKeyPath:@"arrangedObjects" options:nil];
[logArrayController addObserver:self forKeyPath:@"arrangedObjects" options:(NSKeyValueObservingOptions)0 context:self];
[logArrayController bind:@"filterPredicate" toObject:self withKeyPath:@"fullLogPredicate" options:nil];
[logTableView scrollRowToVisible: [logTableView numberOfRows] - 1];
NSMenu *fsMenu = [[NSMenu alloc] initWithTitle:@"Filesystems"];
[fsMenu setDelegate:self];
filesystemFilterPopup = [[NSPopUpButton alloc] initWithFrame:NSMakeRect(0, 0, 200, 30)];
[filesystemFilterPopup setMenu:fsMenu];
[self menuNeedsUpdate:fsMenu];
autoScrollButton = [[NSButton alloc] initWithFrame:NSMakeRect(0, 0, 200, 30)];
[[autoScrollButton cell] setButtonType:NSSwitchButton];
[autoScrollButton setTitle:@"Auto-scroll"];
[autoScrollButton setAction:@selector(setAutoScroll:)];
[autoScrollButton setState:[[MFPreferences sharedPreferences] getBoolForPreference:kMFPrefsAutoScrollLog]];
logSearchField = [[NSSearchField alloc] initWithFrame: NSMakeRect(0, 0, 200, 30)];
[logSearchField setAction:@selector(searchFieldUpdated:)];
[logSearchField setTarget:self];
[[logSearchField cell] setPlaceholderString:@"Search Logs"];
NSMenu *searchCategoryMenu = [[NSMenu alloc] initWithTitle:@"Search Categories"];
[searchCategoryMenu addItemWithTitle:@"All" action:@selector(searchCategoryChanged:) keyEquivalent:@""];
[[searchCategoryMenu itemAtIndex:0] setTag:kAllSearchCategoryTag];
[searchCategoryMenu addItemWithTitle:@"Sender" action:@selector(searchCategoryChanged:) keyEquivalent:@""];
[[searchCategoryMenu itemAtIndex:1] setTag:kSenderSearchCategoryTag];
[searchCategoryMenu addItemWithTitle: @"Message" action:@selector(searchCategoryChanged:) keyEquivalent:@""];
[[searchCategoryMenu itemAtIndex:2] setTag:kMessageSearchCategoryTag];
[searchCategoryMenu addItemWithTitle: @"Subsystem" action:@selector(searchCategoryChanged:) keyEquivalent:@""];
[[searchCategoryMenu itemAtIndex:3] setTag:kSubsystemSearchCategoryTag];
[searchCategoryMenu addItemWithTitle: @"UUID" action:@selector(searchCategoryChanged:) keyEquivalent:@""];
[[searchCategoryMenu itemAtIndex:4] setTag:kUUIDSearchCategoryTag];
[[logSearchField cell] setSearchMenuTemplate:searchCategoryMenu];
[self searchCategoryChanged:[searchCategoryMenu itemWithTag: kAllSearchCategoryTag]];
NSToolbar *toolbar = [[NSToolbar alloc] initWithIdentifier:@"Test"];
[toolbar setAllowsUserCustomization:NO];
[toolbar setAutosavesConfiguration:NO];
[toolbar setDisplayMode:NSToolbarDisplayModeIconAndLabel];
[toolbar setDelegate:self];
[[self window] setToolbar:toolbar];
}
# pragma mark Updating
- (void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if (context == self) {
[logTableView reloadData];
if ([[MFPreferences sharedPreferences] getBoolForPreference: kMFPrefsAutoScrollLog]) {
[logTableView scrollRowToVisible:[logTableView numberOfRows] - 1];
}
} else {
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
}
- (void)setAutoScroll:(id)sender {
[[MFPreferences sharedPreferences] setBool:[sender state] forPreference:kMFPrefsAutoScrollLog];
}
# pragma mark Menus
- (void)menuNeedsUpdate:(NSMenu *)menu {
// NSLog(@"Menu needs update");
for (id item in [menu itemArray]) {
[menu removeItem: item];
}
[menu setShowsStateColumn:NO];
[menu addItemWithTitle:@"All" action:@selector(fsSelected:) keyEquivalent:@""];
for(MFClientFS *fs in [[MFClient sharedClient] filesystems]) {
NSMenuItem *menuItem = [[NSMenuItem alloc] initWithTitle: fs.name action: @selector(fsSelected:) keyEquivalent:@""];
[menuItem setRepresentedObject:fs];
[menu addItem:menuItem];
}
}
- (void)fsSelected:(id)sender {
if ([[sender representedObject] isKindOfClass: [MFClientFS class]]) {
// MFLogS(self, @"Setting filter");
self.filterPredicate = [NSPredicate predicateWithFormat: @"self.UUID == %@",[[sender representedObject] uuid]];
} else {
self.filterPredicate = nil;
}
}
# pragma mark Search & Filter
- (NSPredicate *)fullLogPredicate {
if (_filterPredicate && _searchPredicate) {
return [NSCompoundPredicate andPredicateWithSubpredicates:[NSArray arrayWithObjects:_filterPredicate, _searchPredicate, nil]];
}
if (_filterPredicate) {
return _filterPredicate;
}
if (_searchPredicate) {
return _searchPredicate;
}
return nil;
}
- (void)searchFieldUpdated:(id)sender {
NSString *text = [logSearchField stringValue];
if (!text || [text length] == 0) {
self.searchPredicate = nil;
return;
}
NSPredicate *messagePredicate = [NSPredicate predicateWithFormat: @"self.%@ CONTAINS[cd] %@", kMFLogKeyMessage, text];;
NSPredicate *senderPredicate = [NSPredicate predicateWithFormat: @"self.%@ CONTAINS[cd] %@", kMFLogKeySender, text];
NSPredicate *subsystemPredicate = [NSPredicate predicateWithFormat: @"self.%@ CONTAINS[cd] %@", kMFLogKeySubsystem, text];
NSPredicate *uuidPredicate = [NSPredicate predicateWithFormat:@"self.%@ CONTAINS[cd] %@", kMFLogKeyUUID, text];
switch (_searchCategory) {
case kAllSearchCategoryTag:
self.searchPredicate = [NSCompoundPredicate orPredicateWithSubpredicates:
[NSArray arrayWithObjects: messagePredicate, senderPredicate, subsystemPredicate, uuidPredicate, nil]];
break;
case kMessageSearchCategoryTag:
self.searchPredicate = messagePredicate;
break;
case kSubsystemSearchCategoryTag:
self.searchPredicate = subsystemPredicate;
break;
case kSenderSearchCategoryTag:
self.searchPredicate = senderPredicate;
break;
case kUUIDSearchCategoryTag:
self.searchPredicate = uuidPredicate;
break;
default:
break;
}
}
- (void)searchCategoryChanged:(id)sender {
[[logSearchField cell] setPlaceholderString: [NSString stringWithFormat:@"%@", [sender title]]];
_searchCategory = [sender tag];
[self searchFieldUpdated: logSearchField];
for (NSMenuItem *m in [[sender menu] itemArray]) {
[m setState: NO];
}
[sender setState: YES];
}
- (IBAction)filterForFilesystem:(MFClientFS *)fs {
for(NSMenuItem *item in [filesystemFilterPopup itemArray]) {
if ([item representedObject] == fs) {
[filesystemFilterPopup selectItem:item];
[self fsSelected:item];
}
}
}
# pragma mark Toolbars
- (NSArray *)toolbarAllowedItemIdentifiers: (NSToolbar *) toolbar {
return [NSArray arrayWithObjects:
kSearchToolbarItemIdentifier,
kAutoscrollToolbarItemIdentifier,
kFSFilterToolbarItemIdentifier,
NSToolbarFlexibleSpaceItemIdentifier,
NSToolbarSpaceItemIdentifier,
NSToolbarSeparatorItemIdentifier,
nil];
}
- (NSArray *)toolbarDefaultItemIdentifiers:(NSToolbar *)toolbar {
return [NSArray arrayWithObjects: kFSFilterToolbarItemIdentifier,kAutoscrollToolbarItemIdentifier, NSToolbarFlexibleSpaceItemIdentifier, kSearchToolbarItemIdentifier,nil];
}
- (NSToolbarItem *)toolbar:(NSToolbar *)toolbar itemForItemIdentifier:(NSString *)itemIdentifier willBeInsertedIntoToolbar:(BOOL)flag {
NSToolbarItem *toolbarItem = nil;
if ([itemIdentifier isEqualTo:kFSFilterToolbarItemIdentifier]) {
toolbarItem = [[NSToolbarItem alloc] initWithItemIdentifier:kFSFilterToolbarItemIdentifier];
[toolbarItem setLabel:@"Filesystem"];
[toolbarItem setPaletteLabel:[toolbarItem label]];
[toolbarItem setView:filesystemFilterPopup];
} else if ([itemIdentifier isEqualTo:kAutoscrollToolbarItemIdentifier]) {
toolbarItem = [[NSToolbarItem alloc] initWithItemIdentifier: kAutoscrollToolbarItemIdentifier];
[toolbarItem setLabel:@""];
[toolbarItem setPaletteLabel:[toolbarItem label]];
[toolbarItem setView:autoScrollButton];
[toolbarItem setMinSize:NSMakeSize(100, 20)];
} else if ([itemIdentifier isEqualTo: kSearchToolbarItemIdentifier]) {
toolbarItem = [[NSToolbarItem alloc] initWithItemIdentifier:kSearchToolbarItemIdentifier];
[toolbarItem setLabel:@"Search"];
[toolbarItem setPaletteLabel:[toolbarItem label]];
[toolbarItem setView:logSearchField];
[toolbarItem setMinSize:NSMakeSize(75, 20)];
[toolbarItem setMaxSize:NSMakeSize(150, 20)];
}
return toolbarItem;
}
@synthesize filterPredicate=_filterPredicate, searchPredicate=_searchPredicate;
@end