forked from swiftlang/vscode-swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSwiftOutputChannel.ts
168 lines (143 loc) · 4.5 KB
/
SwiftOutputChannel.ts
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
//===----------------------------------------------------------------------===//
//
// This source file is part of the VS Code Swift open source project
//
// Copyright (c) 2021 the VS Code Swift project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of VS Code Swift project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import * as vscode from "vscode";
import configuration from "../configuration";
export class SwiftOutputChannel implements vscode.OutputChannel {
private channel: vscode.OutputChannel;
private logStore: RollingLog;
/**
* Creates a vscode.OutputChannel that allows for later retrival of logs.
* @param name
*/
constructor(
public name: string,
private logToConsole: boolean = true,
logStoreLinesSize: number = 250_000 // default to capturing 250k log lines
) {
this.name = name;
this.logToConsole = process.env["CI"] !== "1" && logToConsole;
this.channel = vscode.window.createOutputChannel(name);
this.logStore = new RollingLog(logStoreLinesSize);
}
append(value: string): void {
this.channel.append(value);
this.logStore.append(value);
if (this.logToConsole) {
console.log(value);
}
}
appendLine(value: string): void {
this.channel.appendLine(value);
this.logStore.appendLine(value);
if (this.logToConsole) {
console.log(value);
}
}
replace(value: string): void {
this.channel.replace(value);
this.logStore.replace(value);
}
clear(): void {
this.channel.clear();
}
show(column?: unknown, preserveFocus?: boolean | undefined): void {
this.channel.show(preserveFocus);
}
hide(): void {
this.channel.hide();
}
dispose() {
this.channel.dispose();
}
log(message: string, label?: string) {
let fullMessage: string;
if (label !== undefined) {
fullMessage = `${label}: ${message}`;
} else {
fullMessage = message;
}
this.appendLine(`${this.nowFormatted}: ${fullMessage}`);
}
logDiagnostic(message: string, label?: string) {
if (!configuration.diagnostics) {
return;
}
let fullMessage: string;
if (label !== undefined) {
fullMessage = `${label}: ${message}`;
} else {
fullMessage = message;
}
this.appendLine(`${this.nowFormatted}: ${fullMessage}`);
}
get nowFormatted(): string {
return new Date().toLocaleString("en-US", {
hourCycle: "h23",
hour: "2-digit",
minute: "numeric",
second: "numeric",
});
}
get logs(): string[] {
return this.logStore.logs;
}
}
class RollingLog {
private _logs: (string | null)[];
private startIndex: number = 0;
private endIndex: number = 0;
private logCount: number = 0;
private appending: boolean = false;
constructor(private maxLogs: number) {
this._logs = new Array(maxLogs).fill(null);
}
public get logs(): string[] {
const logs: string[] = [];
for (let i = 0; i < this.logCount; i++) {
logs.push(this._logs[(this.startIndex + i) % this.maxLogs]!);
}
return logs;
}
private incrementIndex(index: number): number {
return (index + 1) % this.maxLogs;
}
appendLine(log: string) {
// Writing to a new line that isn't the very first, increment the end index
if (this.logCount > 0) {
this.endIndex = this.incrementIndex(this.endIndex);
}
// We're over the window size, move the start index
if (this.logCount === this.maxLogs) {
this.startIndex = this.incrementIndex(this.startIndex);
} else {
this.logCount++;
}
this._logs[this.endIndex] = log;
}
append(log: string) {
if (this.logCount === 0) {
this.logCount = 1;
}
const newLogLine = (this._logs[this.endIndex] ?? "") + log;
this._logs[this.endIndex] = newLogLine;
this.appending = true;
}
replace(log: string) {
this._logs = new Array(this.maxLogs).fill(null);
this._logs[0] = log;
this.startIndex = 0;
this.endIndex = 1;
this.logCount = 1;
}
}