microsoft/vscode-react-native

Public

mirrored from https://github.com/microsoft/vscode-react-nativeAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
0.6.17

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

src/extension/log/OutputChannelLogger.ts

108lines · modeblame

0a68f8dbArtem Egorov8 years ago1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the MIT license. See LICENSE file in the project root for details.
3
4/**
5* Formatter for the Output channel.
6*/
7
8import * as vscode from "vscode";
9import { ILogger, LogLevel, LogHelper } from "./LogHelper";
10
11const channels: { [channelName: string]: OutputChannelLogger } = {};
12
13export class OutputChannelLogger implements ILogger {
14public static MAIN_CHANNEL_NAME: string = "React Native";
15private outputChannel: vscode.OutputChannel;
16
17public static disposeChannel(channelName: string): void {
18if (channels[channelName]) {
19channels[channelName].getOutputChannel().dispose();
20delete channels[channelName];
21}
22}
23
24public static getMainChannel(): OutputChannelLogger {
25return this.getChannel(this.MAIN_CHANNEL_NAME, true);
26}
27
28public static getChannel(channelName: string, lazy?: boolean, preserveFocus?: boolean): OutputChannelLogger {
29if (!channels[channelName]) {
30channels[channelName] = new OutputChannelLogger(channelName, lazy, preserveFocus);
31}
32
33return channels[channelName];
34}
35
36constructor(public readonly channelName: string, lazy: boolean = false, private preserveFocus: boolean = false) {
37if (!lazy) {
38this.channel = vscode.window.createOutputChannel(this.channelName);
39this.channel.show(this.preserveFocus);
40}
41}
42
43public log(message: string, level: LogLevel): void {
44if (LogHelper.LOG_LEVEL === LogLevel.None) {
45return;
46}
47
48if (level >= LogHelper.LOG_LEVEL) {
49message = OutputChannelLogger.getFormattedMessage(message, level);
50this.channel.appendLine(message);
51}
52}
53
54public info(message: string): void {
55this.log(message, LogLevel.Info);
56}
57
58public warning(message: string | Error, logStack = false): void {
59this.log(message.toString(), LogLevel.Warning);
60}
61
62public error(errorMessage: string, error?: Error, logStack: boolean = true): void {
63this.channel.appendLine(OutputChannelLogger.getFormattedMessage(errorMessage, LogLevel.Error));
64
65// Print the error stack if necessary
66if (logStack && error && (<Error>error).stack) {
67this.channel.appendLine(`Stack: ${(<Error>error).stack}`);
68}
69}
70
71public debug(message: string): void {
72this.log(message, LogLevel.Debug);
73}
74
75public logStream(data: Buffer | string) {
76this.channel.append(data.toString());
77}
78
79public setFocusOnLogChannel(): void {
80this.channel.show(false);
81}
82
83protected static getFormattedMessage(message: string, level: LogLevel): string {
84return `[${LogLevel[level]}] ${message}\n`;
85}
86
87public getOutputChannel(): vscode.OutputChannel {
88return this.channel;
89}
90
91public clear(): void {
92this.channel.clear();
93}
94
95private get channel(): vscode.OutputChannel {
96if (this.outputChannel) {
97return this.outputChannel;
98} else {
99this.outputChannel = vscode.window.createOutputChannel(this.channelName);
100this.outputChannel.show(this.preserveFocus);
101return this.outputChannel;
102}
103}
104
105private set channel(channel: vscode.OutputChannel) {
106this.outputChannel = channel;
107}
108}