microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
0.1.3

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/common/log/log.ts

117lines · 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
cf138e34Meena Kunnathur Balakrishnan10 years ago75/**
76* Brings the target output window to focus.
77*/
f1e34747Meena Kunnathur Balakrishnan10 years ago78export function setFocusOnLogChannel() {
79globalLogger.setFocusOnLogChannel();
cf138e34Meena Kunnathur Balakrishnan10 years ago80}
81
14a23821digeff10 years ago82/**
17161993Meena Kunnathur Balakrishnan10 years ago83* Logs a message to the console.
14a23821digeff10 years ago84*/
f1a07677Meena Kunnathur Balakrishnan10 years ago85export function logWithLogger(logger: ILogger, message: string, formatMessage: boolean) {
86logger.logMessage(message, formatMessage);
17161993Meena Kunnathur Balakrishnan10 years ago87}
14a23821digeff10 years ago88
898cb3c6Meena Kunnathur Balakrishnan10 years ago89/**
90* Logs a message to the console.
91*/
92export function logToStderr(message: string, formatMessage: boolean = true) {
93new StreamLogger(process.stderr).logMessage(message, formatMessage);
94}
95
96/**
97* Logs a message to the console.
98*/
99export function logToStdout(message: string, formatMessage: boolean = true) {
100new StreamLogger(process.stdout).logMessage(message, formatMessage);
101}
102
103export function getCommandStatusString(command: string, status: CommandStatus) {
17161993Meena Kunnathur Balakrishnan10 years ago104console.assert(status >= CommandStatus.Start && status <= CommandStatus.End, "Unsupported Command Status");
105
106switch (status) {
107case CommandStatus.Start:
108return `Executing command: ${command}`;
109
110case CommandStatus.End:
111return `Finished executing: ${command}`;
112
113default:
114throw new Error("Unsupported command status");
3736c251dlebu10 years ago115}
116}
3fb37ad5unknown10 years ago117}