Skip to content

Commit 378e37a

Browse files
authored
Add an SSH logging channel (#9901)
* Add an SSH logging channel
1 parent 3612c22 commit 378e37a

File tree

3 files changed

+35
-12
lines changed

3 files changed

+35
-12
lines changed

Extension/src/SSH/sshCommandRunner.ts

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ import {
2323
ConnectionFailureInteractor
2424
} from './commandInteractors';
2525
import { isWindows, ISshHostInfo, splitLines, stripEscapeSequences, ProcessReturnType } from '../common';
26-
import { getOutputChannelLogger } from '../logger';
26+
import { getSshChannel } from '../logger';
27+
import { CppSettings } from '../LanguageServer/settings';
2728

2829
nls.config({ messageFormat: nls.MessageFormat.bundle, bundleFormat: nls.BundleFormat.standalone })();
2930
const localize: nls.LocalizeFunc = nls.loadMessageBundle();
@@ -257,6 +258,7 @@ export function runInteractiveSshTerminalCommand(args: ITerminalCommandArgs): Pr
257258
const disposables: vscode.Disposable[] = [];
258259
const { systemInteractor, command, interactors, nickname, token } = args;
259260
let logIsPaused: boolean = false;
261+
const loggingLevel: string | undefined = new CppSettings().loggingLevel;
260262
return new Promise(async (resolve, reject) => {
261263
let stdout: string = '';
262264
let windowListener: vscode.Disposable | undefined;
@@ -292,11 +294,13 @@ export function runInteractiveSshTerminalCommand(args: ITerminalCommandArgs): Pr
292294
if (!noClean) {
293295
clean();
294296
}
295-
getOutputChannelLogger().appendLine(cancel ? localize('ssh.terminal.command.canceled', '"{0}" terminal command canceled.', nickname) : localize('ssh.terminal.command.done', '"{0}" terminal command done.', nickname));
297+
getSshChannel().appendLine(cancel ? localize('ssh.terminal.command.canceled', '"{0}" terminal command canceled.', nickname) : localize('ssh.terminal.command.done', '"{0}" terminal command done.', nickname));
296298

297299
if (cancel) {
298300
if (continueWithoutExiting) {
299-
getOutputChannelLogger().showWarningMessage(localize('ssh.continuing.command.canceled', 'Task \'{0}\' is canceled, but the underlying command may not be terminated. Please check manually.', command));
301+
const warningMessage: string = localize('ssh.continuing.command.canceled', 'Task \'{0}\' is canceled, but the underlying command may not be terminated. Please check manually.', command);
302+
getSshChannel().appendLine(warningMessage);
303+
vscode.window.showWarningMessage(warningMessage);
300304
}
301305
return reject(new CanceledError());
302306
}
@@ -308,7 +312,9 @@ export function runInteractiveSshTerminalCommand(args: ITerminalCommandArgs): Pr
308312

309313
const failed = (error?: any) => {
310314
clean();
311-
getOutputChannelLogger().showErrorMessage(localize('ssh.process.failed', '"{0}" process failed: {1}', nickname, error));
315+
const errorMessage: string = localize('ssh.process.failed', '"{0}" process failed: {1}', nickname, error);
316+
getSshChannel().appendLine(errorMessage);
317+
vscode.window.showErrorMessage(errorMessage);
312318
reject(error);
313319
};
314320

@@ -341,7 +347,9 @@ export function runInteractiveSshTerminalCommand(args: ITerminalCommandArgs): Pr
341347
};
342348

343349
const handleTerminalOutput = async (dataWrite: vscode.TerminalDataWriteEvent): Promise<void> => {
344-
handleOutputLogging(dataWrite.data);
350+
if (loggingLevel !== 'None') {
351+
handleOutputLogging(dataWrite.data);
352+
}
345353
stdout += dataWrite.data;
346354

347355
if (interactors) {
@@ -385,7 +393,9 @@ export function runInteractiveSshTerminalCommand(args: ITerminalCommandArgs): Pr
385393
const logOutput: string = interaction.isPassword
386394
? interaction.response.replace(/./g, '*')
387395
: interaction.response;
388-
getOutputChannelLogger().appendLine(localize('ssh.wrote.data.to.terminal', '"{0}" wrote data to terminal: "{1}".', nickname, logOutput));
396+
if (loggingLevel === 'Debug' || loggingLevel === 'Information') {
397+
getSshChannel().appendLine(localize('ssh.wrote.data.to.terminal', '"{0}" wrote data to terminal: "{1}".', nickname, logOutput));
398+
}
389399
}
390400
}
391401
} catch (e) {
@@ -447,7 +457,9 @@ export function runInteractiveSshTerminalCommand(args: ITerminalCommandArgs): Pr
447457
const sendText: string = terminalIsWindows ? `(${args.sendText})\nexit /b %ErrorLevel%` : `${args.sendText}\nexit $?`;
448458

449459
terminal.sendText(sendText);
450-
getOutputChannelLogger().appendLine(localize('ssh.wrote.data.to.terminal', '"{0}" wrote data to terminal: "{1}".', nickname, args.sendText));
460+
if (loggingLevel === 'Debug' || loggingLevel === 'Information') {
461+
getSshChannel().appendLine(localize('ssh.wrote.data.to.terminal', '"{0}" wrote data to terminal: "{1}".', nickname, args.sendText));
462+
}
451463
}
452464

453465
if (args.showLoginTerminal) {
@@ -493,7 +505,7 @@ function logReceivedData(data: string, nickname: string): void {
493505
.map(line => `${nickname}> ${line}`)
494506
.join('\n');
495507

496-
getOutputChannelLogger().appendLine(markedLines);
508+
getSshChannel().appendLine(markedLines);
497509
}
498510

499511
function lastNonemptyLine(str: string): string | undefined {

Extension/src/SSH/sshHosts.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import {
1717
} from 'ssh-config';
1818
import { promisify } from 'util';
1919
import { ISshConfigHostInfo, ISshHostInfo, isWindows, resolveHome } from "../common";
20-
import { getOutputChannelLogger } from '../logger';
20+
import { getSshChannel } from '../logger';
2121
import * as glob from 'glob';
2222
import * as vscode from 'vscode';
2323
import * as nls from 'vscode-nls';
@@ -133,7 +133,7 @@ async function getIncludedConfigFile(config: Configuration, includePath: string)
133133
const parsed: Configuration = parse(includedContents);
134134
config.push(...parsed);
135135
} catch (e) {
136-
getOutputChannelLogger().appendLine(localize("failed.to.read.file", "Failed to read file {0}.", includePath));
136+
getSshChannel().appendLine(localize("failed.to.read.file", "Failed to read file {0}.", includePath));
137137
}
138138
}
139139

@@ -143,7 +143,7 @@ export async function writeSshConfiguration(configurationPath: string, configura
143143
await vscode.workspace.fs.createDirectory(vscode.Uri.file(path.dirname(configurationPath)));
144144
await fs.writeFile(configurationPath, configuration.toString());
145145
} catch (e) {
146-
getOutputChannelLogger().appendLine(localize("failed.to.write.file", "Failed to write to file {0}.", configurationPath));
146+
getSshChannel().appendLine(localize("failed.to.write.file", "Failed to write to file {0}.", configurationPath));
147147
}
148148
}
149149

