microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/common/log/log.ts
117lines · modeblame
a31b007cunknown10 years ago | 1 | // 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 ago | 4 | /** |
| 5 | * Logging utility class. | |
| 6 | */ | |
3194e9afMeena Kunnathur Balakrishnan10 years ago | 7 | |
190e393cMeena Kunnathur Balakrishnan10 years ago | 8 | import {CommandStatus} from "../commandExecutor"; |
53520386Meena Kunnathur Balakrishnan10 years ago | 9 | import {LogHelper, LogLevel} from "./logHelper"; |
f1a07677Meena Kunnathur Balakrishnan10 years ago | 10 | import {ILogger, StreamLogger, ConsoleLogger} from "./loggers"; |
3194e9afMeena Kunnathur Balakrishnan10 years ago | 11 | |
898cb3c6Meena Kunnathur Balakrishnan10 years ago | 12 | export module Log { |
b6247839Meena Kunnathur Balakrishnan10 years ago | 13 | /** |
| 14 | * The global logger defaults to the Console logger. | |
| 15 | */ | |
53520386Meena Kunnathur Balakrishnan10 years ago | 16 | let globalLogger: ILogger = new ConsoleLogger(); |
b6247839Meena Kunnathur Balakrishnan10 years ago | 17 | |
4677921cdigeff10 years ago | 18 | /** |
b6247839Meena Kunnathur Balakrishnan10 years ago | 19 | * Sets the global logger. |
4677921cdigeff10 years ago | 20 | */ |
53520386Meena Kunnathur Balakrishnan10 years ago | 21 | export function SetGlobalLogger(logger: ILogger) { |
| 22 | globalLogger = logger; | |
10873e11digeff10 years ago | 23 | } |
| 24 | | |
bedf110funknown10 years ago | 25 | /** |
898cb3c6Meena Kunnathur Balakrishnan10 years ago | 26 | * Logs a message. |
bedf110funknown10 years ago | 27 | */ |
898cb3c6Meena Kunnathur Balakrishnan10 years ago | 28 | export function logMessage(message: string, formatMessage: boolean = true) { |
53520386Meena Kunnathur Balakrishnan10 years ago | 29 | globalLogger.logMessage(message, formatMessage); |
898cb3c6Meena Kunnathur Balakrishnan10 years ago | 30 | } |
a61d89c4Meena Kunnathur Balakrishnan10 years ago | 31 | |
898cb3c6Meena Kunnathur Balakrishnan10 years ago | 32 | /** |
| 33 | * Logs an error message. | |
| 34 | */ | |
| 35 | export function logError(error?: any, logStack = true) { | |
| 36 | let errorMessageToLog = LogHelper.getErrorString(error); | |
53520386Meena Kunnathur Balakrishnan10 years ago | 37 | globalLogger.logError(errorMessageToLog, error, logStack); |
3fb37ad5unknown10 years ago | 38 | } |
| 39 | | |
3736c251dlebu10 years ago | 40 | /** |
a289475bMeena Kunnathur Balakrishnan10 years ago | 41 | * Logs a warning message. |
3736c251dlebu10 years ago | 42 | */ |
898cb3c6Meena Kunnathur Balakrishnan10 years ago | 43 | export function logWarning(error?: any, logStack = true) { |
| 44 | Log.logError(error, logStack); | |
a289475bMeena Kunnathur Balakrishnan10 years ago | 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 | */ | |
d206c683Meena Kunnathur Balakrishnan10 years ago | 52 | export function logInternalMessage(logLevel: LogLevel, message: string) { |
a289475bMeena Kunnathur Balakrishnan10 years ago | 53 | if (LogHelper.logLevel >= logLevel) { |
53520386Meena Kunnathur Balakrishnan10 years ago | 54 | globalLogger.logInternalMessage(logLevel, message); |
a289475bMeena Kunnathur Balakrishnan10 years ago | 55 | } |
| 56 | } | |
| 57 | | |
| 58 | /** | |
| 59 | * Logs the status (Start/End) of a command. | |
| 60 | */ | |
898cb3c6Meena Kunnathur Balakrishnan10 years ago | 61 | export function logCommandStatus(command: string, status: CommandStatus) { |
a289475bMeena Kunnathur Balakrishnan10 years ago | 62 | console.assert(status >= CommandStatus.Start && status <= CommandStatus.End, "Unsupported Command Status"); |
| 63 | | |
| 64 | let statusMessage = Log.getCommandStatusString(command, status); | |
53520386Meena Kunnathur Balakrishnan10 years ago | 65 | globalLogger.logMessage(statusMessage); |
3736c251dlebu10 years ago | 66 | } |
| 67 | | |
99e41548Meena Kunnathur Balakrishnan10 years ago | 68 | /** |
| 69 | * Logs a stream data buffer. | |
| 70 | */ | |
| 71 | export function logStreamData(data: Buffer, stream: NodeJS.WritableStream) { | |
| 72 | globalLogger.logStreamData(data, stream); | |
| 73 | } | |
| 74 | | |
cf138e34Meena Kunnathur Balakrishnan10 years ago | 75 | /** |
| 76 | * Brings the target output window to focus. | |
| 77 | */ | |
f1e34747Meena Kunnathur Balakrishnan10 years ago | 78 | export function setFocusOnLogChannel() { |
| 79 | globalLogger.setFocusOnLogChannel(); | |
cf138e34Meena Kunnathur Balakrishnan10 years ago | 80 | } |
| 81 | | |
14a23821digeff10 years ago | 82 | /** |
17161993Meena Kunnathur Balakrishnan10 years ago | 83 | * Logs a message to the console. |
14a23821digeff10 years ago | 84 | */ |
f1a07677Meena Kunnathur Balakrishnan10 years ago | 85 | export function logWithLogger(logger: ILogger, message: string, formatMessage: boolean) { |
| 86 | logger.logMessage(message, formatMessage); | |
17161993Meena Kunnathur Balakrishnan10 years ago | 87 | } |
14a23821digeff10 years ago | 88 | |
898cb3c6Meena Kunnathur Balakrishnan10 years ago | 89 | /** |
| 90 | * Logs a message to the console. | |
| 91 | */ | |
| 92 | export function logToStderr(message: string, formatMessage: boolean = true) { | |
| 93 | new StreamLogger(process.stderr).logMessage(message, formatMessage); | |
| 94 | } | |
| 95 | | |
| 96 | /** | |
| 97 | * Logs a message to the console. | |
| 98 | */ | |
| 99 | export function logToStdout(message: string, formatMessage: boolean = true) { | |
| 100 | new StreamLogger(process.stdout).logMessage(message, formatMessage); | |
| 101 | } | |
| 102 | | |
| 103 | export function getCommandStatusString(command: string, status: CommandStatus) { | |
17161993Meena Kunnathur Balakrishnan10 years ago | 104 | console.assert(status >= CommandStatus.Start && status <= CommandStatus.End, "Unsupported Command Status"); |
| 105 | | |
| 106 | switch (status) { | |
| 107 | case CommandStatus.Start: | |
| 108 | return `Executing command: ${command}`; | |
| 109 | | |
| 110 | case CommandStatus.End: | |
| 111 | return `Finished executing: ${command}`; | |
| 112 | | |
| 113 | default: | |
| 114 | throw new Error("Unsupported command status"); | |
3736c251dlebu10 years ago | 115 | } |
| 116 | } | |
3fb37ad5unknown10 years ago | 117 | } |