microsoft/vscode-react-native

Public

mirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
0.6.9

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/extension/log/OutputChannelLogger.ts

108lines · modecode

1// 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 {
14 public static MAIN_CHANNEL_NAME: string = "React Native";
15 private outputChannel: vscode.OutputChannel;
16
17 public static disposeChannel(channelName: string): void {
18 if (channels[channelName]) {
19 channels[channelName].getOutputChannel().dispose();
20 delete channels[channelName];
21 }
22 }
23
24 public static getMainChannel(): OutputChannelLogger {
25 return this.getChannel(this.MAIN_CHANNEL_NAME, true);
26 }
27
28 public static getChannel(channelName: string, lazy?: boolean, preserveFocus?: boolean): OutputChannelLogger {
29 if (!channels[channelName]) {
30 channels[channelName] = new OutputChannelLogger(channelName, lazy, preserveFocus);
31 }
32
33 return channels[channelName];
34 }
35
36 constructor(public readonly channelName: string, lazy: boolean = false, private preserveFocus: boolean = false) {
37 if (!lazy) {
38 this.channel = vscode.window.createOutputChannel(this.channelName);
39 this.channel.show(this.preserveFocus);
40 }
41 }
42
43 public log(message: string, level: LogLevel): void {
44 if (LogHelper.LOG_LEVEL === LogLevel.None) {
45 return;
46 }
47
48 if (level >= LogHelper.LOG_LEVEL) {
49 message = OutputChannelLogger.getFormattedMessage(message, level);
50 this.channel.appendLine(message);
51 }
52 }
53
54 public info(message: string): void {
55 this.log(message, LogLevel.Info);
56 }
57
58 public warning(message: string | Error, logStack = false): void {
59 this.log(message.toString(), LogLevel.Warning);
60 }
61
62 public error(errorMessage: string, error?: Error, logStack: boolean = true): void {
63 this.channel.appendLine(OutputChannelLogger.getFormattedMessage(errorMessage, LogLevel.Error));
64
65 // Print the error stack if necessary
66 if (logStack && error && (<Error>error).stack) {
67 this.channel.appendLine(`Stack: ${(<Error>error).stack}`);
68 }
69 }
70
71 public debug(message: string): void {
72 this.log(message, LogLevel.Debug);
73 }
74
75 public logStream(data: Buffer | string) {
76 this.channel.append(data.toString());
77 }
78
79 public setFocusOnLogChannel(): void {
80 this.channel.show(false);
81 }
82
83 protected static getFormattedMessage(message: string, level: LogLevel): string {
84 return `[${LogLevel[level]}] ${message}\n`;
85 }
86
87 public getOutputChannel(): vscode.OutputChannel {
88 return this.channel;
89 }
90
91 public clear(): void {
92 this.channel.clear();
93 }
94
95 private get channel(): vscode.OutputChannel {
96 if (this.outputChannel) {
97 return this.outputChannel;
98 } else {
99 this.outputChannel = vscode.window.createOutputChannel(this.channelName);
100 this.outputChannel.show(this.preserveFocus);
101 return this.outputChannel;
102 }
103 }
104
105 private set channel(channel: vscode.OutputChannel) {
106 this.outputChannel = channel;
107 }
108}
109