Skip to content

Commit d0c4d13

Browse files
author
Andy
authored
In tsserver, indent logged JSON (#19080)
1 parent 487504d commit d0c4d13

File tree

4 files changed

+21
-10
lines changed

4 files changed

+21
-10
lines changed

src/server/server.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -350,14 +350,14 @@ namespace ts.server {
350350
const request = createInstallTypingsRequest(project, typeAcquisition, unresolvedImports);
351351
if (this.logger.hasLevel(LogLevel.verbose)) {
352352
if (this.logger.hasLevel(LogLevel.verbose)) {
353-
this.logger.info(`Scheduling throttled operation: ${JSON.stringify(request)}`);
353+
this.logger.info(`Scheduling throttled operation:${stringifyIndented(request)}`);
354354
}
355355
}
356356

357357
const operationId = project.getProjectName();
358358
const operation = () => {
359359
if (this.logger.hasLevel(LogLevel.verbose)) {
360-
this.logger.info(`Sending request: ${JSON.stringify(request)}`);
360+
this.logger.info(`Sending request:${stringifyIndented(request)}`);
361361
}
362362
this.installer.send(request);
363363
};
@@ -377,7 +377,7 @@ namespace ts.server {
377377

378378
private handleMessage(response: SetTypings | InvalidateCachedTypings | BeginInstallTypes | EndInstallTypes | InitializationFailedResponse) {
379379
if (this.logger.hasLevel(LogLevel.verbose)) {
380-
this.logger.info(`Received response: ${JSON.stringify(response)}`);
380+
this.logger.info(`Received response:${stringifyIndented(response)}`);
381381
}
382382

383383
switch (response.kind) {
@@ -780,7 +780,7 @@ namespace ts.server {
780780
try {
781781
const args = [combinePaths(__dirname, "watchGuard.js"), path];
782782
if (logger.hasLevel(LogLevel.verbose)) {
783-
logger.info(`Starting ${process.execPath} with args ${JSON.stringify(args)}`);
783+
logger.info(`Starting ${process.execPath} with args:${stringifyIndented(args)}`);
784784
}
785785
childProcess.execFileSync(process.execPath, args, { stdio: "ignore", env: { "ELECTRON_RUN_AS_NODE": "1" } });
786786
status = true;

src/server/session.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ namespace ts.server {
131131

132132
const json = JSON.stringify(msg);
133133
if (verboseLogging) {
134-
logger.info(msg.type + ": " + json);
134+
logger.info(msg.type + ":\n" + indent(json));
135135
}
136136

137137
const len = byteLength(json, "utf8");
@@ -383,9 +383,9 @@ namespace ts.server {
383383
public logError(err: Error, cmd: string) {
384384
let msg = "Exception on executing command " + cmd;
385385
if (err.message) {
386-
msg += ":\n" + err.message;
386+
msg += ":\n" + indent(err.message);
387387
if ((<StackTraceError>err).stack) {
388-
msg += "\n" + (<StackTraceError>err).stack;
388+
msg += "\n" + indent((<StackTraceError>err).stack);
389389
}
390390
}
391391
this.logger.msg(msg, Msg.Err);
@@ -1962,7 +1962,7 @@ namespace ts.server {
19621962
return this.executeWithRequestId(request.seq, () => handler(request));
19631963
}
19641964
else {
1965-
this.logger.msg(`Unrecognized JSON command: ${JSON.stringify(request)}`, Msg.Err);
1965+
this.logger.msg(`Unrecognized JSON command:${stringifyIndented(request)}`, Msg.Err);
19661966
this.output(undefined, CommandNames.Unknown, request.seq, `Unrecognized JSON command: ${request.command}`);
19671967
return { responseRequired: false };
19681968
}
@@ -1974,7 +1974,7 @@ namespace ts.server {
19741974
if (this.logger.hasLevel(LogLevel.requestTime)) {
19751975
start = this.hrtime();
19761976
if (this.logger.hasLevel(LogLevel.verbose)) {
1977-
this.logger.info(`request: ${message}`);
1977+
this.logger.info(`request:${indent(message)}`);
19781978
}
19791979
}
19801980

src/server/typingsInstaller/nodeTypingsInstaller.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ namespace ts.server.typingsInstaller {
145145

146146
protected sendResponse(response: SetTypings | InvalidateCachedTypings | BeginInstallTypes | EndInstallTypes | InitializationFailedResponse) {
147147
if (this.log.isEnabled()) {
148-
this.log.writeLine(`Sending response: ${JSON.stringify(response)}`);
148+
this.log.writeLine(`Sending response:\n ${JSON.stringify(response)}`);
149149
}
150150
process.send(response);
151151
if (this.log.isEnabled()) {

src/server/utilities.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,4 +318,15 @@ namespace ts.server {
318318
deleted(oldItems[oldIndex++]);
319319
}
320320
}
321+
322+
/* @internal */
323+
export function indent(string: string): string {
324+
return "\n " + string;
325+
}
326+
327+
/** Put stringified JSON on the next line, indented. */
328+
/* @internal */
329+
export function stringifyIndented(json: {}): string {
330+
return "\n " + JSON.stringify(json);
331+
}
321332
}

0 commit comments

Comments
 (0)