Use lazy for creating output channel

This commit is contained in:
Matt Bierner
2018-06-07 16:55:12 -07:00
parent ed3e645112
commit 31a8fe2690

View File

@@ -3,7 +3,8 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { OutputChannel, window, workspace } from 'vscode';
import * as vscode from 'vscode';
import { lazy } from './util/lazy';
enum Trace {
Off,
@@ -31,7 +32,8 @@ function isString(value: any): value is string {
export class Logger {
private trace?: Trace;
private _output?: OutputChannel;
private readonly outputChannel = lazy(() => vscode.window.createOutputChannel('Markdown'));
constructor() {
this.updateConfiguration();
@@ -39,9 +41,9 @@ export class Logger {
public log(message: string, data?: any): void {
if (this.trace === Trace.Verbose) {
this.output.appendLine(`[Log - ${(new Date().toLocaleTimeString())}] ${message}`);
this.appendLine(`[Log - ${(new Date().toLocaleTimeString())}] ${message}`);
if (data) {
this.output.appendLine(Logger.data2String(data));
this.appendLine(Logger.data2String(data));
}
}
}
@@ -50,15 +52,12 @@ export class Logger {
this.trace = this.readTrace();
}
private get output(): OutputChannel {
if (!this._output) {
this._output = window.createOutputChannel('Markdown');
}
return this._output;
private appendLine(value: string) {
return this.outputChannel.value.appendLine(value);
}
private readTrace(): Trace {
return Trace.fromString(workspace.getConfiguration().get<string>('markdown.trace', 'off'));
return Trace.fromString(vscode.workspace.getConfiguration().get<string>('markdown.trace', 'off'));
}
private static data2String(data: any): string {
@@ -73,4 +72,4 @@ export class Logger {
}
return JSON.stringify(data, undefined, 2);
}
}
}