microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
1.8.0

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/extension/log/OutputChannelLogger.ts

191lines · 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";
08722261Yuri Skorokhodov7 years ago9import { ILogger, LogLevel, LogHelper, getLoggingDirectory } from "./LogHelper";
10import * as fs from "fs";
11import * as path from "path";
a324603aRedMickey6 years ago12import { getFormattedDatetimeString } from "../../common/utils";
08722261Yuri Skorokhodov7 years ago13
0a68f8dbArtem Egorov8 years ago14const channels: { [channelName: string]: OutputChannelLogger } = {};
15
16export class OutputChannelLogger implements ILogger {
17public static MAIN_CHANNEL_NAME: string = "React Native";
08722261Yuri Skorokhodov7 years ago18private readonly channelLogFilePath: string | undefined;
19private channelLogFileStream: fs.WriteStream;
0a68f8dbArtem Egorov8 years ago20private outputChannel: vscode.OutputChannel;
a324603aRedMickey6 years ago21private logTimestamps: boolean;
08722261Yuri Skorokhodov7 years ago22private static forbiddenFileNameSymbols: RegExp = /\W/gi;
0a68f8dbArtem Egorov8 years ago23
24public static disposeChannel(channelName: string): void {
25if (channels[channelName]) {
26channels[channelName].getOutputChannel().dispose();
08722261Yuri Skorokhodov7 years ago27if (channels[channelName].channelLogFileStream) {
28channels[channelName].channelLogFileStream.end();
29}
0a68f8dbArtem Egorov8 years ago30delete channels[channelName];
31}
32}
33
34public static getMainChannel(): OutputChannelLogger {
35return this.getChannel(this.MAIN_CHANNEL_NAME, true);
36}
37
34472878RedMickey5 years ago38public static getChannel(
39channelName: string,
40lazy?: boolean,
41preserveFocus?: boolean,
42logTimestamps?: boolean,
43): OutputChannelLogger {
0a68f8dbArtem Egorov8 years ago44if (!channels[channelName]) {
34472878RedMickey5 years ago45channels[channelName] = new OutputChannelLogger(
46channelName,
47lazy,
48preserveFocus,
49logTimestamps,
50);
0a68f8dbArtem Egorov8 years ago51}
52return channels[channelName];
53}
54
34472878RedMickey5 years ago55constructor(
56public readonly channelName: string,
57lazy: boolean = false,
58private preserveFocus: boolean = false,
59logTimestamps: boolean = false,
60) {
a324603aRedMickey6 years ago61this.logTimestamps = logTimestamps;
08722261Yuri Skorokhodov7 years ago62const channelLogFolder = getLoggingDirectory();
63if (channelLogFolder) {
64const filename = channelName.replace(OutputChannelLogger.forbiddenFileNameSymbols, "");
65this.channelLogFilePath = path.join(channelLogFolder, `${filename}.txt`);
66this.channelLogFileStream = fs.createWriteStream(this.channelLogFilePath);
67this.channelLogFileStream.on("error", err => {
34472878RedMickey5 years ago68this.error(
69`Error writing to log file at path: ${
70this.channelLogFilePath
71}. Error: ${err.toString()}\n`,
72);
08722261Yuri Skorokhodov7 years ago73});
74}
0a68f8dbArtem Egorov8 years ago75if (!lazy) {
76this.channel = vscode.window.createOutputChannel(this.channelName);
77this.channel.show(this.preserveFocus);
78}
79}
80
81public log(message: string, level: LogLevel): void {
82if (LogHelper.LOG_LEVEL === LogLevel.None) {
83return;
84}
85
86if (level >= LogHelper.LOG_LEVEL) {
34472878RedMickey5 years ago87message = OutputChannelLogger.getFormattedMessage(
88message,
89LogLevel[level],
90this.logTimestamps,
91);
a324603aRedMickey6 years ago92this.channel.appendLine(message);
93if (this.channelLogFileStream) {
94this.channelLogFileStream.write(message);
95}
96}
97}
98
99public logWithCustomTag(tag: string, message: string, level: LogLevel): void {
100if (LogHelper.LOG_LEVEL === LogLevel.None) {
101return;
102}
103
104if (level === LogLevel.Custom) {
105message = OutputChannelLogger.getFormattedMessage(message, tag, this.logTimestamps);
0a68f8dbArtem Egorov8 years ago106this.channel.appendLine(message);
08722261Yuri Skorokhodov7 years ago107if (this.channelLogFileStream) {
108this.channelLogFileStream.write(message);
109}
0a68f8dbArtem Egorov8 years ago110}
111}
112
113public info(message: string): void {
114this.log(message, LogLevel.Info);
115}
116
34472878RedMickey5 years ago117public warning(message: string | Error): void {
0a68f8dbArtem Egorov8 years ago118this.log(message.toString(), LogLevel.Warning);
119}
120
121public error(errorMessage: string, error?: Error, logStack: boolean = true): void {
34472878RedMickey5 years ago122const message = OutputChannelLogger.getFormattedMessage(
123errorMessage,
124LogLevel[LogLevel.Error],
125this.logTimestamps,
126);
08722261Yuri Skorokhodov7 years ago127this.channel.appendLine(message);
128if (this.channelLogFileStream) {
129this.channelLogFileStream.write(message);
130}
0a68f8dbArtem Egorov8 years ago131
132// Print the error stack if necessary
133if (logStack && error && (<Error>error).stack) {
134this.channel.appendLine(`Stack: ${(<Error>error).stack}`);
08722261Yuri Skorokhodov7 years ago135if (this.channelLogFileStream) {
136this.channelLogFileStream.write(`Stack: ${(<Error>error).stack}`);
137}
0a68f8dbArtem Egorov8 years ago138}
139}
140
141public debug(message: string): void {
142this.log(message, LogLevel.Debug);
143}
144
34472878RedMickey5 years ago145public logStream(data: Buffer | string): void {
0a68f8dbArtem Egorov8 years ago146this.channel.append(data.toString());
08722261Yuri Skorokhodov7 years ago147if (this.channelLogFileStream) {
148this.channelLogFileStream.write(data);
149}
0a68f8dbArtem Egorov8 years ago150}
151
152public setFocusOnLogChannel(): void {
153this.channel.show(false);
154}
155
34472878RedMickey5 years ago156protected static getFormattedMessage(
157message: string,
158tag: string,
159prependTimestamp: boolean = false,
160): string {
a324603aRedMickey6 years ago161let formattedMessage = `[${tag}] ${message}\n`;
162
163if (prependTimestamp) {
164formattedMessage = `[${getFormattedDatetimeString(new Date())}] ${formattedMessage}`;
165}
166
167return formattedMessage;
0a68f8dbArtem Egorov8 years ago168}
169
170public getOutputChannel(): vscode.OutputChannel {
171return this.channel;
172}
173
174public clear(): void {
175this.channel.clear();
176}
177
178private get channel(): vscode.OutputChannel {
179if (this.outputChannel) {
180return this.outputChannel;
181} else {
182this.outputChannel = vscode.window.createOutputChannel(this.channelName);
183this.outputChannel.show(this.preserveFocus);
184return this.outputChannel;
185}
186}
187
188private set channel(channel: vscode.OutputChannel) {
189this.outputChannel = channel;
190}
191}