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