microsoft/vscode-react-native
Publicmirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable
src/common/log.ts
104lines · 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 {OutputChannel} from "vscode"; |
| 9 | |
| 10 | export enum LogLevel { |
| 11 | None = 0, |
| 12 | Error = 1, |
| 13 | Warning = 2, |
| 14 | Info = 3, |
| 15 | Debug = 4, |
| 16 | Trace = 5 |
| 17 | } |
| 18 | |
| 19 | export class Log { |
| 20 | |
| 21 | private static TAG: string = "[vscode-react-native]"; |
| 22 | |
| 23 | public static appendStringToOutputChannel(message: string, outputChannel: OutputChannel) { |
| 24 | outputChannel.appendLine(Log.formatStringForOutputChannel(message)); |
| 25 | outputChannel.show(); |
| 26 | } |
| 27 | |
| 28 | public static commandStarted(command: string, outputChannel?: OutputChannel) { |
| 29 | Log.logMessage(`Executing command: ${command}`, outputChannel); |
| 30 | } |
| 31 | |
| 32 | public static commandEnded(command: string, outputChannel?: OutputChannel) { |
| 33 | Log.logMessage(`Finished executing: ${command}\n`, outputChannel); |
| 34 | } |
| 35 | |
| 36 | public static commandFailed(command: string, error: any, outputChannel?: OutputChannel) { |
| 37 | Log.logError(`Error while executing: ${command}`, error, outputChannel); |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Logs an internal message for when someone is debugging the extension itself. |
| 42 | * Customers aren't interested in these messages, so we normally shouldn't show |
| 43 | * them to them. |
| 44 | */ |
| 45 | public static logInternalMessage(logLevel: LogLevel, message: string) { |
| 46 | if (this.extensionLogLevel() >= logLevel) { |
| 47 | this.logMessage(`[Internal-${logLevel}] ${message}`); |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Logs an error message to the console. |
| 53 | */ |
| 54 | public static logError(message: string, error?: any, outputChannel?: OutputChannel, logStack = true) { |
| 55 | let errorMessageToLog = outputChannel ? `${message} ${Log.getErrorMessage(error)}` : `${Log.TAG} ${message} ${Log.getErrorMessage(error)}`; |
| 56 | |
| 57 | if (outputChannel) { |
| 58 | Log.appendStringToOutputChannel(errorMessageToLog, outputChannel); |
| 59 | } else { |
| 60 | console.error(errorMessageToLog); |
| 61 | } |
| 62 | |
| 63 | // We will not need the stack trace when logging to the OutputChannel in VS Code |
| 64 | if (!outputChannel && logStack && error && (<Error>error).stack) { |
| 65 | console.error(`Stack: ${(<Error>error).stack}`); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Gets the message of an error, if any. Otherwise it returns the empty string. |
| 71 | */ |
| 72 | public static getErrorMessage(e: any): string { |
| 73 | return e && e.message || e && e.error && e.error.message || e && e.toString() || ""; |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Logs a message to the console. |
| 78 | */ |
| 79 | public static logMessage(message: string, outputChannel?: OutputChannel) { |
| 80 | let messageToLog = outputChannel ? message : `${Log.TAG} ${message}`; |
| 81 | |
| 82 | if (outputChannel) { |
| 83 | Log.appendStringToOutputChannel(messageToLog, outputChannel); |
| 84 | } else { |
| 85 | console.log(messageToLog); |
| 86 | } |
| 87 | |
| 88 | } |
| 89 | |
| 90 | private static extensionLogLevel(): LogLevel { |
| 91 | // TODO: Improve this logic. Make it case insensitive, etc... |
| 92 | let logLevelIndex = process.argv.indexOf("--extensionLogLevel"); |
| 93 | if (logLevelIndex >= 0 && logLevelIndex + 1 < process.argv.length) { |
| 94 | let logLevelText = process.argv[logLevelIndex + 1]; |
| 95 | return (<any>LogLevel)[logLevelText]; |
| 96 | } else { |
| 97 | return LogLevel.None; // Default extension log level |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | private static formatStringForOutputChannel(message: string) { |
| 102 | return "######### " + message + " ##########"; |
| 103 | } |
| 104 | } |