microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
1.2.0

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/extension/log/OutputChannelLogger.ts

161lines · 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 ago14
15const channels: { [channelName: string]: OutputChannelLogger } = {};
16
17export class OutputChannelLogger implements ILogger {
18public static MAIN_CHANNEL_NAME: string = "React Native";
08722261Yuri Skorokhodov7 years ago19private readonly channelLogFilePath: string | undefined;
20private channelLogFileStream: fs.WriteStream;
0a68f8dbArtem Egorov8 years ago21private outputChannel: vscode.OutputChannel;
a324603aRedMickey6 years ago22private logTimestamps: boolean;
08722261Yuri Skorokhodov7 years ago23private static forbiddenFileNameSymbols: RegExp = /\W/gi;
0a68f8dbArtem Egorov8 years ago24
25public static disposeChannel(channelName: string): void {
26if (channels[channelName]) {
27channels[channelName].getOutputChannel().dispose();
08722261Yuri Skorokhodov7 years ago28if (channels[channelName].channelLogFileStream) {
29channels[channelName].channelLogFileStream.end();
30}
0a68f8dbArtem Egorov8 years ago31delete channels[channelName];
32}
33}
34
35public static getMainChannel(): OutputChannelLogger {
36return this.getChannel(this.MAIN_CHANNEL_NAME, true);
37}
38
a324603aRedMickey6 years ago39public static getChannel(channelName: string, lazy?: boolean, preserveFocus?: boolean, logTimestamps?: boolean): OutputChannelLogger {
0a68f8dbArtem Egorov8 years ago40if (!channels[channelName]) {
a324603aRedMickey6 years ago41channels[channelName] = new OutputChannelLogger(channelName, lazy, preserveFocus, logTimestamps);
0a68f8dbArtem Egorov8 years ago42}
43return channels[channelName];
44}
45
a324603aRedMickey6 years ago46constructor(public readonly channelName: string, lazy: boolean = false, private preserveFocus: boolean = false, logTimestamps: boolean = false) {
47this.logTimestamps = logTimestamps;
08722261Yuri Skorokhodov7 years ago48const channelLogFolder = getLoggingDirectory();
49if (channelLogFolder) {
50const filename = channelName.replace(OutputChannelLogger.forbiddenFileNameSymbols, "");
51this.channelLogFilePath = path.join(channelLogFolder, `${filename}.txt`);
52this.channelLogFileStream = fs.createWriteStream(this.channelLogFilePath);
53this.channelLogFileStream.on("error", err => {
54this.error(`Error writing to log file at path: ${this.channelLogFilePath}. Error: ${err.toString()}\n`);
55});
56}
0a68f8dbArtem Egorov8 years ago57if (!lazy) {
58this.channel = vscode.window.createOutputChannel(this.channelName);
59this.channel.show(this.preserveFocus);
60}
61}
62
63public log(message: string, level: LogLevel): void {
64if (LogHelper.LOG_LEVEL === LogLevel.None) {
65return;
66}
67
68if (level >= LogHelper.LOG_LEVEL) {
a324603aRedMickey6 years ago69message = OutputChannelLogger.getFormattedMessage(message, LogLevel[level], this.logTimestamps);
70this.channel.appendLine(message);
71if (this.channelLogFileStream) {
72this.channelLogFileStream.write(message);
73}
74}
75}
76
77public logWithCustomTag(tag: string, message: string, level: LogLevel): void {
78if (LogHelper.LOG_LEVEL === LogLevel.None) {
79return;
80}
81
82if (level === LogLevel.Custom) {
83message = OutputChannelLogger.getFormattedMessage(message, tag, this.logTimestamps);
0a68f8dbArtem Egorov8 years ago84this.channel.appendLine(message);
08722261Yuri Skorokhodov7 years ago85if (this.channelLogFileStream) {
86this.channelLogFileStream.write(message);
87}
0a68f8dbArtem Egorov8 years ago88}
89}
90
91public info(message: string): void {
92this.log(message, LogLevel.Info);
93}
94
95public warning(message: string | Error, logStack = false): void {
96this.log(message.toString(), LogLevel.Warning);
97}
98
99public error(errorMessage: string, error?: Error, logStack: boolean = true): void {
a324603aRedMickey6 years ago100const message = OutputChannelLogger.getFormattedMessage(errorMessage, LogLevel[LogLevel.Error], this.logTimestamps);
08722261Yuri Skorokhodov7 years ago101this.channel.appendLine(message);
102if (this.channelLogFileStream) {
103this.channelLogFileStream.write(message);
104}
0a68f8dbArtem Egorov8 years ago105
106// Print the error stack if necessary
107if (logStack && error && (<Error>error).stack) {
108this.channel.appendLine(`Stack: ${(<Error>error).stack}`);
08722261Yuri Skorokhodov7 years ago109if (this.channelLogFileStream) {
110this.channelLogFileStream.write(`Stack: ${(<Error>error).stack}`);
111}
0a68f8dbArtem Egorov8 years ago112}
113}
114
115public debug(message: string): void {
116this.log(message, LogLevel.Debug);
117}
118
119public logStream(data: Buffer | string) {
120this.channel.append(data.toString());
08722261Yuri Skorokhodov7 years ago121if (this.channelLogFileStream) {
122this.channelLogFileStream.write(data);
123}
0a68f8dbArtem Egorov8 years ago124}
125
126public setFocusOnLogChannel(): void {
127this.channel.show(false);
128}
129
a324603aRedMickey6 years ago130protected static getFormattedMessage(message: string, tag: string, prependTimestamp: boolean = false): string {
131let formattedMessage = `[${tag}] ${message}\n`;
132
133if (prependTimestamp) {
134formattedMessage = `[${getFormattedDatetimeString(new Date())}] ${formattedMessage}`;
135}
136
137return formattedMessage;
0a68f8dbArtem Egorov8 years ago138}
139
140public getOutputChannel(): vscode.OutputChannel {
141return this.channel;
142}
143
144public clear(): void {
145this.channel.clear();
146}
147
148private get channel(): vscode.OutputChannel {
149if (this.outputChannel) {
150return this.outputChannel;
151} else {
152this.outputChannel = vscode.window.createOutputChannel(this.channelName);
153this.outputChannel.show(this.preserveFocus);
154return this.outputChannel;
155}
156}
157
158private set channel(channel: vscode.OutputChannel) {
159this.outputChannel = channel;
160}
161}