microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/common/log/log.ts
114lines · 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 | * Logging utility class. |
| 6 | */ |
| 7 | |
| 8 | import {CommandStatus} from "../commandExecutor"; |
| 9 | import {LogHelper, LogChannelType, LogLevel} from "./logHelper"; |
| 10 | import {OutputChannelLogFormatter} from "./outputChannelLogFormatter"; |
| 11 | import {StreamLogFormatter} from "./streamLogFormatter"; |
| 12 | import {OutputChannel} from "vscode"; |
| 13 | |
| 14 | export class Log { |
| 15 | public static logCommandStatus(command: string, status: CommandStatus, targetLogChannel: any = null) { |
| 16 | console.assert(status >= CommandStatus.Start && status <= CommandStatus.End, "Unsupported Command Status"); |
| 17 | |
| 18 | let statusMessage = Log.getCommandStatusString(command, status); |
| 19 | Log.log(statusMessage, targetLogChannel || console); |
| 20 | } |
| 21 | |
| 22 | /** |
| 23 | * Logs an internal message for when someone is debugging the extension itself. |
| 24 | * Customers aren't interested in these messages, so we normally shouldn't show |
| 25 | * them to them. |
| 26 | */ |
| 27 | public static logInternalMessage(logLevel: LogLevel, message: string, targetChannel: any = null) { |
| 28 | if (LogHelper.getExtensionLogLevel() >= logLevel) { |
| 29 | this.logMessage(`[Internal-${logLevel}] ${message}`, targetChannel); |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * Logs a warning message to the console. |
| 35 | */ |
| 36 | public static logWarning(error?: any, outputChannel: OutputChannel = null, logStack = true) { |
| 37 | this.logError(error, outputChannel, logStack); |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Logs an error message to the console. |
| 42 | */ |
| 43 | public static logError(error?: any, outputChannel?: OutputChannel, logStack = true) { |
| 44 | let errorMessageToLog = LogHelper.getErrorString(error, outputChannel || console); |
| 45 | |
| 46 | if (outputChannel) { |
| 47 | Log.logToOutputChannel(errorMessageToLog, outputChannel); |
| 48 | } else { |
| 49 | console.error(errorMessageToLog); |
| 50 | } |
| 51 | |
| 52 | // We will not need the stack trace when logging to the OutputChannel in VS Code |
| 53 | if (!outputChannel && logStack && error && (<Error>error).stack) { |
| 54 | console.error(`Stack: ${(<Error>error).stack}`); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Logs a message to the console or the OutputChannel |
| 60 | */ |
| 61 | public static logMessage(message: string, targetLogChannel: any = null, formatMessage: boolean = true) { |
| 62 | Log.log(message, targetLogChannel || console, formatMessage); |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Logs a message to the console. |
| 67 | */ |
| 68 | public static logToConsole(message: string, formatMessage: boolean = true) { |
| 69 | console.log(formatMessage ? |
| 70 | StreamLogFormatter.getFormattedMessage(message) : |
| 71 | message); |
| 72 | } |
| 73 | |
| 74 | public static logToOutputChannel(message: string, outputChannel: OutputChannel, formatMessage: boolean = true) { |
| 75 | outputChannel.appendLine(formatMessage ? |
| 76 | OutputChannelLogFormatter.getFormattedMessage(message) : |
| 77 | message); |
| 78 | outputChannel.show(); |
| 79 | } |
| 80 | |
| 81 | private static getCommandStatusString(command: string, status: CommandStatus) { |
| 82 | console.assert(status >= CommandStatus.Start && status <= CommandStatus.End, "Unsupported Command Status"); |
| 83 | |
| 84 | switch (status) { |
| 85 | case CommandStatus.Start: |
| 86 | return `Executing command: ${command}`; |
| 87 | |
| 88 | case CommandStatus.End: |
| 89 | return `Finished executing: ${command}`; |
| 90 | |
| 91 | default: |
| 92 | throw new Error("Unsupported command status"); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | private static log(message: string, targetLogChannel: any, formatMessage: boolean = true) { |
| 97 | switch (LogHelper.getLogChannelType(targetLogChannel)) { |
| 98 | case LogChannelType.OutputChannel: |
| 99 | Log.logToOutputChannel(message, targetLogChannel, formatMessage); |
| 100 | break; |
| 101 | |
| 102 | case LogChannelType.WritableStream: |
| 103 | targetLogChannel.write(formatMessage ? |
| 104 | StreamLogFormatter.getFormattedMessage(message) : |
| 105 | message); |
| 106 | break; |
| 107 | |
| 108 | case LogChannelType.Console: |
| 109 | default: |
| 110 | Log.logToConsole(message, formatMessage); |
| 111 | break; |
| 112 | } |
| 113 | } |
| 114 | } |