Skip to content

Commit 99f55b2

Browse files
committed
Extract TypeScript logger to own file
1 parent c1eccca commit 99f55b2

2 files changed

Lines changed: 69 additions & 47 deletions

File tree

extensions/typescript/src/typescriptServiceClient.ts

Lines changed: 7 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,11 @@ import * as os from 'os';
1313
import * as electron from './utils/electron';
1414
import { Reader } from './utils/wireProtocol';
1515

16-
import { workspace, window, Uri, CancellationToken, Disposable, OutputChannel, Memento, MessageItem, QuickPickItem, EventEmitter, Event, commands, WorkspaceConfiguration } from 'vscode';
16+
import { workspace, window, Uri, CancellationToken, Disposable, Memento, MessageItem, QuickPickItem, EventEmitter, Event, commands, WorkspaceConfiguration } from 'vscode';
1717
import * as Proto from './protocol';
1818
import { ITypescriptServiceClient, ITypescriptServiceClientHost, API } from './typescriptService';
1919
import { TypeScriptServerPlugin } from './utils/plugins';
20+
import Logger from './utils/logger';
2021

2122
import * as VersionStatus from './utils/versionStatus';
2223
import * as is from './utils/is';
@@ -141,7 +142,7 @@ export default class TypeScriptServiceClient implements ITypescriptServiceClient
141142
private _checkGlobalTSCVersion: boolean;
142143
private _experimentalAutoBuild: boolean;
143144
private trace: Trace;
144-
private _output: OutputChannel;
145+
private readonly logger: Logger = new Logger();
145146
private tsServerLogFile: string | null = null;
146147
private tsServerLogLevel: TsServerLogLevel = TsServerLogLevel.Off;
147148
private servicePromise: Thenable<cp.ChildProcess> | null;
@@ -291,13 +292,6 @@ export default class TypeScriptServiceClient implements ITypescriptServiceClient
291292
return this._onTypesInstallerInitializationFailed.event;
292293
}
293294

294-
private get output(): OutputChannel {
295-
if (!this._output) {
296-
this._output = window.createOutputChannel(localize('channelName', 'TypeScript'));
297-
}
298-
return this._output;
299-
}
300-
301295
private readTrace(): Trace {
302296
let result: Trace = Trace.fromString(workspace.getConfiguration().get<string>('typescript.tsserver.trace', 'off'));
303297
if (result === Trace.Off && !!process.env.TSS_TRACE) {
@@ -331,54 +325,20 @@ export default class TypeScriptServiceClient implements ITypescriptServiceClient
331325
return this._onReady.promise;
332326
}
333327

334-
private data2String(data: any): string {
335-
if (data instanceof Error) {
336-
if (is.string(data.stack)) {
337-
return data.stack;
338-
}
339-
return (data as Error).message;
340-
}
341-
if (is.boolean(data.success) && !data.success && is.string(data.message)) {
342-
return data.message;
343-
}
344-
if (is.string(data)) {
345-
return data;
346-
}
347-
return data.toString();
348-
}
349-
350328
public info(message: string, data?: any): void {
351-
this.output.appendLine(`[Info - ${(new Date().toLocaleTimeString())}] ${message}`);
352-
if (data) {
353-
this.output.appendLine(this.data2String(data));
354-
}
329+
this.logger.info(message, data);
355330
}
356331

357332
public warn(message: string, data?: any): void {
358-
this.output.appendLine(`[Warn - ${(new Date().toLocaleTimeString())}] ${message}`);
359-
if (data) {
360-
this.output.appendLine(this.data2String(data));
361-
}
333+
this.logger.warn(message, data);
362334
}
363335

364336
public error(message: string, data?: any): void {
365-
// See https://github.com/Microsoft/TypeScript/issues/10496
366-
if (data && data.message === 'No content available.') {
367-
return;
368-
}
369-
this.output.appendLine(`[Error - ${(new Date().toLocaleTimeString())}] ${message}`);
370-
if (data) {
371-
this.output.appendLine(this.data2String(data));
372-
}
373-
// this.output.show(true);
337+
this.logger.error(message, data);
374338
}
375339

376340
private logTrace(message: string, data?: any): void {
377-
this.output.appendLine(`[Trace - ${(new Date().toLocaleTimeString())}] ${message}`);
378-
if (data) {
379-
this.output.appendLine(this.data2String(data));
380-
}
381-
// this.output.show(true);
341+
this.logger.logLevel('Trace', message, data);
382342
}
383343

384344
private get packageInfo(): IPackageInfo | null {
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*---------------------------------------------------------------------------------------------
2+
* Copyright (c) Microsoft Corporation. All rights reserved.
3+
* Licensed under the MIT License. See License.txt in the project root for license information.
4+
*--------------------------------------------------------------------------------------------*/
5+
6+
'use strict';
7+
8+
import { OutputChannel, window } from "vscode";
9+
import * as is from './is';
10+
11+
import * as nls from 'vscode-nls';
12+
const localize = nls.loadMessageBundle();
13+
14+
export default class Logger {
15+
private _output: OutputChannel;
16+
17+
private get output(): OutputChannel {
18+
if (!this._output) {
19+
this._output = window.createOutputChannel(localize('channelName', 'TypeScript'));
20+
}
21+
return this._output;
22+
}
23+
24+
private data2String(data: any): string {
25+
if (data instanceof Error) {
26+
if (is.string(data.stack)) {
27+
return data.stack;
28+
}
29+
return (data as Error).message;
30+
}
31+
if (is.boolean(data.success) && !data.success && is.string(data.message)) {
32+
return data.message;
33+
}
34+
if (is.string(data)) {
35+
return data;
36+
}
37+
return data.toString();
38+
}
39+
40+
public info(message: string, data?: any): void {
41+
this.logLevel('Info', message, data);
42+
}
43+
44+
public warn(message: string, data?: any): void {
45+
this.logLevel('Warn', message, data);
46+
}
47+
48+
public error(message: string, data?: any): void {
49+
// See https://github.com/Microsoft/TypeScript/issues/10496
50+
if (data && data.message === 'No content available.') {
51+
return;
52+
}
53+
this.logLevel('Error', message, data);
54+
}
55+
56+
public logLevel(level: string, message: string, data?: any): void {
57+
this.output.appendLine(`[${level} - ${(new Date().toLocaleTimeString())}] ${message}`);
58+
if (data) {
59+
this.output.appendLine(this.data2String(data));
60+
}
61+
}
62+
}

0 commit comments

Comments
 (0)