microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/common/log.ts
118lines · modeblame
a31b007cunknown10 years ago | 1 | // Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | // Licensed under the MIT license. See LICENSE file in the project root for details. | |
| 3 | | |
bedf110funknown10 years ago | 4 | /** |
| 5 | * Logging utility class. | |
| 6 | */ | |
3194e9afMeena Kunnathur Balakrishnan10 years ago | 7 | |
| 8 | import {OutputChannel} from "vscode"; | |
| 9 | | |
ea8a5f88digeff10 years ago | 10 | export enum LogLevel { |
5d4d4de0digeff10 years ago | 11 | None = 0, |
| 12 | Error = 1, | |
| 13 | Warning = 2, | |
ea8a5f88digeff10 years ago | 14 | Info = 3, |
| 15 | Debug = 4, | |
| 16 | Trace = 5 | |
5d4d4de0digeff10 years ago | 17 | } |
| 18 | | |
3fb37ad5unknown10 years ago | 19 | export class Log { |
bedf110funknown10 years ago | 20 | |
| 21 | private static TAG: string = "[vscode-react-native]"; | |
| 22 | | |
3194e9afMeena Kunnathur Balakrishnan10 years ago | 23 | public static appendStringToOutputChannel(message: string, outputChannel: OutputChannel) { |
| 24 | outputChannel.appendLine(Log.formatStringForOutputChannel(message)); | |
| 25 | outputChannel.show(); | |
bedf110funknown10 years ago | 26 | } |
| 27 | | |
3194e9afMeena Kunnathur Balakrishnan10 years ago | 28 | public static commandStarted(command: string, outputChannel?: OutputChannel) { |
a61d89c4Meena Kunnathur Balakrishnan10 years ago | 29 | Log.logMessage(`Executing command: ${command}`, outputChannel); |
bedf110funknown10 years ago | 30 | } |
| 31 | | |
3194e9afMeena Kunnathur Balakrishnan10 years ago | 32 | public static commandEnded(command: string, outputChannel?: OutputChannel) { |
fb8f49fbMeena Kunnathur Balakrishnan10 years ago | 33 | Log.logMessage(`Finished executing: ${command}\n`, outputChannel); |
3194e9afMeena Kunnathur Balakrishnan10 years ago | 34 | } |
| 35 | | |
| 36 | public static commandFailed(command: string, error: any, outputChannel?: OutputChannel) { | |
a61d89c4Meena Kunnathur Balakrishnan10 years ago | 37 | Log.logError(`Error while executing: ${command}`, error, outputChannel); |
3fb37ad5unknown10 years ago | 38 | } |
| 39 | | |
4677921cdigeff10 years ago | 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 | */ | |
ea8a5f88digeff10 years ago | 45 | public static logInternalMessage(logLevel: LogLevel, message: string) { |
| 46 | if (this.extensionLogLevel() >= logLevel) { | |
| 47 | this.logMessage(`[Internal-${logLevel}] ${message}`); | |
5d4d4de0digeff10 years ago | 48 | } |
4677921cdigeff10 years ago | 49 | } |
| 50 | | |
bedf110funknown10 years ago | 51 | /** |
| 52 | * Logs an error message to the console. | |
| 53 | */ | |
a61d89c4Meena Kunnathur Balakrishnan10 years ago | 54 | public static logError(message: string, error?: any, outputChannel?: OutputChannel, logStack = true) { |
14a23821digeff10 years ago | 55 | let errorMessagePrefix = outputChannel ? "" : `${Log.TAG} `; |
| 56 | let errorMessagePostfix = error ? `: ${Log.getErrorMessage(error)}` : ""; | |
| 57 | let errorMessageToLog = errorMessagePrefix + message + errorMessagePostfix; | |
a61d89c4Meena Kunnathur Balakrishnan10 years ago | 58 | |
| 59 | if (outputChannel) { | |
| 60 | Log.appendStringToOutputChannel(errorMessageToLog, outputChannel); | |
| 61 | } else { | |
| 62 | console.error(errorMessageToLog); | |
| 63 | } | |
| 64 | | |
| 65 | // We will not need the stack trace when logging to the OutputChannel in VS Code | |
| 66 | if (!outputChannel && logStack && error && (<Error>error).stack) { | |
2f10b3adunknown10 years ago | 67 | console.error(`Stack: ${(<Error>error).stack}`); |
| 68 | } | |
3fb37ad5unknown10 years ago | 69 | } |
| 70 | | |
3736c251dlebu10 years ago | 71 | /** |
| 72 | * Logs a message to the console. | |
| 73 | */ | |
| 74 | public static logMessage(message: string, outputChannel?: OutputChannel) { | |
| 75 | let messageToLog = outputChannel ? message : `${Log.TAG} ${message}`; | |
| 76 | | |
| 77 | if (outputChannel) { | |
| 78 | Log.appendStringToOutputChannel(messageToLog, outputChannel); | |
| 79 | } else { | |
| 80 | console.log(messageToLog); | |
| 81 | } | |
| 82 | | |
| 83 | } | |
| 84 | | |
14a23821digeff10 years ago | 85 | /** |
| 86 | * Gets the message of a non null error, if any. Otherwise it returns the empty string. | |
| 87 | */ | |
| 88 | private static getErrorMessage(e: any): string { | |
| 89 | let message = e.message || e.error && e.error.message; | |
| 90 | if (!message) { | |
| 91 | try { | |
| 92 | return JSON.stringify(e); | |
| 93 | } catch (exception) { | |
| 94 | // This is a best-effort feature, so we ignore any exceptions. If possible we'll print the error stringified. | |
| 95 | // If not, we'll just use one of the fallbacks | |
| 96 | return e.error || e.toString() || ""; | |
| 97 | } | |
| 98 | } else { | |
| 99 | return message; | |
| 100 | } | |
| 101 | | |
| 102 | } | |
| 103 | | |
3736c251dlebu10 years ago | 104 | private static extensionLogLevel(): LogLevel { |
| 105 | // TODO: Improve this logic. Make it case insensitive, etc... | |
| 106 | let logLevelIndex = process.argv.indexOf("--extensionLogLevel"); | |
| 107 | if (logLevelIndex >= 0 && logLevelIndex + 1 < process.argv.length) { | |
| 108 | let logLevelText = process.argv[logLevelIndex + 1]; | |
| 109 | return (<any>LogLevel)[logLevelText]; | |
| 110 | } else { | |
| 111 | return LogLevel.None; // Default extension log level | |
| 112 | } | |
| 113 | } | |
| 114 | | |
| 115 | private static formatStringForOutputChannel(message: string) { | |
| 116 | return "######### " + message + " ##########"; | |
| 117 | } | |
3fb37ad5unknown10 years ago | 118 | } |