microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/common/log/logHelper.ts
101lines · 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 * as util from "util"; |
| 9 | import {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 enum LogChannelType { |
| 21 | Console = 0, |
| 22 | OutputChannel = 1, |
| 23 | WritableStream = 2 |
| 24 | } |
| 25 | |
| 26 | export class LogHelper { |
| 27 | public static MESSAGE_TAG: string = "[vscode-react-native]"; |
| 28 | public static INTERNAL_TAG: string = "[Internal]"; |
| 29 | public static ERROR_TAG_FORMATSTRING: string = "[Error: %s]"; |
| 30 | public static WARN_TAG: string = "[Warning]"; |
| 31 | private static ERROR_CODE_WIDTH: string = "0000"; |
| 32 | private static LOG_LEVEL_NAME: string = "TACO_LOG_LEVEL"; |
| 33 | |
| 34 | public static get logLevel(): LogLevel { |
| 35 | let valName: string = process.env[LogHelper.LOG_LEVEL_NAME]; |
| 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 | * Determines the type of the log channel (LogChannelType). |
| 50 | */ |
| 51 | public static getLogChannelType(targetChannel: any): LogChannelType { |
| 52 | console.assert(!!targetChannel, "targetChannl is undefined"); |
| 53 | if (typeof targetChannel.log === "function") { |
| 54 | return LogChannelType.Console; |
| 55 | } else if (typeof targetChannel.append === "function") { |
| 56 | return LogChannelType.OutputChannel; |
| 57 | } else if (typeof targetChannel.write === "function") { |
| 58 | return LogChannelType.WritableStream; |
| 59 | } else { |
| 60 | return LogChannelType.Console; |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | |
| 65 | /** |
| 66 | * Gets the message of a non null error, if any. Otherwise it returns the empty string. |
| 67 | */ |
| 68 | public static getErrorString(e: any, targetChannel: any): string { |
| 69 | let errorMessageTag = LogHelper.getLogChannelType(targetChannel) === LogChannelType.OutputChannel ? |
| 70 | "" : |
| 71 | `${LogHelper.MESSAGE_TAG}`; |
| 72 | |
| 73 | if (e.isInternalError()) { |
| 74 | let errorMessage = e.message; |
| 75 | let errorMessagePrefix = `${LogHelper.WARN_TAG}`; |
| 76 | switch (e.errorLevel) { |
| 77 | case InternalErrorLevel.Error: { |
| 78 | // Encode the error code to a four-char code - ex, 0198 |
| 79 | let errorCodeString = (LogHelper.ERROR_CODE_WIDTH + e.errorCode).slice(-LogHelper.ERROR_CODE_WIDTH.length); |
| 80 | errorMessagePrefix = util.format(LogHelper.ERROR_TAG_FORMATSTRING, errorCodeString); |
| 81 | break; |
| 82 | } |
| 83 | |
| 84 | case InternalErrorLevel.Warning: |
| 85 | default: |
| 86 | errorMessagePrefix = `${LogHelper.WARN_TAG}`; |
| 87 | break; |
| 88 | } |
| 89 | |
| 90 | return errorMessageTag + errorMessagePrefix + errorMessage; |
| 91 | } else { |
| 92 | try { |
| 93 | return JSON.stringify(e); |
| 94 | } catch (exception) { |
| 95 | // This is a best-effort feature, so we ignore any exceptions. If possible we'll print the error stringified. |
| 96 | // If not, we'll just use one of the fallbacks |
| 97 | return e.error || e.toString() || ""; |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | |