microsoft/vscode-react-native

Public

mirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
0.1.3

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/common/log/loggers.ts

94lines · modepreview

// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for details.

/**
 * Formatter for the Output channel.
 */
import {LogHelper, LogLevel} from "./logHelper";

export interface ILogger {
    logMessage: (message: string, formatMessage?: boolean) => void;
    logError: (errorMessage: string, error?: any, logStack?: boolean) => void;
    logStreamData: (data: Buffer, stream: NodeJS.WritableStream) => void;
    logInternalMessage: (logLevel: LogLevel, message: string) => void;
    setFocusOnLogChannel: () => void;
}

export class ConsoleLogger implements ILogger {
    public logMessage(message: string, formatMessage: boolean = true ) {
        console.log(formatMessage ?
            this.getFormattedMessage(message) :
            message);
    }

    public logError(errorMessage: string, error?: any, logStack: boolean = true) {
        console.error(this.getFormattedMessage(errorMessage));

        // Print the error stack if necessary
        if (logStack && error && (<Error>error).stack) {
            console.error(`Stack: ${(<Error>error).stack}`);
        }
    }

    public logStreamData(data: Buffer, stream: NodeJS.WritableStream) {
        stream.write(data.toString());
    }

    public logInternalMessage(logLevel: LogLevel, message: string) {
        this.logMessage(this.getFormattedInternalMessage(logLevel, message), /* formatMessage */ false);
    }

    public setFocusOnLogChannel() {
        // Do nothing - console takes focus automatically upon logging
        return;
    }

    private getFormattedMessage(message: string) {
        return `${LogHelper.MESSAGE_TAG} ${message}\n`;
    }

    private getFormattedInternalMessage(logLevel: LogLevel, message: string) {
        return (`${LogHelper.INTERNAL_TAG} [${LogLevel[logLevel]}] ${message}\n`);
    }
}

export class StreamLogger implements ILogger {
    private stream: NodeJS.WritableStream;
    constructor(stream: NodeJS.WritableStream) {
        this.stream = stream;
    }
    public logMessage(message: string, formatMessage: boolean = true ) {
        this.stream.write(formatMessage ?
            this.getFormattedMessage(message) :
            message);
    }

    public logError(errorMessage: string, error?: any, logStack: boolean = true) {
        this.logMessage(errorMessage);

        if (logStack && error && (<Error>error).stack) {
            this.logMessage(`Stack: ${(<Error>error).stack}`, /* formatMessage */ false);
        }
    }

    public logStreamData(data: Buffer, stream: NodeJS.WritableStream) {
        stream.write(data.toString());
    }

    public logInternalMessage(logLevel: LogLevel, message: string) {
        this.logMessage(this.getFormattedInternalMessage(logLevel, message), /* formatMessage */ false);
    }

    public getFormattedMessage(message: string) {
        return `${LogHelper.MESSAGE_TAG} ${message}\n`;
    }

    public getFormattedInternalMessage(logLevel: LogLevel, message: string) {
        return (`${LogHelper.INTERNAL_TAG} [${logLevel}] ${message}\n`);
    }

    public setFocusOnLogChannel() {
         // Do nothing
        return;
    }
}