microsoft/vscode-react-native
Publicmirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable
src/common/log/logHelper.ts
89lines · 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 | * Helper for the log utility. |
| 6 | */ |
| 7 | |
| 8 | import * as util from "util"; |
| 9 | import {InternalError, InternalErrorLevel} from "../error/internalError"; |
| 10 | |
| 11 | export enum LogLevel { |
| 12 | None = 0, |
| 13 | Error = 1, |
| 14 | Warning = 2, |
| 15 | Info = 3, |
| 16 | Debug = 4, |
| 17 | Trace = 5 |
| 18 | } |
| 19 | |
| 20 | export class LogHelper { |
| 21 | public static MESSAGE_TAG: string = "[vscode-react-native]"; |
| 22 | public static INTERNAL_TAG: string = "[Internal]"; |
| 23 | public static ERROR_TAG_FORMATSTRING: string = "[Error : %s] "; |
| 24 | public static ERROR_TAG: string = "[Error]"; |
| 25 | public static WARN_TAG: string = "[Warning]"; |
| 26 | private static ERROR_CODE_WIDTH: string = "0000"; |
| 27 | private static LOG_LEVEL_NAME: string = "RN_LOG_LEVEL"; |
| 28 | |
| 29 | public static get logLevel(): LogLevel { |
| 30 | let valName: string = process.env[LogHelper.LOG_LEVEL_NAME]; |
| 31 | |
| 32 | if (typeof(valName) === "undefined") { |
| 33 | valName = "None"; // Set the default LogLevel to LogLevel.None |
| 34 | } |
| 35 | |
| 36 | return (<any> LogLevel)[valName]; |
| 37 | } |
| 38 | |
| 39 | public static set logLevel(level: LogLevel) { |
| 40 | if (!level) { |
| 41 | return; |
| 42 | } |
| 43 | |
| 44 | // Set the process env value |
| 45 | process.env[LogHelper.LOG_LEVEL_NAME] = LogLevel[level]; |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Gets the message of a non null error, if any. Otherwise it returns the empty string. |
| 50 | */ |
| 51 | public static getErrorString(e: any): string { |
| 52 | |
| 53 | if (e.isInternalError) { |
| 54 | let errorMessage = e.message; |
| 55 | let errorMessagePrefix = LogHelper.getErrorMessagePrefix(e); |
| 56 | return errorMessagePrefix + " " + errorMessage; |
| 57 | } else { |
| 58 | let message = e.message || e.error && e.error.message; |
| 59 | if (!message) { |
| 60 | try { |
| 61 | return JSON.stringify(e); |
| 62 | } catch (exception) { |
| 63 | // This is a best-effort feature, so we ignore any exceptions. If possible we'll print the error stringified. |
| 64 | // If not, we'll just use one of the fallbacks |
| 65 | return e.error || e.toString() || ""; |
| 66 | } |
| 67 | } else { |
| 68 | return message; |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | private static getErrorMessagePrefix(error: InternalError) { |
| 74 | if (!error) { |
| 75 | return ""; |
| 76 | } |
| 77 | |
| 78 | switch (error.errorLevel) { |
| 79 | case InternalErrorLevel.Error: |
| 80 | // Encode the error code to a four-char code - ex, 0198 |
| 81 | let errorCodeString = (LogHelper.ERROR_CODE_WIDTH + error.errorCode).slice(-LogHelper.ERROR_CODE_WIDTH.length); |
| 82 | return util.format(LogHelper.ERROR_TAG_FORMATSTRING, errorCodeString); |
| 83 | case InternalErrorLevel.Warning: |
| 84 | return `${LogHelper.WARN_TAG}`; |
| 85 | default: |
| 86 | return `${LogHelper.WARN_TAG}`; |
| 87 | } |
| 88 | } |
| 89 | } |
| 90 | |