microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
a9d77c8b0667ef4b6618231d585f8465dae8b0df

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/common/log/log.ts

103lines · 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 {CommandStatus} from "../commandExecutor";
9import {LogHelper, LogLevel} from "./logHelper";
10import {ILogger, StreamLogger, ConsoleLogger} from "./loggers";
11
12export module Log {
13 /**
14 * The global logger defaults to the Console logger.
15 */
16 let globalLogger: ILogger = new ConsoleLogger();
17
18 /**
19 * Sets the global logger.
20 */
21 export function SetGlobalLogger(logger: ILogger) {
22 globalLogger = logger;
23 }
24
25 /**
26 * Logs a message.
27 */
28 export function logMessage(message: string, formatMessage: boolean = true) {
29 globalLogger.logMessage(message, formatMessage);
30 }
31
32 /**
33 * Logs an error message.
34 */
35 export function logError(error?: any, logStack = true) {
36 let errorMessageToLog = LogHelper.getErrorString(error);
37 globalLogger.logError(errorMessageToLog, error, logStack);
38 }
39
40 /**
41 * Logs a warning message.
42 */
43 export function logWarning(error?: any, logStack = true) {
44 Log.logError(error, logStack);
45 }
46
47 /**
48 * Logs an internal message for when someone is debugging the extension itself.
49 * Customers aren't interested in these messages, so we normally shouldn't show
50 * them to them.
51 */
52 export function logInternalMessage(logLevel: LogLevel, message: string) {
53 if (LogHelper.logLevel >= logLevel) {
54 globalLogger.logInternalMessage(logLevel, message);
55 }
56 }
57
58 /**
59 * Logs the status (Start/End) of a command.
60 */
61 export function logCommandStatus(command: string, status: CommandStatus) {
62 console.assert(status >= CommandStatus.Start && status <= CommandStatus.End, "Unsupported Command Status");
63
64 let statusMessage = Log.getCommandStatusString(command, status);
65 globalLogger.logMessage(statusMessage);
66 }
67
68 /**
69 * Logs a message to the console.
70 */
71 export function logWithLogger(logger: ILogger, message: string, formatMessage: boolean) {
72 logger.logMessage(message, formatMessage);
73 }
74
75 /**
76 * Logs a message to the console.
77 */
78 export function logToStderr(message: string, formatMessage: boolean = true) {
79 new StreamLogger(process.stderr).logMessage(message, formatMessage);
80 }
81
82 /**
83 * Logs a message to the console.
84 */
85 export function logToStdout(message: string, formatMessage: boolean = true) {
86 new StreamLogger(process.stdout).logMessage(message, formatMessage);
87 }
88
89 export function getCommandStatusString(command: string, status: CommandStatus) {
90 console.assert(status >= CommandStatus.Start && status <= CommandStatus.End, "Unsupported Command Status");
91
92 switch (status) {
93 case CommandStatus.Start:
94 return `Executing command: ${command}`;
95
96 case CommandStatus.End:
97 return `Finished executing: ${command}`;
98
99 default:
100 throw new Error("Unsupported command status");
101 }
102 }
103}