microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
0.4.0

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/common/log/log.ts

124lines · 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
190e393cMeena Kunnathur Balakrishnan10 years ago8import {CommandStatus} from "../commandExecutor";
53520386Meena Kunnathur Balakrishnan10 years ago9import {LogHelper, LogLevel} from "./logHelper";
f1a07677Meena Kunnathur Balakrishnan10 years ago10import {ILogger, StreamLogger, ConsoleLogger} from "./loggers";
3194e9afMeena Kunnathur Balakrishnan10 years ago11
898cb3c6Meena Kunnathur Balakrishnan10 years ago12export module Log {
b6247839Meena Kunnathur Balakrishnan10 years ago13/**
14* The global logger defaults to the Console logger.
15*/
53520386Meena Kunnathur Balakrishnan10 years ago16let globalLogger: ILogger = new ConsoleLogger();
b6247839Meena Kunnathur Balakrishnan10 years ago17
4677921cdigeff10 years ago18/**
b6247839Meena Kunnathur Balakrishnan10 years ago19* Sets the global logger.
4677921cdigeff10 years ago20*/
53520386Meena Kunnathur Balakrishnan10 years ago21export function SetGlobalLogger(logger: ILogger) {
22globalLogger = logger;
10873e11digeff10 years ago23}
24
bedf110funknown10 years ago25/**
898cb3c6Meena Kunnathur Balakrishnan10 years ago26* Logs a message.
bedf110funknown10 years ago27*/
898cb3c6Meena Kunnathur Balakrishnan10 years ago28export function logMessage(message: string, formatMessage: boolean = true) {
53520386Meena Kunnathur Balakrishnan10 years ago29globalLogger.logMessage(message, formatMessage);
898cb3c6Meena Kunnathur Balakrishnan10 years ago30}
a61d89c4Meena Kunnathur Balakrishnan10 years ago31
898cb3c6Meena Kunnathur Balakrishnan10 years ago32/**
33* Logs an error message.
34*/
35export function logError(error?: any, logStack = true) {
36let errorMessageToLog = LogHelper.getErrorString(error);
53520386Meena Kunnathur Balakrishnan10 years ago37globalLogger.logError(errorMessageToLog, error, logStack);
3fb37ad5unknown10 years ago38}
39
3736c251dlebu10 years ago40/**
a289475bMeena Kunnathur Balakrishnan10 years ago41* Logs a warning message.
3736c251dlebu10 years ago42*/
898cb3c6Meena Kunnathur Balakrishnan10 years ago43export function logWarning(error?: any, logStack = true) {
44Log.logError(error, logStack);
a289475bMeena Kunnathur Balakrishnan10 years ago45}
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*/
d206c683Meena Kunnathur Balakrishnan10 years ago52export function logInternalMessage(logLevel: LogLevel, message: string) {
a289475bMeena Kunnathur Balakrishnan10 years ago53if (LogHelper.logLevel >= logLevel) {
53520386Meena Kunnathur Balakrishnan10 years ago54globalLogger.logInternalMessage(logLevel, message);
a289475bMeena Kunnathur Balakrishnan10 years ago55}
56}
57
58/**
59* Logs the status (Start/End) of a command.
60*/
898cb3c6Meena Kunnathur Balakrishnan10 years ago61export function logCommandStatus(command: string, status: CommandStatus) {
a289475bMeena Kunnathur Balakrishnan10 years ago62console.assert(status >= CommandStatus.Start && status <= CommandStatus.End, "Unsupported Command Status");
63
64let statusMessage = Log.getCommandStatusString(command, status);
53520386Meena Kunnathur Balakrishnan10 years ago65globalLogger.logMessage(statusMessage);
3736c251dlebu10 years ago66}
67
99e41548Meena Kunnathur Balakrishnan10 years ago68/**
69* Logs a stream data buffer.
70*/
71export function logStreamData(data: Buffer, stream: NodeJS.WritableStream) {
72globalLogger.logStreamData(data, stream);
73}
74
831f4a85Patricio Beltran9 years ago75/**
76* Logs string
77*/
78export function logString(data: string) {
79globalLogger.logString(data);
80}
81
cf138e34Meena Kunnathur Balakrishnan10 years ago82/**
83* Brings the target output window to focus.
84*/
f1e34747Meena Kunnathur Balakrishnan10 years ago85export function setFocusOnLogChannel() {
86globalLogger.setFocusOnLogChannel();
cf138e34Meena Kunnathur Balakrishnan10 years ago87}
88
14a23821digeff10 years ago89/**
17161993Meena Kunnathur Balakrishnan10 years ago90* Logs a message to the console.
14a23821digeff10 years ago91*/
f1a07677Meena Kunnathur Balakrishnan10 years ago92export function logWithLogger(logger: ILogger, message: string, formatMessage: boolean) {
93logger.logMessage(message, formatMessage);
17161993Meena Kunnathur Balakrishnan10 years ago94}
14a23821digeff10 years ago95
898cb3c6Meena Kunnathur Balakrishnan10 years ago96/**
97* Logs a message to the console.
98*/
99export function logToStderr(message: string, formatMessage: boolean = true) {
100new StreamLogger(process.stderr).logMessage(message, formatMessage);
101}
102
103/**
104* Logs a message to the console.
105*/
106export function logToStdout(message: string, formatMessage: boolean = true) {
107new StreamLogger(process.stdout).logMessage(message, formatMessage);
108}
109
110export function getCommandStatusString(command: string, status: CommandStatus) {
17161993Meena Kunnathur Balakrishnan10 years ago111console.assert(status >= CommandStatus.Start && status <= CommandStatus.End, "Unsupported Command Status");
112
113switch (status) {
114case CommandStatus.Start:
115return `Executing command: ${command}`;
116
117case CommandStatus.End:
118return `Finished executing: ${command}`;
119
120default:
121throw new Error("Unsupported command status");
3736c251dlebu10 years ago122}
123}
3fb37ad5unknown10 years ago124}