microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/extension/outputChannelLogger.ts
83lines · 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 | |
| 8 | import {ILogger} from "../common/log/loggers"; |
| 9 | import {LogHelper, LogLevel} from "../common/log/logHelper"; |
| 10 | import {OutputChannel} from "vscode"; |
| 11 | import * as vscode from "vscode"; |
| 12 | |
| 13 | export 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 setFocusOnLogChannel() { |
| 35 | this.logger.setFocusOnLogChannel(); |
| 36 | } |
| 37 | |
| 38 | private get logger(): OutputChannelLogger { |
| 39 | if (!this.outputChannelLogger) { |
| 40 | this.outputChannelLogger = new OutputChannelLogger(vscode.window.createOutputChannel(this.channelName)); |
| 41 | } |
| 42 | return this.outputChannelLogger; |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | export class OutputChannelLogger implements ILogger { |
| 47 | private outputChannel: OutputChannel; |
| 48 | |
| 49 | constructor(outputChannel: OutputChannel) { |
| 50 | this.outputChannel = outputChannel; |
| 51 | this.outputChannel.show(); |
| 52 | } |
| 53 | |
| 54 | public logInternalMessage(logLevel: LogLevel, message: string) { |
| 55 | console.log(this.getFormattedInternalMessage(logLevel, message)); |
| 56 | } |
| 57 | |
| 58 | public logMessage(message: string, formatMessage: boolean = true ) { |
| 59 | this.outputChannel.appendLine(formatMessage ? |
| 60 | this.getFormattedMessage(message) : |
| 61 | message); |
| 62 | } |
| 63 | |
| 64 | public logError(errorMessage: string, error?: any, logStack: boolean = true) { |
| 65 | this.logMessage(errorMessage, /* formatMessage */ false); |
| 66 | } |
| 67 | |
| 68 | public logStreamData(data: Buffer, stream: NodeJS.WritableStream) { |
| 69 | this.outputChannel.append(data.toString()); |
| 70 | } |
| 71 | |
| 72 | public setFocusOnLogChannel() { |
| 73 | this.outputChannel.show(); |
| 74 | } |
| 75 | |
| 76 | private getFormattedMessage(message: string) { |
| 77 | return `######### ${message} ##########`; |
| 78 | } |
| 79 | |
| 80 | private getFormattedInternalMessage(logLevel: LogLevel, message: string) { |
| 81 | return (`${LogHelper.INTERNAL_TAG} [${LogLevel[logLevel]}] ${message}`); |
| 82 | } |
| 83 | } |