microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
70f7cae4a697f9868d22dfdd08089a2cd2076772

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/common/log/log.ts

124lines · 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 stream data buffer.
70 */
71 export function logStreamData(data: Buffer, stream: NodeJS.WritableStream) {
72 globalLogger.logStreamData(data, stream);
73 }
74
75 /**
76 * Logs string
77 */
78 export function logString(data: string) {
79 globalLogger.logString(data);
80 }
81
82 /**
83 * Brings the target output window to focus.
84 */
85 export function setFocusOnLogChannel() {
86 globalLogger.setFocusOnLogChannel();
87 }
88
89 /**
90 * Logs a message to the console.
91 */
92 export function logWithLogger(logger: ILogger, message: string, formatMessage: boolean) {
93 logger.logMessage(message, formatMessage);
94 }
95
96 /**
97 * Logs a message to the console.
98 */
99 export function logToStderr(message: string, formatMessage: boolean = true) {
100 new StreamLogger(process.stderr).logMessage(message, formatMessage);
101 }
102
103 /**
104 * Logs a message to the console.
105 */
106 export function logToStdout(message: string, formatMessage: boolean = true) {
107 new StreamLogger(process.stdout).logMessage(message, formatMessage);
108 }
109
110 export function getCommandStatusString(command: string, status: CommandStatus) {
111 console.assert(status >= CommandStatus.Start && status <= CommandStatus.End, "Unsupported Command Status");
112
113 switch (status) {
114 case CommandStatus.Start:
115 return `Executing command: ${command}`;
116
117 case CommandStatus.End:
118 return `Finished executing: ${command}`;
119
120 default:
121 throw new Error("Unsupported command status");
122 }
123 }
124}