microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
0.1.0

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/common/log.ts

118lines · modeblame

a31b007cunknown10 years ago1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the MIT license. See LICENSE file in the project root for details.
3
bedf110funknown10 years ago4/**
5* Logging utility class.
6*/
3194e9afMeena Kunnathur Balakrishnan10 years ago7
8import {OutputChannel} from "vscode";
9
ea8a5f88digeff10 years ago10export enum LogLevel {
5d4d4de0digeff10 years ago11None = 0,
12Error = 1,
13Warning = 2,
ea8a5f88digeff10 years ago14Info = 3,
15Debug = 4,
16Trace = 5
5d4d4de0digeff10 years ago17}
18
3fb37ad5unknown10 years ago19export class Log {
bedf110funknown10 years ago20
21private static TAG: string = "[vscode-react-native]";
22
3194e9afMeena Kunnathur Balakrishnan10 years ago23public static appendStringToOutputChannel(message: string, outputChannel: OutputChannel) {
24outputChannel.appendLine(Log.formatStringForOutputChannel(message));
25outputChannel.show();
bedf110funknown10 years ago26}
27
3194e9afMeena Kunnathur Balakrishnan10 years ago28public static commandStarted(command: string, outputChannel?: OutputChannel) {
a61d89c4Meena Kunnathur Balakrishnan10 years ago29Log.logMessage(`Executing command: ${command}`, outputChannel);
bedf110funknown10 years ago30}
31
3194e9afMeena Kunnathur Balakrishnan10 years ago32public static commandEnded(command: string, outputChannel?: OutputChannel) {
fb8f49fbMeena Kunnathur Balakrishnan10 years ago33Log.logMessage(`Finished executing: ${command}\n`, outputChannel);
3194e9afMeena Kunnathur Balakrishnan10 years ago34}
35
36public static commandFailed(command: string, error: any, outputChannel?: OutputChannel) {
a61d89c4Meena Kunnathur Balakrishnan10 years ago37Log.logError(`Error while executing: ${command}`, error, outputChannel);
3fb37ad5unknown10 years ago38}
39
4677921cdigeff10 years ago40/**
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*/
ea8a5f88digeff10 years ago45public static logInternalMessage(logLevel: LogLevel, message: string) {
46if (this.extensionLogLevel() >= logLevel) {
47this.logMessage(`[Internal-${logLevel}] ${message}`);
5d4d4de0digeff10 years ago48}
4677921cdigeff10 years ago49}
50
bedf110funknown10 years ago51/**
52* Logs an error message to the console.
53*/
a61d89c4Meena Kunnathur Balakrishnan10 years ago54public static logError(message: string, error?: any, outputChannel?: OutputChannel, logStack = true) {
14a23821digeff10 years ago55let errorMessagePrefix = outputChannel ? "" : `${Log.TAG} `;
56let errorMessagePostfix = error ? `: ${Log.getErrorMessage(error)}` : "";
57let errorMessageToLog = errorMessagePrefix + message + errorMessagePostfix;
a61d89c4Meena Kunnathur Balakrishnan10 years ago58
59if (outputChannel) {
60Log.appendStringToOutputChannel(errorMessageToLog, outputChannel);
61} else {
62console.error(errorMessageToLog);
63}
64
65// We will not need the stack trace when logging to the OutputChannel in VS Code
66if (!outputChannel && logStack && error && (<Error>error).stack) {
2f10b3adunknown10 years ago67console.error(`Stack: ${(<Error>error).stack}`);
68}
3fb37ad5unknown10 years ago69}
70
3736c251dlebu10 years ago71/**
72* Logs a message to the console.
73*/
74public static logMessage(message: string, outputChannel?: OutputChannel) {
75let messageToLog = outputChannel ? message : `${Log.TAG} ${message}`;
76
77if (outputChannel) {
78Log.appendStringToOutputChannel(messageToLog, outputChannel);
79} else {
80console.log(messageToLog);
81}
82
83}
84
14a23821digeff10 years ago85/**
86* Gets the message of a non null error, if any. Otherwise it returns the empty string.
87*/
88private static getErrorMessage(e: any): string {
89let message = e.message || e.error && e.error.message;
90if (!message) {
91try {
92return 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
96return e.error || e.toString() || "";
97}
98} else {
99return message;
100}
101
102}
103
3736c251dlebu10 years ago104private static extensionLogLevel(): LogLevel {
105// TODO: Improve this logic. Make it case insensitive, etc...
106let logLevelIndex = process.argv.indexOf("--extensionLogLevel");
107if (logLevelIndex >= 0 && logLevelIndex + 1 < process.argv.length) {
108let logLevelText = process.argv[logLevelIndex + 1];
109return (<any>LogLevel)[logLevelText];
110} else {
111return LogLevel.None; // Default extension log level
112}
113}
114
115private static formatStringForOutputChannel(message: string) {
116return "######### " + message + " ##########";
117}
3fb37ad5unknown10 years ago118}