microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
0.13.0

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/extension/log/OutputChannelLogger.ts

138lines · 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, getLoggingDirectory } from "./LogHelper";
10import * as fs from "fs";
11import * as path from "path";
12
13
14const channels: { [channelName: string]: OutputChannelLogger } = {};
15
16export class OutputChannelLogger implements ILogger {
17 public static MAIN_CHANNEL_NAME: string = "React Native";
18 private readonly channelLogFilePath: string | undefined;
19 private channelLogFileStream: fs.WriteStream;
20 private outputChannel: vscode.OutputChannel;
21 private static forbiddenFileNameSymbols: RegExp = /\W/gi;
22
23 public static disposeChannel(channelName: string): void {
24 if (channels[channelName]) {
25 channels[channelName].getOutputChannel().dispose();
26 if (channels[channelName].channelLogFileStream) {
27 channels[channelName].channelLogFileStream.end();
28 }
29 delete channels[channelName];
30 }
31 }
32
33 public static getMainChannel(): OutputChannelLogger {
34 return this.getChannel(this.MAIN_CHANNEL_NAME, true);
35 }
36
37 public static getChannel(channelName: string, lazy?: boolean, preserveFocus?: boolean): OutputChannelLogger {
38 if (!channels[channelName]) {
39 channels[channelName] = new OutputChannelLogger(channelName, lazy, preserveFocus);
40 }
41 return channels[channelName];
42 }
43
44 constructor(public readonly channelName: string, lazy: boolean = false, private preserveFocus: boolean = false) {
45 const channelLogFolder = getLoggingDirectory();
46 if (channelLogFolder) {
47 const filename = channelName.replace(OutputChannelLogger.forbiddenFileNameSymbols, "");
48 this.channelLogFilePath = path.join(channelLogFolder, `${filename}.txt`);
49 this.channelLogFileStream = fs.createWriteStream(this.channelLogFilePath);
50 this.channelLogFileStream.on("error", err => {
51 this.error(`Error writing to log file at path: ${this.channelLogFilePath}. Error: ${err.toString()}\n`);
52 });
53 }
54 if (!lazy) {
55 this.channel = vscode.window.createOutputChannel(this.channelName);
56 this.channel.show(this.preserveFocus);
57 }
58 }
59
60 public log(message: string, level: LogLevel): void {
61 if (LogHelper.LOG_LEVEL === LogLevel.None) {
62 return;
63 }
64
65 if (level >= LogHelper.LOG_LEVEL) {
66 message = OutputChannelLogger.getFormattedMessage(message, level);
67 this.channel.appendLine(message);
68 if (this.channelLogFileStream) {
69 this.channelLogFileStream.write(message);
70 }
71 }
72 }
73
74 public info(message: string): void {
75 this.log(message, LogLevel.Info);
76 }
77
78 public warning(message: string | Error, logStack = false): void {
79 this.log(message.toString(), LogLevel.Warning);
80 }
81
82 public error(errorMessage: string, error?: Error, logStack: boolean = true): void {
83 const message = OutputChannelLogger.getFormattedMessage(errorMessage, LogLevel.Error);
84 this.channel.appendLine(message);
85 if (this.channelLogFileStream) {
86 this.channelLogFileStream.write(message);
87 }
88
89 // Print the error stack if necessary
90 if (logStack && error && (<Error>error).stack) {
91 this.channel.appendLine(`Stack: ${(<Error>error).stack}`);
92 if (this.channelLogFileStream) {
93 this.channelLogFileStream.write(`Stack: ${(<Error>error).stack}`);
94 }
95 }
96 }
97
98 public debug(message: string): void {
99 this.log(message, LogLevel.Debug);
100 }
101
102 public logStream(data: Buffer | string) {
103 this.channel.append(data.toString());
104 if (this.channelLogFileStream) {
105 this.channelLogFileStream.write(data);
106 }
107 }
108
109 public setFocusOnLogChannel(): void {
110 this.channel.show(false);
111 }
112
113 protected static getFormattedMessage(message: string, level: LogLevel): string {
114 return `[${LogLevel[level]}] ${message}\n`;
115 }
116
117 public getOutputChannel(): vscode.OutputChannel {
118 return this.channel;
119 }
120
121 public clear(): void {
122 this.channel.clear();
123 }
124
125 private get channel(): vscode.OutputChannel {
126 if (this.outputChannel) {
127 return this.outputChannel;
128 } else {
129 this.outputChannel = vscode.window.createOutputChannel(this.channelName);
130 this.outputChannel.show(this.preserveFocus);
131 return this.outputChannel;
132 }
133 }
134
135 private set channel(channel: vscode.OutputChannel) {
136 this.outputChannel = channel;
137 }
138}
139