@@ -153,7 +153,10 @@ async function getSshConfigSource(configurationPath: string): Promise<string> {
153153
const buffer: Buffer = await fs.readFile(configurationPath);
154154
return buffer.toString('utf8');
155155
} catch (e) {
156-
getOutputChannelLogger().appendLine(localize("failed.to.read.file", "Failed to read file {0}.", configurationPath));
156+
if ((e as NodeJS.ErrnoException).code === 'ENOENT') {
157+
return '';
158+
}
159+
getSshChannel().appendLine(localize("failed.to.read.file", "Failed to read file {0}.", configurationPath));
157160
}
158161

159162
return '';

Extension/src/logger.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ export let outputChannel: vscode.OutputChannel | undefined;
7575
export let diagnosticsChannel: vscode.OutputChannel | undefined;
7676
export let debugChannel: vscode.OutputChannel | undefined;
7777
export let warningChannel: vscode.OutputChannel | undefined;
78+
export let sshChannel: vscode.OutputChannel | undefined;
7879

7980
export function getOutputChannel(): vscode.OutputChannel {
8081
if (!outputChannel) {
@@ -96,6 +97,13 @@ export function getDiagnosticsChannel(): vscode.OutputChannel {
9697
return diagnosticsChannel;
9798
}
9899

100+
export function getSshChannel(): vscode.OutputChannel {
101+
if (!sshChannel) {
102+
sshChannel = vscode.window.createOutputChannel(localize("c.cpp.ssh.channel", "{0}: SSH", "Cpptools"));
103+
}
104+
return sshChannel;
105+
}
106+
99107
export function showOutputChannel(): void {
100108
getOutputChannel().show();
101109
}

0 commit comments

Comments
 (0)