microsoft/vscode-react-native

Public

mirrored from https://github.com/microsoft/vscode-react-nativeAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
190e393cdfd678505e325972de68688a94051a36

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

src/common/log/logHelper.ts

111lines · 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
8import * as util from "util";
9import {InternalErrorLevel} from "../error/internalError";
10
11export enum LogLevel {
12 None = 0,
13 Error = 1,
14 Warning = 2,
15 Info = 3,
16 Debug = 4,
17 Trace = 5
18}
19
20export enum LogChannelType {
21 Console = 0,
22 OutputChannel = 1,
23 WritableStream = 2
24}
25
26export 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
33 public static getLogChannelType(targetChannel: any): LogChannelType {
34 if (!targetChannel) {
35 return -1;
36 } else if (typeof targetChannel.log === "function") {
37 return LogChannelType.Console;
38 } else if (typeof targetChannel.append === "function") {
39 return LogChannelType.OutputChannel;
40 } else if (typeof targetChannel.write === "function") {
41 return LogChannelType.WritableStream;
42 } else {
43 return 0;
44 }
45 }
46
47
48 /**
49 * Gets the message of a non null error, if any. Otherwise it returns the empty string.
50 */
51 public static getErrorMessage(e: any): string {
52 let message = e.message || e.error && e.error.message;
53 if (!message) {
54 try {
55 return JSON.stringify(e);
56 } catch (exception) {
57 // This is a best-effort feature, so we ignore any exceptions. If possible we'll print the error stringified.
58 // If not, we'll just use one of the fallbacks
59 return e.error || e.toString() || "";
60 }
61 } else {
62 return message;
63 }
64
65 }
66
67 public static getErrorString(e: any, targetChannel: any): string {
68 let errorMessageTag = LogHelper.getLogChannelType(targetChannel) === LogChannelType.OutputChannel ?
69 "" :
70 `${LogHelper.MESSAGE_TAG}`;
71
72 if (e.isInternalError()) {
73 let errorMessage = e.message;
74 let errorMessagePrefix = `${LogHelper.WARN_TAG}`;
75 switch (e.errorLevel) {
76 case InternalErrorLevel.Error: {
77 // Transforms 32 to say "0032" (for fixed width = 4)
78 let errorCodeString = (LogHelper.ERROR_CODE_WIDTH + e.errorCode).slice(-LogHelper.ERROR_CODE_WIDTH.length);
79 errorMessagePrefix = util.format(LogHelper.ERROR_TAG_FORMATSTRING, errorCodeString);
80 break;
81 }
82
83 case InternalErrorLevel.Warning:
84 default:
85 errorMessagePrefix = `${LogHelper.WARN_TAG}`;
86 break;
87 }
88
89 return errorMessageTag + errorMessagePrefix + errorMessage;
90 } else {
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 }
99 }
100
101 public static getExtensionLogLevel(): LogLevel {
102 // TODO: Improve this logic. Make it case insensitive, etc...
103 let logLevelIndex = process.argv.indexOf("--extensionLogLevel");
104 if (logLevelIndex >= 0 && logLevelIndex + 1 < process.argv.length) {
105 let logLevelText = process.argv[logLevelIndex + 1];
106 return (<any>LogLevel)[logLevelText];
107 } else {
108 return LogLevel.None; // Default extension log level
109 }
110 }
111}
112