microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
e54a23cec8f8d2b2adefd558262187fc8444f2c1

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/utils/commands/log.ts

105lines · 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
10enum LogLevel {
11 None = 0,
12 Error = 1,
13 Warning = 2,
14 Debug = 3,
15 Trace = 4
16}
17
18export class Log {
19
20 private static TAG: string = "[vscode-react-native]";
21 private static formatStringForOutputChannel(message: string) {
22 return "######### " + message + " ##########";
23 }
24
25 public static appendStringToOutputChannel(message: string, outputChannel: OutputChannel) {
26 outputChannel.appendLine(Log.formatStringForOutputChannel(message));
27 outputChannel.show();
28 }
29
30 public static commandStarted(command: string, outputChannel?: OutputChannel) {
31 Log.logMessage(`Executing command: ${command}`, outputChannel);
32 }
33
34 public static commandEnded(command: string, outputChannel?: OutputChannel) {
35 Log.logMessage(`Finished executing: ${command}\n`, outputChannel);
36 }
37
38 public static commandFailed(command: string, error: any, outputChannel?: OutputChannel) {
39 Log.logError(`Error while executing: ${command}`, error, outputChannel);
40 }
41
42 /**
43 * Logs a message to the console.
44 */
45 public static logMessage(message: string, outputChannel?: OutputChannel) {
46 let messageToLog = outputChannel ? message : `${Log.TAG} ${message}`;
47
48 if (outputChannel) {
49 Log.appendStringToOutputChannel(messageToLog, outputChannel);
50 } else {
51 console.log(messageToLog);
52 }
53
54 }
55
56 private static extensionLogLevel(): LogLevel {
57 // TODO: Improve this logic. Make it case insensitive, etc...
58 let logLevelIndex = process.argv.indexOf("--extensionLogLevel");
59 if (logLevelIndex >= 0 && logLevelIndex + 1 < process.argv.length) {
60 let logLevelText = process.argv[logLevelIndex + 1];
61 return (<any>LogLevel)[logLevelText];
62 } else {
63 return LogLevel.None; // Default extension log level
64 }
65 }
66
67 private static shouldLogInternal(): boolean {
68 return this.extensionLogLevel() > LogLevel.None;
69 }
70 /**
71 * Logs an internal message for when someone is debugging the extension itself.
72 * Customers aren't interested in these messages, so we normally shouldn't show
73 * them to them.
74 */
75 public static logInternalMessage(message: string) {
76 if (this.shouldLogInternal()) {
77 console.log(`${Log.TAG}[Internal] ${message}`);
78 }
79 }
80
81 /**
82 * Logs an error message to the console.
83 */
84 public static logError(message: string, error?: any, outputChannel?: OutputChannel, logStack = true) {
85 let errorMessageToLog = outputChannel ? `${message} ${Log.getErrorMessage(error)}` : `${Log.TAG} ${message} ${Log.getErrorMessage(error)}`;
86
87 if (outputChannel) {
88 Log.appendStringToOutputChannel(errorMessageToLog, outputChannel);
89 } else {
90 console.error(errorMessageToLog);
91 }
92
93 // We will not need the stack trace when logging to the OutputChannel in VS Code
94 if (!outputChannel && logStack && error && (<Error>error).stack) {
95 console.error(`Stack: ${(<Error>error).stack}`);
96 }
97 }
98
99 /**
100 * Gets the message of an error, if any. Otherwise it returns the empty string.
101 */
102 public static getErrorMessage(e: any): string {
103 return e && e.message || e && e.error && e.error.message || e && e.toString() || "";
104 }
105}