microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
0502b7a86030ea91bf3320d0f2ad32aff86fa6db

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/common/log.ts

118lines · 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 public static commandFailed(command: string, error: any, outputChannel?: OutputChannel) {
37 Log.logError(`Error while executing: ${command}`, error, outputChannel);
38 }
39
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 */
45 public static logInternalMessage(logLevel: LogLevel, message: string) {
46 if (this.extensionLogLevel() >= logLevel) {
47 this.logMessage(`[Internal-${logLevel}] ${message}`);
48 }
49 }
50
51 /**
52 * Logs an error message to the console.
53 */
54 public static logError(message: string, error?: any, outputChannel?: OutputChannel, logStack = true) {
55 let errorMessagePrefix = outputChannel ? "" : `${Log.TAG} `;
56 let errorMessagePostfix = error ? `: ${Log.getErrorMessage(error)}` : "";
57 let errorMessageToLog = errorMessagePrefix + message + errorMessagePostfix;
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) {
67 console.error(`Stack: ${(<Error>error).stack}`);
68 }
69 }
70
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
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
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 }
118}