Skip to content

Commit 8b98126

Browse files
JustinGroteandyleejordan
authored andcommitted
Pre-Restart Experiments
1 parent 9b77ed3 commit 8b98126

File tree

3 files changed

+36
-66
lines changed

3 files changed

+36
-66
lines changed

src/logging.ts

Lines changed: 8 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT License.
33

4-
import { LogOutputChannel, Uri, Disposable, LogLevel, window, commands, Event } from "vscode";
4+
import { LogOutputChannel, LogLevel, window, Event } from "vscode";
55

66
/** Interface for logging operations. New features should use this interface for the "type" of logger.
77
* This will allow for easy mocking of the logger during unit tests.
88
*/
99
export interface ILogger {
10-
logDirectoryPath: Uri;
1110
write(message: string, ...additionalMessages: string[]): void;
1211
writeAndShowInformation(message: string, ...additionalMessages: string[]): Promise<void>;
1312
writeDiagnostic(message: string, ...additionalMessages: string[]): void;
@@ -22,39 +21,16 @@ export interface ILogger {
2221
}
2322

2423
export class Logger implements ILogger {
25-
public logDirectoryPath: Uri; // The folder for all the logs
26-
private commands: Disposable[];
2724
// Log output channel handles all the verbosity management so we don't have to.
2825
private logChannel: LogOutputChannel;
2926
public get logLevel(): LogLevel { return this.logChannel.logLevel;}
3027

31-
constructor(logPath: Uri, logChannel?: LogOutputChannel) {
28+
constructor(logChannel?: LogOutputChannel) {
3229
this.logChannel = logChannel ?? window.createOutputChannel("PowerShell", {log: true});
33-
// We have to override the scheme because it defaults to
34-
// 'vscode-userdata' which breaks UNC paths.
35-
this.logDirectoryPath = logPath;
36-
37-
// Early logging of the log paths for debugging.
38-
if (this.logLevel > LogLevel.Off) {
39-
this.logChannel.trace(`Log directory: ${this.logDirectoryPath.fsPath}`);
40-
}
41-
42-
this.commands = [
43-
commands.registerCommand(
44-
"PowerShell.ShowLogs",
45-
() => { this.showLogPanel(); }),
46-
47-
commands.registerCommand(
48-
"PowerShell.OpenLogFolder",
49-
async () => { await this.openLogFolder(); }),
50-
];
5130
}
5231

5332
public dispose(): void {
5433
this.logChannel.dispose();
55-
for (const command of this.commands) {
56-
command.dispose();
57-
}
5834
}
5935

6036
private writeAtLevel(logLevel: LogLevel, message: string, ...additionalMessages: string[]): void {
@@ -137,14 +113,10 @@ export class Logger implements ILogger {
137113
}
138114
}
139115

140-
private showLogPanel(): void {
116+
public showLogPanel(): void {
141117
this.logChannel.show();
142118
}
143119

144-
private async openLogFolder(): Promise<void> {
145-
await commands.executeCommand("openFolder", this.logDirectoryPath, true);
146-
}
147-
148120
private async writeLine(message: string, level: LogLevel = LogLevel.Info): Promise<void> {
149121
return new Promise<void>((resolve) => {
150122
switch (level) {
@@ -163,12 +135,13 @@ export class Logger implements ILogger {
163135

164136
/** Parses logs received via the legacy OutputChannel to LogOutputChannel with proper severity.
165137
*
166-
* HACK: This is for legacy compatability and can be removed when https://github.com/microsoft/vscode-languageserver-node/issues/1116 is merged and replaced with a normal LogOutputChannel. We don't use a middleware here because any direct logging calls like client.warn() would not be captured by middleware.
138+
* HACK: This is for legacy compatability and can be removed when https://github.com/microsoft/vscode-languageserver-node/issues/1116 is merged and replaced with a normal LogOutputChannel. We don't use a middleware here because any direct logging calls like client.warn() and server-initiated messages would not be captured by middleware.
167139
*/
168140
export class LanguageClientOutputChannelAdapter implements LogOutputChannel {
169141
private channel: LogOutputChannel;
170-
constructor(channel: LogOutputChannel) {
171-
this.channel = channel;
142+
143+
constructor(public channelName: string) {
144+
this.channel = window.createOutputChannel(channelName, {log: true});
172145
}
173146

174147
public appendLine(message: string): void {
@@ -229,7 +202,7 @@ export class LanguageClientOutputChannelAdapter implements LogOutputChannel {
229202
this.channel.error(message);
230203
break;
231204
default:
232-
this.channel.trace(message);
205+
this.channel.error("!UNKNOWN LOG LEVEL!: " + message);
233206
break;
234207
}
235208
}
@@ -284,7 +257,6 @@ export class LanguageClientOutputChannelAdapter implements LogOutputChannel {
284257

285258
/** Overrides the severity of some LSP traces to be more logical */
286259
export class LanguageClientTraceFormatter extends LanguageClientOutputChannelAdapter {
287-
288260
public override appendLine(message: string): void {
289261
this.append(message);
290262
}
@@ -293,7 +265,6 @@ export class LanguageClientTraceFormatter extends LanguageClientOutputChannelAda
293265
// eslint-disable-next-line prefer-const
294266
let [parsedMessage, level] = this.parse(message);
295267

296-
// Override some LSP traces to be more logical
297268
if (parsedMessage.startsWith("Sending ")) {
298269
parsedMessage = parsedMessage.replace("Sending", "▶️");
299270
level = LogLevel.Debug;

src/process.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ export class PowerShellProcess {
3030
private isTemp: boolean,
3131
private shellIntegrationEnabled: boolean,
3232
private logger: ILogger,
33+
private logDirectoryPath: vscode.Uri,
3334
private startPsesArgs: string,
3435
private sessionFilePath: vscode.Uri,
3536
private sessionSettings: Settings) {
@@ -51,7 +52,7 @@ export class PowerShellProcess {
5152
: "";
5253

5354
this.startPsesArgs +=
54-
`-LogPath '${utils.escapeSingleQuotes(this.logger.logDirectoryPath.fsPath)}' ` +
55+
`-LogPath '${utils.escapeSingleQuotes(this.logDirectoryPath.fsPath)}' ` +
5556
`-SessionDetailsPath '${utils.escapeSingleQuotes(this.sessionFilePath.fsPath)}' ` +
5657
`-FeatureFlags @(${featureFlags}) `;
5758

src/session.ts

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,17 @@ import net = require("net");
55
import path = require("path");
66
import vscode = require("vscode");
77
import TelemetryReporter, { TelemetryEventProperties, TelemetryEventMeasurements } from "@vscode/extension-telemetry";
8-
import { Message } from "vscode-jsonrpc";
9-
import { ILogger, LanguageClientOutputChannelAdapter, LanguageClientTraceFormatter } from "./logging";
8+
import { Message, Trace } from "vscode-jsonrpc";
9+
import { ILogger } from "./logging";
1010
import { PowerShellProcess } from "./process";
1111
import { Settings, changeSetting, getSettings, getEffectiveConfigurationTarget, validateCwdSetting } from "./settings";
1212
import utils = require("./utils");
1313

1414
import {
1515
CloseAction, CloseHandlerResult, DocumentSelector, ErrorAction, ErrorHandlerResult,
1616
LanguageClientOptions, Middleware, NotificationType,
17-
RequestType0, ResolveCodeLensSignature
17+
RequestType0, ResolveCodeLensSignature,
18+
RevealOutputChannelOn,
1819
} from "vscode-languageclient";
1920
import { LanguageClient, StreamInfo } from "vscode-languageclient/node";
2021

@@ -93,28 +94,7 @@ export class SessionManager implements Middleware {
9394
private startCancellationTokenSource: vscode.CancellationTokenSource | undefined;
9495
private suppressRestartPrompt = false;
9596
private versionDetails: IPowerShellVersionDetails | undefined;
96-
97-
private _outputChannel?: vscode.LogOutputChannel;
98-
/** Omnisharp and PSES messages sent via LSP are surfaced here. */
99-
private get outputChannel(): vscode.LogOutputChannel | undefined {
100-
return vscode.workspace.getConfiguration("powershell.developer").get<boolean>("traceLsp", false)
101-
? this._outputChannel
102-
??= new LanguageClientOutputChannelAdapter(
103-
vscode.window.createOutputChannel("PowerShell: Editor Services", { log: true })
104-
)
105-
: undefined;
106-
}
107-
108-
private _traceOutputChannel?: vscode.LogOutputChannel;
109-
/** The LanguageClient LSP message trace is surfaced here. */
110-
private get traceOutputChannel(): vscode.LogOutputChannel | undefined {
111-
return vscode.workspace.getConfiguration("powershell.developer").get<boolean>("traceLsp", false)
112-
? this._traceOutputChannel
113-
??= new LanguageClientTraceFormatter(
114-
vscode.window.createOutputChannel("PowerShell: Trace LSP", { log: true })
115-
)
116-
: undefined;
117-
}
97+
private traceLogLevelHandler?: vscode.Disposable;
11898

11999
constructor(
120100
private extensionContext: vscode.ExtensionContext,
@@ -126,7 +106,6 @@ export class SessionManager implements Middleware {
126106
hostVersion: string,
127107
publisher: string,
128108
private telemetryReporter: TelemetryReporter) {
129-
130109
// Create the language status item
131110
this.languageStatusItem = this.createStatusBarItem();
132111
// We have to override the scheme because it defaults to
@@ -299,6 +278,8 @@ export class SessionManager implements Middleware {
299278
this.startCancellationTokenSource?.dispose();
300279
this.startCancellationTokenSource = undefined;
301280
this.sessionDetails = undefined;
281+
this.traceLogLevelHandler?.dispose();
282+
this.traceLogLevelHandler = undefined;
302283

303284
this.setSessionStatus("Not Started", SessionStatus.NotStarted);
304285
}
@@ -375,6 +356,7 @@ export class SessionManager implements Middleware {
375356
true,
376357
false,
377358
this.logger,
359+
this.extensionContext.logUri,
378360
this.getEditorServicesArgs(bundledModulesPath, this.PowerShellExeDetails) + "-DebugServiceOnly ",
379361
this.getNewSessionFilePath(),
380362
this.sessionSettings);
@@ -585,6 +567,7 @@ export class SessionManager implements Middleware {
585567
false,
586568
this.shellIntegrationEnabled,
587569
this.logger,
570+
this.extensionContext.logUri,
588571
this.getEditorServicesArgs(bundledModulesPath, powerShellExeDetails),
589572
this.getNewSessionFilePath(),
590573
this.sessionSettings);
@@ -669,6 +652,7 @@ export class SessionManager implements Middleware {
669652
});
670653
});
671654
};
655+
672656
const clientOptions: LanguageClientOptions = {
673657
documentSelector: this.documentSelector,
674658
synchronize: {
@@ -705,8 +689,9 @@ export class SessionManager implements Middleware {
705689
},
706690
},
707691
middleware: this,
708-
traceOutputChannel: this.traceOutputChannel,
709-
outputChannel: this.outputChannel
692+
// traceOutputChannel: traceOutputChannel,
693+
// outputChannel: outputChannel,
694+
revealOutputChannelOn: RevealOutputChannelOn.Never
710695
};
711696

712697
const languageClient = new LanguageClient("powershell", "PowerShell Editor Services Client", connectFunc, clientOptions);
@@ -759,6 +744,19 @@ export class SessionManager implements Middleware {
759744
return languageClient;
760745
}
761746

747+
/** Synchronizes a vscode LogOutputChannel log level to the LSP trace setting to minimize traffic */
748+
private async setLspTrace(languageClient: LanguageClient, level: vscode.LogLevel): Promise<void> {
749+
this.logger.writeVerbose("LSP Trace level changed to: " + level.toString());
750+
if (level == vscode.LogLevel.Trace) {
751+
return languageClient.setTrace(Trace.Verbose);
752+
} else if (level == vscode.LogLevel.Debug) {
753+
return languageClient.setTrace(Trace.Messages);
754+
} else {
755+
return languageClient.setTrace(Trace.Off);
756+
}
757+
}
758+
759+
762760
private async getBundledModulesPath(): Promise<string> {
763761
// Because the extension is always at `<root>/out/main.js`
764762
let bundledModulesPath = path.resolve(__dirname, "../modules");

0 commit comments

Comments
 (0)