Skip to content

Introduce Traceformat to allow JSON format log #366

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

Merged
merged 6 commits into from
Aug 28, 2018
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
61 changes: 53 additions & 8 deletions client/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
RequestType, RequestType0, RequestHandler, RequestHandler0, GenericRequestHandler,
NotificationType, NotificationType0,
NotificationHandler, NotificationHandler0, GenericNotificationHandler,
MessageReader, MessageWriter, Trace, Tracer, Event, Emitter,
MessageReader, MessageWriter, Trace, Tracer, TraceFormat, TraceOptions, Event, Emitter,
createProtocolConnection,
ClientCapabilities, WorkspaceEdit,
RegistrationRequest, RegistrationParams, UnregistrationRequest, UnregistrationParams, TextDocumentRegistrationOptions,
Expand Down Expand Up @@ -101,6 +101,7 @@ interface IConnection {
onNotification(method: string | RPCMessageType, handler: GenericNotificationHandler): void;

trace(value: Trace, tracer: Tracer, sendNotification?: boolean): void;
trace(value: Trace, tracer: Tracer, traceOptions?: TraceOptions): void;

initialize(params: InitializeParams): Thenable<InitializeResult>;
shutdown(): Thenable<void>;
Expand Down Expand Up @@ -161,7 +162,20 @@ function createConnection(input: any, output: any, errorHandler: ConnectionError
sendNotification: (type: string | RPCMessageType, params?: any): void => connection.sendNotification(Is.string(type) ? type : type.method, params),
onNotification: (type: string | RPCMessageType, handler: GenericNotificationHandler): void => connection.onNotification(Is.string(type) ? type : type.method, handler),

trace: (value: Trace, tracer: Tracer, sendNotification: boolean = false): void => connection.trace(value, tracer, sendNotification),
trace: (value: Trace, tracer: Tracer, sendNotificationOrTraceOptions?: boolean | TraceOptions): void => {
const defaultTraceOptions: TraceOptions = {
sendNotification: false,
traceFormat: TraceFormat.Text
};

if (sendNotificationOrTraceOptions === void 0) {
connection.trace(value, tracer, defaultTraceOptions);
} else if (Is.boolean(sendNotificationOrTraceOptions)) {
connection.trace(value, tracer, sendNotificationOrTraceOptions);
} else {
connection.trace(value, tracer, sendNotificationOrTraceOptions);
}
},

initialize: (params: InitializeParams) => connection.sendRequest(InitializeRequest.type, params),
shutdown: () => connection.sendRequest(ShutdownRequest.type, undefined),
Expand Down Expand Up @@ -2249,6 +2263,7 @@ export abstract class BaseLanguageClient {
private _stateChangeEmitter: Emitter<StateChangeEvent>;

private _trace: Trace;
private _traceFormat: TraceFormat = TraceFormat.Text;
private _tracer: Tracer;

private _c2p: c2p.Converter;
Expand Down Expand Up @@ -2300,9 +2315,13 @@ export abstract class BaseLanguageClient {
this._telemetryEmitter = new Emitter<any>();
this._stateChangeEmitter = new Emitter<StateChangeEvent>();
this._tracer = {
log: (message: string, data?: string) => {
this.logTrace(message, data);
}
log: (messageOrDataObject: string | any, data?: string) => {
if (Is.string(messageOrDataObject)) {
this.logTrace(messageOrDataObject, data);
} else {
this.logObjectTrace(messageOrDataObject);
}
},
};
this._c2p = c2p.createConverter(clientOptions.uriConverters ? clientOptions.uriConverters.code2Protocol : undefined);
this._p2c = p2c.createConverter(clientOptions.uriConverters ? clientOptions.uriConverters.protocol2Code : undefined);
Expand Down Expand Up @@ -2438,7 +2457,10 @@ export abstract class BaseLanguageClient {
this._trace = value;
this.onReady().then(() => {
this.resolveConnection().then((connection) => {
connection.trace(value, this._tracer);
connection.trace(this._trace, this._tracer, {
sendNotification: false,
traceFormat: this._traceFormat
});
})
}, () => {
});
Expand Down Expand Up @@ -2498,6 +2520,17 @@ export abstract class BaseLanguageClient {
}
}

private logObjectTrace(data: any): void {
if (data.isLSPMessage && data.type) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dbaeumer Not sure if this is what you meant by "if the data is an object then log it by JSON stringify here". Also not sure if that isLSPMessage is the best way to go. If you have better suggestions, let me know!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, in this case I would simply do

this.outputChannel.append(`[LSP   - ${(new Date().toLocaleTimeString())}] `);
this.outputChannel.appendLine(`${JSON.stringify(data)}`);

this.outputChannel.append(`[LSP - ${(new Date().toLocaleTimeString())}] `);
} else {
this.outputChannel.append(`[Trace - ${(new Date().toLocaleTimeString())}] `);
}
if (data) {
this.outputChannel.appendLine(`${JSON.stringify(data)}`);
}
}

public needsStart(): boolean {
return this.state === ClientState.Initial || this.state === ClientState.Stopping || this.state === ClientState.Stopped;
}
Expand Down Expand Up @@ -2846,11 +2879,23 @@ export abstract class BaseLanguageClient {
private refreshTrace(connection: IConnection, sendNotification: boolean = false): void {
let config = Workspace.getConfiguration(this._id);
let trace: Trace = Trace.Off;
let traceFormat: TraceFormat = TraceFormat.Text;
if (config) {
trace = Trace.fromString(config.get('trace.server', 'off'));
const traceConfig = config.get('trace.server', 'off');

if (typeof traceConfig === 'string') {
trace = Trace.fromString(traceConfig);
} else {
trace = Trace.fromString(config.get('trace.server.verbosity', 'off'));
traceFormat = TraceFormat.fromString(config.get('trace.server.format', 'text'));
}
}
this._trace = trace;
connection.trace(this._trace, this._tracer, sendNotification);
this._traceFormat = traceFormat;
connection.trace(this._trace, this._tracer, {
sendNotification,
traceFormat: this._traceFormat
});
}


Expand Down
Loading