microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
3c172a056576b34ff7bb5d777cbd9b47ff97cd7f

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/extension/outputChannelLogger.ts

100lines · 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 { ILogger } from "../common/log/loggers";
9import { LogHelper, LogLevel } from "../common/log/logHelper";
10import { SettingsHelper } from "./settingsHelper";
11import { OutputChannel } from "vscode";
12import * as vscode from "vscode";
13
14export class OutputChannelLogger implements ILogger {
15 private outputChannel: OutputChannel;
16
17 constructor(outputChannel: OutputChannel) {
18 this.outputChannel = outputChannel;
19 this.outputChannel.show();
20 }
21
22 public logInternalMessage(logLevel: LogLevel, message: string) {
23 if (SettingsHelper.getShowInternalLogs()) {
24 this.logMessage(this.getFormattedInternalMessage(logLevel, message));
25 return;
26 }
27 console.log(this.getFormattedInternalMessage(logLevel, message));
28 }
29
30 public logMessage(message: string, formatMessage: boolean = true) {
31 this.outputChannel.appendLine(formatMessage ?
32 this.getFormattedMessage(message) :
33 message);
34 }
35
36 public logError(errorMessage: string, error?: any, logStack: boolean = true) {
37 this.logMessage(errorMessage, /* formatMessage */ false);
38 }
39
40 public logStreamData(data: Buffer, stream: NodeJS.WritableStream) {
41 this.outputChannel.append(data.toString());
42 }
43
44 public logString(data: string) {
45 this.outputChannel.append(data);
46 }
47
48 public setFocusOnLogChannel() {
49 this.outputChannel.show();
50 }
51
52 public getOutputChannel(): OutputChannel {
53 return this.outputChannel;
54 }
55
56 private getFormattedMessage(message: string) {
57 return `######### ${message} ##########`;
58 }
59
60 private getFormattedInternalMessage(logLevel: LogLevel, message: string) {
61 return (`${LogHelper.INTERNAL_TAG} [${LogLevel[logLevel]}] ${message}`);
62 }
63}
64
65export class DelayedOutputChannelLogger implements ILogger {
66 private outputChannelLogger: OutputChannelLogger;
67
68 constructor(private channelName: string) { }
69
70 public logInternalMessage(logLevel: LogLevel, message: string) {
71 this.logger.logInternalMessage(logLevel, message);
72 }
73
74 public logMessage(message: string, formatMessage: boolean = true) {
75 this.logger.logMessage(message, formatMessage);
76 }
77
78 public logError(errorMessage: string, error?: any, logStack: boolean = true) {
79 this.logger.logError(errorMessage, error, logStack);
80 }
81
82 public logStreamData(data: Buffer, stream: NodeJS.WritableStream) {
83 this.logger.logStreamData(data, stream);
84 }
85
86 public logString(data: string) {
87 this.logger.logString(data);
88 }
89
90 public setFocusOnLogChannel() {
91 this.logger.setFocusOnLogChannel();
92 }
93
94 private get logger(): OutputChannelLogger {
95 if (!this.outputChannelLogger) {
96 this.outputChannelLogger = new OutputChannelLogger(vscode.window.createOutputChannel(this.channelName));
97 }
98 return this.outputChannelLogger;
99 }
100}
101