Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DevTools: adjustments to logging behaviour #244

Merged
merged 1 commit into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions src/debug.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { OutputChannel } from "vscode";
import { TraceValues } from "vscode-languageclient";
import { format } from "node:util";

/**
* for errors (and other logs in debug mode) we want to print
Expand All @@ -21,6 +23,21 @@ export class Debug {
this.outputConsole = outputConsole;
}

private static _traceLevel: Exclude<TraceValues, "compact"> = "off";
public static get traceLevel(): TraceValues {
return Debug._traceLevel;
}
public static set traceLevel(value: TraceValues | undefined) {
console.log("setting trace level to", value);
if (value === "compact") {
// we do not handle "compact" and it's not possible to set in settings, but it doesn't hurt to at least map
// it to another value
this._traceLevel = "messages";
} else {
this._traceLevel = value || "off";
}
}

/**
* Displays an info message prefixed with [INFO]
*/
Expand Down Expand Up @@ -55,6 +72,28 @@ export class Debug {
this.outputConsole.appendLine(`[WARN] ${message}`);
}

public static traceMessage(
short: string,
verbose = short,
...verboseParams: any[]
) {
if (!this.outputConsole) return;
if (Debug.traceLevel === "verbose") {
this.outputConsole.appendLine(
`[Trace] ${format(verbose, ...verboseParams)}`,
);
} else if (Debug.traceLevel === "messages") {
this.outputConsole.appendLine(`[Trace] ${short}`);
}
}

public static traceVerbose(message: string, ...params: any[]) {
if (!this.outputConsole) return;
if (Debug.traceLevel === "verbose") {
this.outputConsole.appendLine(`[Trace] ${format(message, ...params)}`);
}
}

/**
* TODO: enable error reporting and telemetry
*/
Expand Down
2 changes: 1 addition & 1 deletion src/devtools/DevToolsViewProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ export class DevToolsViewProvider implements vscode.WebviewViewProvider {
return vscode.postMessage(...args);
};

window.addEventListener("message", (event) => { console.debug(event); });
// window.addEventListener("message", (event) => { console.debug(event); });
</script>
<script nonce="${nonce}" src="${scriptUri}"></script>
<script nonce="${nonce}">
Expand Down
16 changes: 14 additions & 2 deletions src/devtools/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,22 @@ export const devtoolsEvents = new EventEmitter<{
fromDevTools: [unknown];
}>();
devtoolsEvents.addListener("toDevTools", (msg) => {
Debug.info("WS > DevTools: " + JSON.stringify(msg));
Debug.traceMessage(
`WS > DevTools: ${
msg && typeof msg === "object" && "type" in msg ? msg.type : "unknown"
}`,
"WS > DevTools: %o",
msg,
);
});
devtoolsEvents.addListener("fromDevTools", (msg) => {
Debug.info("DevTools > WS: " + JSON.stringify(msg));
Debug.traceMessage(
`DevTools > WS: ${
msg && typeof msg === "object" && "type" in msg ? msg.type : "unknown"
}`,
"DevTools > WS: %o",
msg,
);
});

let id = 1;
Expand Down
14 changes: 14 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,20 @@ export async function activate(
}),
);

Debug.traceLevel = workspace
.getConfiguration("apollographql")
.get("trace.server");
context.subscriptions.push(
workspace.onDidChangeConfiguration((event) => {
const affected = event.affectsConfiguration("apollographql.trace.server");
if (affected) {
Debug.traceLevel = workspace
.getConfiguration("apollographql")
.get("trace.server");
}
}),
);

await client.start();
return {
outputChannel,
Expand Down
Loading