Skip to content

Commit 9ff2d56

Browse files
committed
Change showAndLog functions to take NotificationLogger
1 parent d54ee0c commit 9ff2d56

44 files changed

Lines changed: 277 additions & 88 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

extensions/ql-vscode/src/codeql-cli/distribution.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ export class DistributionManager implements DistributionProvider {
160160
if (this.config.customCodeQlPath) {
161161
if (!(await pathExists(this.config.customCodeQlPath))) {
162162
void showAndLogErrorMessage(
163+
extLogger,
163164
`The CodeQL executable path is specified as "${this.config.customCodeQlPath}" ` +
164165
"by a configuration setting, but a CodeQL executable could not be found at that path. Please check " +
165166
"that a CodeQL executable exists at the specified path or remove the setting.",
@@ -852,6 +853,7 @@ export async function getExecutableFromDirectory(
852853

853854
function warnDeprecatedLauncher() {
854855
void showAndLogWarningMessage(
856+
extLogger,
855857
`The "${deprecatedCodeQlLauncherName()!}" launcher has been deprecated and will be removed in a future version. ` +
856858
`Please use "${codeQlLauncherName()}" instead. It is recommended to update to the latest CodeQL binaries.`,
857859
);

extensions/ql-vscode/src/codeql-cli/query-language.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ export async function askForLanguage(
5959
throw new UserCancellationException("Cancelled.");
6060
} else {
6161
void showAndLogErrorMessage(
62+
extLogger,
6263
"Language not found. Language must be specified manually.",
6364
);
6465
}
@@ -67,6 +68,7 @@ export async function askForLanguage(
6768

6869
if (!isQueryLanguage(language)) {
6970
void showAndLogErrorMessage(
71+
extLogger,
7072
`Language '${language}' is not supported. Only languages ${Object.values(
7173
QueryLanguage,
7274
).join(", ")} are supported.`,
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export * from "./logger";
2+
export * from "./notification-logger";
23
export * from "./tee-logger";
34
export * from "./vscode/loggers";
45
export * from "./vscode/output-channel-logger";
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { Logger } from "./logger";
2+
3+
export interface NotificationLogger extends Logger {
4+
showErrorMessage(message: string): Promise<void>;
5+
showWarningMessage(message: string): Promise<void>;
6+
showInformationMessage(message: string): Promise<void>;
7+
}

extensions/ql-vscode/src/common/logging/vscode/output-channel-logger.ts

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
import { window as Window, OutputChannel, Progress } from "vscode";
22
import { Logger, LogOptions } from "../logger";
33
import { DisposableObject } from "../../../pure/disposable-object";
4+
import { NotificationLogger } from "../notification-logger";
45

56
/**
67
* A logger that writes messages to an output channel in the VS Code Output tab.
78
*/
8-
export class OutputChannelLogger extends DisposableObject implements Logger {
9+
export class OutputChannelLogger
10+
extends DisposableObject
11+
implements Logger, NotificationLogger
12+
{
913
public readonly outputChannel: OutputChannel;
1014
isCustomLogDirectory: boolean;
1115

@@ -42,6 +46,30 @@ export class OutputChannelLogger extends DisposableObject implements Logger {
4246
show(preserveFocus?: boolean): void {
4347
this.outputChannel.show(preserveFocus);
4448
}
49+
50+
async showErrorMessage(message: string): Promise<void> {
51+
await this.showMessage(message, Window.showErrorMessage);
52+
}
53+
54+
async showInformationMessage(message: string): Promise<void> {
55+
await this.showMessage(message, Window.showInformationMessage);
56+
}
57+
58+
async showWarningMessage(message: string): Promise<void> {
59+
await this.showMessage(message, Window.showWarningMessage);
60+
}
61+
62+
private async showMessage(
63+
message: string,
64+
show: (message: string, ...items: string[]) => Thenable<string | undefined>,
65+
): Promise<void> {
66+
const label = "Show Log";
67+
const result = await show(message, label);
68+
69+
if (result === label) {
70+
this.show();
71+
}
72+
}
4573
}
4674

4775
export type ProgressReporter = Progress<{ message: string }>;

extensions/ql-vscode/src/common/vscode/commands.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,18 +54,15 @@ export function registerCommandWithErrorHandling(
5454
if (e.silent) {
5555
void outputLogger.log(errorMessage.fullMessage);
5656
} else {
57-
void showAndLogWarningMessage(errorMessage.fullMessage, {
58-
outputLogger,
59-
});
57+
void showAndLogWarningMessage(outputLogger, errorMessage.fullMessage);
6058
}
6159
} else {
6260
// Include the full stack in the error log only.
6361
const errorStack = getErrorStack(e);
6462
const fullMessage = errorStack
6563
? `${errorMessage.fullMessage}\n${errorStack}`
6664
: errorMessage.fullMessage;
67-
void showAndLogExceptionWithTelemetry(errorMessage, {
68-
outputLogger,
65+
void showAndLogExceptionWithTelemetry(outputLogger, errorMessage, {
6966
fullMessage,
7067
extraTelemetryProperties: {
7168
command: commandId,

extensions/ql-vscode/src/common/vscode/external-files.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
getErrorStack,
99
} from "../../pure/helpers-pure";
1010
import { showAndLogExceptionWithTelemetry } from "./log";
11+
import { extLogger } from "../logging";
1112

1213
export async function tryOpenExternalFile(
1314
commandManager: AppCommandManager,
@@ -34,6 +35,7 @@ the file in the file explorer and dragging it into the workspace.`,
3435
await commandManager.execute("revealFileInOS", uri);
3536
} catch (e) {
3637
void showAndLogExceptionWithTelemetry(
38+
extLogger,
3739
redactableError(
3840
asError(e),
3941
)`Failed to reveal file in OS: ${getErrorMessage(e)}`,
@@ -42,6 +44,7 @@ the file in the file explorer and dragging it into the workspace.`,
4244
}
4345
} else {
4446
void showAndLogExceptionWithTelemetry(
47+
extLogger,
4548
redactableError(asError(e))`Could not open file ${fileLocation}`,
4649
{
4750
fullMessage: `${getErrorMessage(e)}\n${getErrorStack(e)}`,
Lines changed: 37 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,13 @@
1-
import { window } from "vscode";
21
import { RedactableError } from "../../pure/errors";
32
import { telemetryListener } from "../../telemetry";
4-
import { extLogger, OutputChannelLogger } from "../logging";
3+
import { NotificationLogger } from "../logging";
54

65
interface ShowAndLogExceptionOptions extends ShowAndLogOptions {
76
/** Custom properties to include in the telemetry report. */
87
extraTelemetryProperties?: { [key: string]: string };
98
}
109

1110
interface ShowAndLogOptions {
12-
/** The output logger that will receive the message. */
13-
outputLogger?: OutputChannelLogger;
14-
/** A set of items that will be rendered as actions in the message. */
15-
items?: string[];
1611
/**
1712
* An alternate message that is added to the log, but not displayed in the popup.
1813
* This is useful for adding extra detail to the logs that would be too noisy for the popup.
@@ -23,34 +18,39 @@ interface ShowAndLogOptions {
2318
/**
2419
* Show an error message, log it to the console, and emit redacted information as telemetry
2520
*
21+
* @param logger The logger that will receive the message.
2622
* @param error The error to show. Only redacted information will be included in the telemetry.
2723
* @param options See individual fields on `ShowAndLogExceptionOptions` type.
2824
*
2925
* @return A promise that resolves to the selected item or undefined when being dismissed.
3026
*/
3127
export async function showAndLogExceptionWithTelemetry(
28+
logger: NotificationLogger,
3229
error: RedactableError,
3330
options: ShowAndLogExceptionOptions = {},
34-
): Promise<string | undefined> {
31+
): Promise<void> {
3532
telemetryListener?.sendError(error, options.extraTelemetryProperties);
36-
return showAndLogErrorMessage(error.fullMessage, options);
33+
return showAndLogErrorMessage(logger, error.fullMessage, options);
3734
}
3835

3936
/**
4037
* Show an error message and log it to the console
4138
*
39+
* @param logger The logger that will receive the message.
4240
* @param message The message to show.
43-
* @param options See individual fields on `ShowAndLogOptions` type.
41+
* @param options? See individual fields on `ShowAndLogOptions` type.
4442
*
4543
* @return A promise that resolves to the selected item or undefined when being dismissed.
4644
*/
4745
export async function showAndLogErrorMessage(
46+
logger: NotificationLogger,
4847
message: string,
4948
options?: ShowAndLogOptions,
50-
): Promise<string | undefined> {
49+
): Promise<void> {
5150
return internalShowAndLog(
51+
logger,
5252
dropLinesExceptInitial(message),
53-
window.showErrorMessage,
53+
logger.showErrorMessage,
5454
{ fullMessage: message, ...options },
5555
);
5656
}
@@ -62,48 +62,53 @@ function dropLinesExceptInitial(message: string, n = 2) {
6262
/**
6363
* Show a warning message and log it to the console
6464
*
65+
* @param logger The logger that will receive the message.
6566
* @param message The message to show.
66-
* @param options See individual fields on `ShowAndLogOptions` type.
67+
* @param options? See individual fields on `ShowAndLogOptions` type.
6768
*
6869
* @return A promise that resolves to the selected item or undefined when being dismissed.
6970
*/
7071
export async function showAndLogWarningMessage(
72+
logger: NotificationLogger,
7173
message: string,
7274
options?: ShowAndLogOptions,
73-
): Promise<string | undefined> {
74-
return internalShowAndLog(message, window.showWarningMessage, options);
75+
): Promise<void> {
76+
return internalShowAndLog(
77+
logger,
78+
message,
79+
logger.showWarningMessage,
80+
options,
81+
);
7582
}
7683

7784
/**
7885
* Show an information message and log it to the console
7986
*
87+
* @param logger The logger that will receive the message.
8088
* @param message The message to show.
81-
* @param options See individual fields on `ShowAndLogOptions` type.
89+
* @param options? See individual fields on `ShowAndLogOptions` type.
8290
*
8391
* @return A promise that resolves to the selected item or undefined when being dismissed.
8492
*/
8593
export async function showAndLogInformationMessage(
94+
logger: NotificationLogger,
8695
message: string,
8796
options?: ShowAndLogOptions,
88-
): Promise<string | undefined> {
89-
return internalShowAndLog(message, window.showInformationMessage, options);
97+
): Promise<void> {
98+
return internalShowAndLog(
99+
logger,
100+
message,
101+
logger.showInformationMessage,
102+
options,
103+
);
90104
}
91105

92-
type ShowMessageFn = (
93-
message: string,
94-
...items: string[]
95-
) => Thenable<string | undefined>;
96-
97106
async function internalShowAndLog(
107+
logger: NotificationLogger,
98108
message: string,
99-
fn: ShowMessageFn,
100-
{ items = [], outputLogger = extLogger, fullMessage }: ShowAndLogOptions = {},
101-
): Promise<string | undefined> {
102-
const label = "Show Log";
103-
void outputLogger.log(fullMessage || message);
104-
const result = await fn(message, label, ...items);
105-
if (result === label) {
106-
outputLogger.show();
107-
}
108-
return result;
109+
fn: (message: string) => Promise<void>,
110+
{ fullMessage }: ShowAndLogOptions = {},
111+
): Promise<void> {
112+
void logger.log(fullMessage || message);
113+
await fn.bind(logger)(message);
109114
}

extensions/ql-vscode/src/common/vscode/selection-commands.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
TreeViewContextSingleSelectionCommandFunction,
55
} from "../commands";
66
import { showAndLogErrorMessage } from "./log";
7+
import { extLogger } from "../logging";
78

89
// A hack to match types that are not an array, which is useful to help avoid
910
// misusing createSingleSelectionCommand, e.g. where T accidentally gets instantiated
@@ -32,7 +33,10 @@ export function createSingleSelectionCommand<T extends NotArray>(
3233
if (multiSelect === undefined || multiSelect.length === 1) {
3334
return f(singleItem);
3435
} else {
35-
void showAndLogErrorMessage(`Please select a single ${itemName}.`);
36+
void showAndLogErrorMessage(
37+
extLogger,
38+
`Please select a single ${itemName}.`,
39+
);
3640
return;
3741
}
3842
};

extensions/ql-vscode/src/compare/compare-view.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
ToCompareViewMessage,
66
QueryCompareResult,
77
} from "../pure/interface-types";
8-
import { Logger } from "../common";
8+
import { extLogger, Logger } from "../common";
99
import { CodeQLCliServer } from "../codeql-cli/cli";
1010
import { DatabaseManager } from "../databases/local-databases";
1111
import { jumpToLocation } from "../databases/local-databases/locations";
@@ -152,6 +152,7 @@ export class CompareView extends AbstractWebview<
152152

153153
case "unhandledError":
154154
void showAndLogExceptionWithTelemetry(
155+
extLogger,
155156
redactableError(
156157
msg.error,
157158
)`Unhandled error in result comparison view: ${msg.error.message}`,

0 commit comments

Comments
 (0)