This repository was archived by the owner on Feb 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathXGALogViewController.m
More file actions
251 lines (222 loc) · 8.47 KB
/
XGALogViewController.m
File metadata and controls
251 lines (222 loc) · 8.47 KB
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
/**
@file XGALogViewController.m
@package xcode-github-app
@brief The log window view controller.
@author Edward Smith
@date May 2018
@copyright Copyright © 2018 Branch. All rights reserved.
*/
#import "BNCThreads.h"
#import "XGALogViewController.h"
#import "XGASettings.h"
#import "XGAStatusPopover.h"
#import <XcodeGitHub/XcodeGitHub.h>
NSString*const XGALogUpdateNotification = @"XGALogUpdatedNotification";
#pragma mark XGALogRow
@interface XGALogRow : NSObject
@property (nonatomic, strong) NSDate*date;
@property (nonatomic, assign) BNCLogLevel logLevel;
@property (nonatomic, strong) NSImage*logLevelImage;
@property (nonatomic, strong) NSString*logMessage;
@end
@implementation XGALogRow
@end
#pragma mark - XGALogViewController
@interface XGALogViewController () <NSPopoverDelegate>
+ (NSMutableArray<XGALogRow*>*) logArray;
+ (NSImage*) imageForLogLevel:(BNCLogLevel)level;
@property (strong) NSDateFormatter*dateFormatter;
@property (strong) IBOutlet NSArrayController *arrayController;
@property (weak) IBOutlet NSTableView*tableView;
@property (strong) XGAStatusPopover*statusPopover;
@end
#pragma mark - Log Function
void XGALogFunction(NSDate*_Nonnull timestamp, BNCLogLevel level, NSString*_Nullable message) {
XGALogRow*row = [[XGALogRow alloc] init];
row.date = timestamp;
row.logLevel = level;
row.logLevelImage = [XGALogViewController imageForLogLevel:level];
if (YES) {
// Make the message pretty:
NSRange range = [message rangeOfString:@": "];
if (range.location != NSNotFound && range.location+2 < message.length)
message = [message substringFromIndex:range.location+2];
}
row.logMessage = message;
[[XGALogViewController logArray] addObject:row];
[[NSNotificationCenter defaultCenter]
postNotificationName:XGALogUpdateNotification
object:XGALogViewController.class];
}
#pragma mark - XGALogViewController
@implementation XGALogViewController
+ (void) startLog {
BNCLogSetOutputFunction(XGALogFunction);
BNCLogSetDisplayLevel(BNCLogLevelWarning);
BNCLog(@"%@ version %@(%@).",
[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleExecutable"],
[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"],
[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"]
);
}
+ (NSMutableArray<XGALogRow*>*) logArray {
static NSMutableArray*logArray = nil;
static dispatch_once_t onceToken = 0;
dispatch_once(&onceToken, ^ {
logArray = [[NSMutableArray alloc] init];
});
return logArray;
}
+ (instancetype) new {
XGALogViewController*controller = [[XGALogViewController alloc] init];
[[NSBundle mainBundle]
loadNibNamed:NSStringFromClass(self)
owner:controller
topLevelObjects:nil];
controller.window.excludedFromWindowsMenu = YES;
[controller startObservers];
[controller updateMessageFilter];
return controller;
}
+ (NSString*) stringForLogLevel:(BNCLogLevel)level {
NSArray<NSString*>*names = @[
@"Debug SDK",
@"Break Point",
@"Debug",
@"Warning",
@"Error",
@"Assert",
@"Information"
];
return names[level];
}
+ (NSImage*) imageForLogLevel:(BNCLogLevel)level {
/*
typedef NS_ENUM(NSInteger, BNCLogLevel) {
BNCLogLevelAll = 0,
BNCLogLevelDebugSDK = BNCLogLevelAll,
BNCLogLevelBreakPoint,
BNCLogLevelDebug,
BNCLogLevelWarning,
BNCLogLevelError,
BNCLogLevelAssert,
BNCLogLevelLog,
BNCLogLevelNone,
BNCLogLevelMax
};
*/
@synchronized(self) {
static NSArray*logIcons = nil;
if (!logIcons) {
logIcons = @[
[NSImage imageNamed:@"RoundDebug"], // 0 = BNCLogLevelDebugSDK
[NSImage imageNamed:@"RoundDebug"],
[NSImage imageNamed:@"RoundDebug"],
[NSImage imageNamed:@"RoundYellow"], // 3 = BNCLogLevelWarning
[NSImage imageNamed:@"RoundRed"],
[NSImage imageNamed:@"RoundAlert"], // 5 = Assert
[NSImage imageNamed:@"RoundBlue"],
];
}
return logIcons[level];
}
}
- (void) startObservers {
if (!self.dateFormatter) {
self.dateFormatter = [[NSDateFormatter alloc] init];
self.dateFormatter.dateStyle = NSDateFormatterShortStyle;
self.dateFormatter.timeStyle = NSDateFormatterShortStyle;
}
[self.tableView setDoubleAction:@selector(showStatusPopoverAction:)];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(logUpdatedNotification:)
name:XGALogUpdateNotification
object:nil];
self.arrayController.content = self.class.logArray;
[[XGASettings shared]
addObserver:self
forKeyPath:@"showDebugMessages"
options:0
context:NULL];
}
- (void) dealloc {
[self.statusPopover close];
[[NSNotificationCenter defaultCenter] removeObserver:self];
[[XGASettings shared] removeObserver:self forKeyPath:@"showDebugMessages"];
}
- (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary<NSKeyValueChangeKey, id> *)change
context:(void *)context {
[self updateMessageFilter];
}
- (void) updateMessageFilter {
if ([XGASettings shared].showDebugMessages) {
self.arrayController.filterPredicate = nil;
} else {
self.arrayController.filterPredicate =
[NSPredicate predicateWithFormat:@"logLevel > 2"];
}
}
- (void)logUpdatedNotification:(NSNotification*)notification {
BNCPerformBlockOnMainThreadAsync(^{
NSRect visibleRect = self.tableView.enclosingScrollView.documentVisibleRect;
NSRect contentRect = self.tableView.enclosingScrollView.documentView.frame;
// NSLog(@"Logging v: %@ c: %@.", NSStringFromRect(visibleRect), NSStringFromRect(contentRect));
[self.arrayController rearrangeObjects];
if ((visibleRect.origin.y + visibleRect.size.height) >= contentRect.size.height) {
NSInteger rowIdx = self.tableView.numberOfRows - 1;
if (rowIdx >= 0) {
// NSLog(@"Scroll to bottom: %ld.", rowIdx);
[self.tableView scrollRowToVisible:rowIdx];
}
}
});
}
- (void)tableViewSelectionDidChange:(NSNotification *)notification {
if (self.statusPopover.popover.isShown) [self showStatusPopoverAction:self.tableView];
}
- (void) showStatusPopoverAction:(id)sender {
NSInteger idx = self.tableView.selectedRow;
if (idx < 0 || idx >= [self.arrayController.arrangedObjects count]) {
[self.statusPopover close];
return;
}
XGALogRow *row = [self.arrayController.arrangedObjects objectAtIndex:idx];
if (![row isKindOfClass:XGALogRow.class]) return;
APFormattedString*status =
[[[APFormattedString
boldText:@"%@", [XGALogViewController stringForLogLevel:row.logLevel]]
plainText:@" "]
italicText:@"%@", [self.dateFormatter stringFromDate:row.date]];
if (!self.statusPopover) {
self.statusPopover = [[XGAStatusPopover alloc] init];
}
NSFont*font = [NSFont systemFontOfSize:[NSFont systemFontSize]];
self.statusPopover.statusImageView.image = [XGALogViewController imageForLogLevel:row.logLevel];
self.statusPopover.statusTextField.attributedStringValue = [status renderAttributedStringWithFont:font];
self.statusPopover.detailTextField.stringValue = row.logMessage;
NSRect r = [self.tableView rectOfRow:idx];
if ([sender isKindOfClass:NSTableView.class]) {
NSPoint p = self.window.mouseLocationOutsideOfEventStream;
r.size.width = 20.0;
r.origin.x = p.x - 10.0;
}
[self.statusPopover showRelativeToRect:r ofView:self.tableView preferredEdge:NSRectEdgeMaxY];
}
- (void)copy:(id)sender {
__auto_type copyBuffer = NSMutableString.new;
[self.tableView.selectedRowIndexes enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL *stop) {
XGALogRow*row = [self.arrayController.arrangedObjects objectAtIndex:idx];
[copyBuffer appendFormat:@"%@\t%@\t%@\n",
row.date,
BNCLogStringFromLogLevel(row.logLevel),
row.logMessage];
}];
__auto_type pb = [NSPasteboard generalPasteboard];
[pb clearContents];
[pb declareTypes:@[NSPasteboardTypeString] owner:nil];
[pb setString:copyBuffer forType:NSPasteboardTypeString];
}
@end