microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
274b561c0cbc309bc164cdaebfb6d63d30b22e60

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/extension/outputChannelLogger.ts

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