microsoft/vscode-react-native

Public

mirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
8a9925ee8b659b3f9dba8a521555c8ee58fd8f31

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/common/log.ts

122lines · 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 {OutputChannel} from "vscode";
9
10export enum LogLevel {
11 None = 0,
12 Error = 1,
13 Warning = 2,
14 Info = 3,
15 Debug = 4,
16 Trace = 5
17}
18
19export 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 /**
37 * Logs an internal message for when someone is debugging the extension itself.
38 * Customers aren't interested in these messages, so we normally shouldn't show
39 * them to them.
40 */
41 public static logInternalMessage(logLevel: LogLevel, message: string) {
42 if (this.extensionLogLevel() >= logLevel) {
43 this.logMessage(`[Internal-${logLevel}] ${message}`);
44 }
45 }
46
47 /**
48 * Logs a warning message to the console.
49 */
50 public static logWarning(message: string, error?: any, outputChannel?: OutputChannel, logStack = true) {
51 // TODO #83: Refactor this code and create a better implementation
52 this.logError(`WARNING: ${message}`, error, outputChannel, logStack);
53 }
54
55 /**
56 * Logs an error message to the console.
57 */
58 public static logError(message: string, error?: any, outputChannel?: OutputChannel, logStack = true) {
59 let errorMessagePrefix = outputChannel ? "" : `${Log.TAG} `;
60 let errorMessagePostfix = error ? `: ${Log.getErrorMessage(error)}` : "";
61 let errorMessageToLog = errorMessagePrefix + message + errorMessagePostfix;
62
63 if (outputChannel) {
64 Log.appendStringToOutputChannel(errorMessageToLog, outputChannel);
65 } else {
66 console.error(errorMessageToLog);
67 }
68
69 // We will not need the stack trace when logging to the OutputChannel in VS Code
70 if (!outputChannel && logStack && error && (<Error>error).stack) {
71 console.error(`Stack: ${(<Error>error).stack}`);
72 }
73 }
74
75 /**
76 * Logs a message to the console.
77 */
78 public static logMessage(message: string, outputChannel?: OutputChannel) {
79 let messageToLog = outputChannel ? message : `${Log.TAG} ${message}`;
80
81 if (outputChannel) {
82 Log.appendStringToOutputChannel(messageToLog, outputChannel);
83 } else {
84 console.log(messageToLog);
85 }
86
87 }
88
89 /**
90 * Gets the message of a non null error, if any. Otherwise it returns the empty string.
91 */
92 private static getErrorMessage(e: any): string {
93 let message = e.message || e.error && e.error.message;
94 if (!message) {
95 try {
96 return JSON.stringify(e);
97 } catch (exception) {
98 // This is a best-effort feature, so we ignore any exceptions. If possible we'll print the error stringified.
99 // If not, we'll just use one of the fallbacks
100 return e.error || e.toString() || "";
101 }
102 } else {
103 return message;
104 }
105
106 }
107
108 private static extensionLogLevel(): LogLevel {
109 // TODO: Improve this logic. Make it case insensitive, etc...
110 let logLevelIndex = process.argv.indexOf("--extensionLogLevel");
111 if (logLevelIndex >= 0 && logLevelIndex + 1 < process.argv.length) {
112 let logLevelText = process.argv[logLevelIndex + 1];
113 return (<any>LogLevel)[logLevelText];
114 } else {
115 return LogLevel.None; // Default extension log level
116 }
117 }
118
119 private static formatStringForOutputChannel(message: string) {
120 return "######### " + message + " ##########";
121 }
122}