microsoft/vscode-react-native
Publicmirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable
src/extension/outputChannelLogger.ts
49lines · 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 | |
| 12 | export class OutputChannelLogger implements ILogger { |
| 13 | private outputChannel: OutputChannel; |
| 14 | |
| 15 | constructor(outputChannel: OutputChannel) { |
| 16 | this.outputChannel = outputChannel; |
| 17 | this.outputChannel.show(); |
| 18 | } |
| 19 | |
| 20 | public logInternalMessage(logLevel: LogLevel, message: string) { |
| 21 | console.log(this.getFormattedInternalMessage(logLevel, message)); |
| 22 | } |
| 23 | |
| 24 | public logMessage(message: string, formatMessage: boolean = true ) { |
| 25 | this.outputChannel.appendLine(formatMessage ? |
| 26 | this.getFormattedMessage(message) : |
| 27 | message); |
| 28 | } |
| 29 | |
| 30 | public logError(errorMessage: string, error?: any, logStack: boolean = true) { |
| 31 | this.logMessage(errorMessage, /* formatMessage */ false); |
| 32 | } |
| 33 | |
| 34 | public logStreamData(data: Buffer, stream: NodeJS.WritableStream) { |
| 35 | this.outputChannel.append(data.toString()); |
| 36 | } |
| 37 | |
| 38 | public setFocusOnLogChannel() { |
| 39 | this.outputChannel.show(); |
| 40 | } |
| 41 | |
| 42 | private getFormattedMessage(message: string) { |
| 43 | return `######### ${message} ##########`; |
| 44 | } |
| 45 | |
| 46 | private getFormattedInternalMessage(logLevel: LogLevel, message: string) { |
| 47 | return (`${LogHelper.INTERNAL_TAG} [${LogLevel[logLevel]}] ${message}`); |
| 48 | } |
| 49 | } |