microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
b4683d60d2ce52828fca73ad1093856d8a898640

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/utils/commands/log.ts

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