forked from konveyor/editor-extensions
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🌱 Add ability to trace the jsonrpc MessageConnection (konveyor#319)
To make it easier to work between the jsonrpc server and the extension client, add the ability to trace the messaging to an output channel. Details: - The output channel's name is of the form: `${basename(kai-rpc-server)} message trace` - The trace output channel is created when the server is first started, and is independent from the `Konveyor-Analyzer` channel - Tracing is controlled by the configuration key `konveyor.logging.traceMessageConnection`. It is `false` by default. Note: Supports konveyor#315 Signed-off-by: Scott J Dickerson <[email protected]>
- Loading branch information
Showing
4 changed files
with
93 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// Modified from https://github.com/microsoft/vscode-languageserver-node/blob/main/client/src/common/client.ts | ||
|
||
import * as vscode from "vscode"; | ||
import { Logger, ResponseError, Tracer } from "vscode-jsonrpc/node"; | ||
|
||
function isString(value: any): value is string { | ||
return typeof value === "string" || value instanceof String; | ||
} | ||
|
||
function data2String(data: object): string { | ||
if (data instanceof ResponseError) { | ||
const responseError = data as ResponseError<any>; | ||
return ` Message: ${responseError.message}\n Code: ${responseError.code} ${responseError.data ? "\n" + responseError.data.toString() : ""}`; | ||
} | ||
if (data instanceof Error) { | ||
if (isString(data.stack)) { | ||
return data.stack; | ||
} | ||
return (data as Error).message; | ||
} | ||
if (isString(data)) { | ||
return data; | ||
} | ||
return data.toString(); | ||
} | ||
|
||
const tracerToLogOutputChannel = (channel: vscode.LogOutputChannel): Tracer => { | ||
const logTrace = (message: string, data?: any) => { | ||
channel.info(message); | ||
if (data) { | ||
channel.info(data2String(data)); | ||
} | ||
}; | ||
|
||
const logObjectTrace = (data: any) => { | ||
if (data) { | ||
channel.info(JSON.stringify(data, null, 2)); | ||
} | ||
}; | ||
|
||
const log = (messageOrDataObject: string | any, data?: string) => { | ||
if (isString(messageOrDataObject)) { | ||
logTrace(messageOrDataObject, data); | ||
} else { | ||
logObjectTrace(messageOrDataObject); | ||
} | ||
}; | ||
|
||
return { log }; | ||
}; | ||
|
||
/** Enable a `MessageConnection` to trace comms to a `LogOutputChannel`. */ | ||
export const tracer = (channelName: string): Tracer => { | ||
const traceLogChannel = vscode.window.createOutputChannel(channelName, { | ||
log: true, | ||
}); | ||
return tracerToLogOutputChannel(traceLogChannel); | ||
}; | ||
|
||
export const logger = (channel: vscode.LogOutputChannel): Logger => ({ | ||
error: (message: string) => channel.error(message), | ||
warn: (message: string) => channel.warn(message), | ||
info: (message: string) => channel.info(message), | ||
log: (message: string) => channel.debug(message), | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters