-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy pathLogOutputManager.ts
More file actions
407 lines (371 loc) · 16.3 KB
/
LogOutputManager.ts
File metadata and controls
407 lines (371 loc) · 16.3 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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
import * as vscode from 'vscode';
import type { DiagnosticCollection } from 'vscode';
import type { BSDebugDiagnostic } from 'roku-debug';
import { isChanperfEvent, isDiagnosticsEvent, isLaunchStartEvent, isLogOutputEvent, isPopupMessageEvent, isRendezvousEvent } from 'roku-debug';
import type { DeclarationProvider } from './DeclarationProvider';
import type { LogDocumentLinkProvider } from './LogDocumentLinkProvider';
import { CustomDocumentLink } from './LogDocumentLinkProvider';
import * as fsExtra from 'fs-extra';
import type { BrightScriptLaunchConfiguration } from './DebugConfigurationProvider';
export class LogLine {
constructor(
public text: string,
public isMustInclude: boolean
) {
}
}
export class LogOutputManager {
constructor(
outputChannel,
context,
docLinkProvider,
private declarationProvider: DeclarationProvider
) {
this.collection = vscode.languages.createDiagnosticCollection('BrightScript');
this.outputChannel = outputChannel;
this.docLinkProvider = docLinkProvider;
this.loadConfigSettings();
vscode.workspace.onDidChangeConfiguration((e) => {
this.loadConfigSettings();
});
this.context = context;
let subscriptions = context.subscriptions;
this.includeRegex = null;
this.logLevelRegex = null;
this.excludeRegex = null;
/**
* we want to catch a few different link formats here:
* - pkg:/path/file.brs(LINE:COL)
* - file://path/file.bs:LINE
* - at line LINE of file pkg:/path/file.brs - this case can arise when the device reports various scenegraph errors such as fields not present, or texture size issues, etc
*/
this.pkgRegex = /(?:\s*at line (\d*) of file )*(?:(pkg:\/|file:\/\/)(.*\.(bs|brs|xml)))[ \t]*(?:(?:(?:\()(\d+)(?:\:(\d+))?\)?)|(?:\:(\d+)?))*/;
this.debugStartRegex = /BrightScript Micro Debugger\./ig;
this.debugEndRegex = /Brightscript Debugger>/ig;
subscriptions.push(vscode.commands.registerCommand('extension.brightscript.markLogOutput', () => {
this.markOutput();
}));
subscriptions.push(vscode.commands.registerCommand('extension.brightscript.clearLogOutput', () => {
this.clearOutput();
}));
subscriptions.push(vscode.commands.registerCommand('extension.brightscript.setOutputIncludeFilter', async () => {
let entryText: string = await vscode.window.showInputBox({
placeHolder: 'Enter log include regex',
value: this.includeRegex ? this.includeRegex.source : ''
});
if (entryText || entryText === '') {
this.setIncludeFilter(entryText);
}
}));
subscriptions.push(vscode.commands.registerCommand('extension.brightscript.setOutputLogLevelFilter', async () => {
let entryText: string = await vscode.window.showInputBox({
placeHolder: 'Enter log level regex',
value: this.logLevelRegex ? this.logLevelRegex.source : ''
});
if (entryText || entryText === '') {
this.setLevelFilter(entryText);
}
}));
subscriptions.push(vscode.commands.registerCommand('extension.brightscript.setOutputExcludeFilter', async () => {
let entryText: string = await vscode.window.showInputBox({
placeHolder: 'Enter log exclude regex',
value: this.excludeRegex ? this.excludeRegex.source : ''
});
if (entryText || entryText === '') {
this.setExcludeFilter(entryText);
}
}));
this.clearOutput();
}
private context: any;
private displayedLogLines: LogLine[];
private allLogLines: LogLine[];
private markCount: number;
private includeRegex?: RegExp;
private logLevelRegex?: RegExp;
private excludeRegex?: RegExp;
private pkgRegex: RegExp;
private isNextBreakpointSkipped = false;
private includeStackTraces: boolean;
private isInMicroDebugger: boolean;
public launchConfig: BrightScriptLaunchConfiguration;
public isFocusingOutputOnLaunch: boolean;
public isClearingOutputOnLaunch: boolean;
public isClearingConsoleOnChannelStart: boolean;
public hyperlinkFormat: string;
private collection: DiagnosticCollection;
private outputChannel: vscode.OutputChannel;
private docLinkProvider: LogDocumentLinkProvider;
private debugStartRegex: RegExp;
private debugEndRegex: RegExp;
public onDidStartDebugSession() {
this.isInMicroDebugger = false;
this.isNextBreakpointSkipped = false;
if (this.isClearingConsoleOnChannelStart) {
this.clearOutput();
}
}
private loadConfigSettings() {
let config: any = vscode.workspace.getConfiguration('brightscript') || {};
this.includeStackTraces = config.output?.includeStackTraces;
this.isFocusingOutputOnLaunch = config?.output?.focusOnLaunch === false ? false : true;
this.isClearingOutputOnLaunch = config?.output?.clearOnLaunch === false ? false : true;
this.isClearingConsoleOnChannelStart = config?.output?.clearConsoleOnChannelStart === false ? false : true;
this.hyperlinkFormat = config.output?.hyperlinkFormat;
}
public setLaunchConfig(launchConfig: BrightScriptLaunchConfiguration) {
this.launchConfig = launchConfig;
}
public async onDidReceiveDebugSessionCustomEvent(e: { event: string; body?: any }) {
if (isRendezvousEvent(e) || isChanperfEvent(e)) {
// No need to handle rendezvous type events
return;
}
if (isLogOutputEvent(e)) {
this.appendLine(e.body.line);
} else if (isPopupMessageEvent(e)) {
this.showMessage(e.body.message, e.body.severity);
} else if (isLaunchStartEvent(e)) {
this.isInMicroDebugger = false;
this.isNextBreakpointSkipped = false;
if (this.isFocusingOutputOnLaunch) {
await vscode.commands.executeCommand('workbench.action.focusPanel');
this.outputChannel.show();
}
if (this.isClearingOutputOnLaunch) {
this.clearOutput();
}
} else if (isDiagnosticsEvent(e)) {
let errorsByPath = {};
for (const diagnostic of e.body.diagnostics) {
if (diagnostic.path) {
if (!errorsByPath[diagnostic.path]) {
errorsByPath[diagnostic.path] = [];
}
errorsByPath[diagnostic.path].push(diagnostic);
}
}
for (const path in errorsByPath) {
if (errorsByPath.hasOwnProperty(path)) {
const errors = errorsByPath[path];
await this.addDiagnosticForError(path, errors);
}
}
}
}
private showMessage(message: string, severity: string) {
const methods = {
error: vscode.window.showErrorMessage,
info: vscode.window.showInformationMessage,
warn: vscode.window.showWarningMessage
};
methods[severity](message);
}
public async addDiagnosticForError(path: string, diagnostics: BSDebugDiagnostic[]) {
//TODO get the actual folder
let documentUri: vscode.Uri;
let uri = vscode.Uri.file(path);
let doc = await vscode.workspace.openTextDocument(uri); // calls back
if (doc !== undefined) {
documentUri = doc.uri;
}
// console.log("got " + documentUri);
//debug crap - for some reason - using this URI works - using the one from the path does not :()
// const document = vscode.window.activeTextEditor.document;
// const currentDocumentUri = document.uri;
// console.log("currentDocumentUri " + currentDocumentUri);
if (documentUri !== undefined) {
let result: vscode.Diagnostic[] = [];
for (const diagnostic of diagnostics) {
result.push({
code: diagnostic.code,
message: diagnostic.message,
source: diagnostic.source,
severity: diagnostic.severity,
tags: diagnostic.tags,
range: new vscode.Range(
new vscode.Position(diagnostic.range.start.line, diagnostic.range.start.character),
new vscode.Position(diagnostic.range.end.line, diagnostic.range.end.character)
)
});
}
this.collection.set(documentUri, result);
}
}
/**
* Log output methods
*/
public appendLine(lineText: string, mustInclude = false): void {
let lines = lineText.split(/\r?\n/g);
for (let line of lines) {
if (line !== '') {
if (!this.includeStackTraces) {
// filter out debugger noise
if (this.debugStartRegex.exec(line)) {
console.log('start MicroDebugger block');
this.isInMicroDebugger = true;
this.isNextBreakpointSkipped = false;
line = 'Pausing for a breakpoint...';
} else if (this.isInMicroDebugger && (this.debugEndRegex.exec(line))) {
console.log('ended MicroDebugger block');
this.isInMicroDebugger = false;
if (this.isNextBreakpointSkipped) {
line = '\n**Was a bogus breakpoint** Skipping!\n';
} else {
line = null;
}
} else if (this.isInMicroDebugger) {
if (this.launchConfig.enableDebuggerAutoRecovery && line.startsWith('Break in ')) {
console.log('this block is a break: skipping it');
this.isNextBreakpointSkipped = true;
}
line = null;
}
}
}
if (line) {
const logLine = new LogLine(line, mustInclude);
this.allLogLines.push(logLine);
if (this.shouldLineBeShown(logLine)) {
this.allLogLines.push(logLine);
this.addLogLineToOutput(logLine);
this.writeLogLineToLogfile(logLine.text);
}
}
}
}
public writeLogLineToLogfile(text: string) {
if (this.launchConfig?.logfilePath) {
fsExtra.appendFileSync(this.launchConfig.logfilePath, text + '\n');
}
}
public addLogLineToOutput(logLine: LogLine) {
const logLineNumber = this.displayedLogLines.length;
if (this.shouldLineBeShown(logLine)) {
this.displayedLogLines.push(logLine);
let match = this.pkgRegex.exec(logLine.text);
if (match) {
const isFilePath = match[2] === 'file://';
const path = isFilePath ? match[3] : 'pkg:/' + match[3];
let lineNumber = match[1] ? Number(match[1]) : undefined;
if (!lineNumber) {
if (isFilePath) {
lineNumber = Number(match[7]);
if (isNaN(lineNumber)) {
lineNumber = Number(match[5]);
}
} else {
lineNumber = Number(match[5]);
}
}
const filename = this.getFilename(path);
const ext = `.${match[4]}`.toLowerCase();
let customText = this.getCustomLogText(path, filename, ext, Number(lineNumber), logLineNumber, isFilePath);
const customLink = new CustomDocumentLink(logLineNumber, match.index, customText.length, path, lineNumber, filename);
if (isFilePath) {
this.docLinkProvider.addCustomFileLink(customLink);
} else {
this.docLinkProvider.addCustomPkgLink(customLink);
}
let logText = logLine.text.substring(0, match.index) + customText + logLine.text.substring(match.index + match[0].length);
this.outputChannel.appendLine(logText);
} else {
this.outputChannel.appendLine(logLine.text);
}
}
}
public getFilename(pkgPath: string): string {
const parts = pkgPath.split('/');
let name = parts.length > 0 ? parts[parts.length - 1] : pkgPath;
if (name.toLowerCase().endsWith('.xml') || name.toLowerCase().endsWith('.brs')) {
name = name.substring(0, name.length - 4);
} else if (name.toLowerCase().endsWith('.bs')) {
name = name.substring(0, name.length - 3);
}
return name;
}
public getCustomLogText(pkgPath: string, filename: string, extension: string, lineNumber: number, logLineNumber: number, isFilePath: boolean): string {
switch (this.hyperlinkFormat) {
case 'Full':
return pkgPath + `(${lineNumber})`;
break;
case 'Short':
return `#${logLineNumber}`;
break;
case 'Hidden':
return ' ';
break;
case 'Filename':
return `${filename}${extension}(${lineNumber})`;
break;
default:
if (extension === '.brs' || extension === '.bs') {
const methodName = this.getMethodName(pkgPath, lineNumber, isFilePath);
if (methodName) {
return `${filename}.${methodName}(${lineNumber})`;
}
}
return pkgPath + `(${lineNumber})`;
break;
}
}
public getMethodName(path: string, lineNumber: number, isFilePath: boolean): string | null {
let fsPath = isFilePath ? path : this.docLinkProvider.convertPkgPathToFsPath(path);
const method = fsPath ? this.declarationProvider.getFunctionBeforeLine(fsPath, lineNumber) : null;
return method ? method.name : null;
}
private shouldLineBeShown(logLine: LogLine): boolean {
//filter excluded lines
if (this.excludeRegex?.test(logLine.text)) {
return false;
}
//once past the exclude filter, always keep "mustInclude" lines
if (logLine.isMustInclude) {
return true;
}
//throw out lines that don't match the logLevelRegex (if we have one)
if (this.logLevelRegex && !this.logLevelRegex.test(logLine.text)) {
return false;
}
//throw out lines that don't match the includeRegex (if we have one)
if (this.includeRegex && !this.includeRegex.test(logLine.text)) {
return false;
}
//all other log entries should be kept
return true;
}
public clearOutput(): any {
this.markCount = 0;
this.allLogLines = [];
this.displayedLogLines = [];
this.outputChannel.clear();
this.collection.clear();
this.docLinkProvider.resetCustomLinks();
}
public setIncludeFilter(text: string): void {
this.includeRegex = text && text.trim() !== '' ? new RegExp(text, 'i') : null;
this.reFilterOutput();
}
public setExcludeFilter(text: string): void {
this.excludeRegex = text && text.trim() !== '' ? new RegExp(text, 'i') : null;
this.reFilterOutput();
}
public setLevelFilter(text: string): void {
this.logLevelRegex = text && text.trim() !== '' ? new RegExp(text, 'i') : null;
this.reFilterOutput();
}
public reFilterOutput(): void {
this.outputChannel.clear();
this.docLinkProvider.resetCustomLinks();
for (let i = 0; i < this.allLogLines.length - 1; i++) {
let logLine = this.allLogLines[i];
if (this.shouldLineBeShown(logLine)) {
this.addLogLineToOutput(logLine);
}
}
}
public markOutput(): void {
this.appendLine(`---------------------- MARK ${this.markCount} ----------------------`, true);
this.markCount++;
}
}