microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
0.10.2

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/extension/log/OutputChannelLogger.ts

138lines · 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";
12
0a68f8dbArtem Egorov8 years ago13
14const 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;
08722261Yuri Skorokhodov7 years ago21private static forbiddenFileNameSymbols: RegExp = /\W/gi;
0a68f8dbArtem Egorov8 years ago22
23public static disposeChannel(channelName: string): void {
24if (channels[channelName]) {
25channels[channelName].getOutputChannel().dispose();
08722261Yuri Skorokhodov7 years ago26if (channels[channelName].channelLogFileStream) {
27channels[channelName].channelLogFileStream.end();
28}
0a68f8dbArtem Egorov8 years ago29delete channels[channelName];
30}
31}
32
33public static getMainChannel(): OutputChannelLogger {
34return this.getChannel(this.MAIN_CHANNEL_NAME, true);
35}
36
37public static getChannel(channelName: string, lazy?: boolean, preserveFocus?: boolean): OutputChannelLogger {
38if (!channels[channelName]) {
39channels[channelName] = new OutputChannelLogger(channelName, lazy, preserveFocus);
40}
41return channels[channelName];
42}
43
44constructor(public readonly channelName: string, lazy: boolean = false, private preserveFocus: boolean = false) {
08722261Yuri Skorokhodov7 years ago45const channelLogFolder = getLoggingDirectory();
46if (channelLogFolder) {
47const filename = channelName.replace(OutputChannelLogger.forbiddenFileNameSymbols, "");
48this.channelLogFilePath = path.join(channelLogFolder, `${filename}.txt`);
49this.channelLogFileStream = fs.createWriteStream(this.channelLogFilePath);
50this.channelLogFileStream.on("error", err => {
51this.error(`Error writing to log file at path: ${this.channelLogFilePath}. Error: ${err.toString()}\n`);
52});
53}
0a68f8dbArtem Egorov8 years ago54if (!lazy) {
55this.channel = vscode.window.createOutputChannel(this.channelName);
56this.channel.show(this.preserveFocus);
57}
58}
59
60public log(message: string, level: LogLevel): void {
61if (LogHelper.LOG_LEVEL === LogLevel.None) {
62return;
63}
64
65if (level >= LogHelper.LOG_LEVEL) {
66message = OutputChannelLogger.getFormattedMessage(message, level);
67this.channel.appendLine(message);
08722261Yuri Skorokhodov7 years ago68if (this.channelLogFileStream) {
69this.channelLogFileStream.write(message);
70}
0a68f8dbArtem Egorov8 years ago71}
72}
73
74public info(message: string): void {
75this.log(message, LogLevel.Info);
76}
77
78public warning(message: string | Error, logStack = false): void {
79this.log(message.toString(), LogLevel.Warning);
80}
81
82public error(errorMessage: string, error?: Error, logStack: boolean = true): void {
08722261Yuri Skorokhodov7 years ago83const message = OutputChannelLogger.getFormattedMessage(errorMessage, LogLevel.Error);
84this.channel.appendLine(message);
85if (this.channelLogFileStream) {
86this.channelLogFileStream.write(message);
87}
0a68f8dbArtem Egorov8 years ago88
89// Print the error stack if necessary
90if (logStack && error && (<Error>error).stack) {
91this.channel.appendLine(`Stack: ${(<Error>error).stack}`);
08722261Yuri Skorokhodov7 years ago92if (this.channelLogFileStream) {
93this.channelLogFileStream.write(`Stack: ${(<Error>error).stack}`);
94}
0a68f8dbArtem Egorov8 years ago95}
96}
97
98public debug(message: string): void {
99this.log(message, LogLevel.Debug);
100}
101
102public logStream(data: Buffer | string) {
103this.channel.append(data.toString());
08722261Yuri Skorokhodov7 years ago104if (this.channelLogFileStream) {
105this.channelLogFileStream.write(data);
106}
0a68f8dbArtem Egorov8 years ago107}
108
109public setFocusOnLogChannel(): void {
110this.channel.show(false);
111}
112
113protected static getFormattedMessage(message: string, level: LogLevel): string {
114return `[${LogLevel[level]}] ${message}\n`;
115}
116
117public getOutputChannel(): vscode.OutputChannel {
118return this.channel;
119}
120
121public clear(): void {
122this.channel.clear();
123}
124
125private get channel(): vscode.OutputChannel {
126if (this.outputChannel) {
127return this.outputChannel;
128} else {
129this.outputChannel = vscode.window.createOutputChannel(this.channelName);
130this.outputChannel.show(this.preserveFocus);
131return this.outputChannel;
132}
133}
134
135private set channel(channel: vscode.OutputChannel) {
136this.outputChannel = channel;
137}
138}