microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/extension/android/logCatMonitor.ts
105lines · 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 | import * as Q from "q"; |
| 5 | import * as vscode from "vscode"; |
| 6 | |
| 7 | import { ChildProcess, ISpawnResult } from "../../common/node/childProcess"; |
| 8 | import { OutputChannelLogger } from "../outputChannelLogger"; |
| 9 | import { ExecutionsFilterBeforeTimestamp } from "../../common/executionsLimiter"; |
| 10 | |
| 11 | /* This class will print the LogCat messages to an Output Channel. The configuration for logcat can be cutomized in |
| 12 | the .vscode/launch.json file by defining a setting named logCatArguments for the configuration being used. The |
| 13 | setting accepts values as: |
| 14 | 1. an array: ["*:S", "ReactNative:V", "ReactNativeJS:V"] |
| 15 | 2. a string: "*:S ReactNative:V ReactNativeJS:V" |
| 16 | Type `adb logcat --help` to see the parameters and usage of logcat |
| 17 | */ |
| 18 | export class LogCatMonitor implements vscode.Disposable { |
| 19 | private static DEFAULT_PARAMETERS = ["*:S", "ReactNative:V", "ReactNativeJS:V"]; |
| 20 | |
| 21 | private _childProcess: ChildProcess; |
| 22 | private _logger: OutputChannelLogger; |
| 23 | |
| 24 | private _deviceId: string; |
| 25 | private _userProvidedLogCatArguments: any; // This is user input, we don't know what's here |
| 26 | |
| 27 | private _logCatSpawn: ISpawnResult | null; |
| 28 | |
| 29 | private static loggers: { [loggerName: string]: OutputChannelLogger } = {}; |
| 30 | |
| 31 | constructor(deviceId: string, userProvidedLogCatArguments: string, { childProcess = new ChildProcess() } = {}) { |
| 32 | this._deviceId = deviceId; |
| 33 | this._userProvidedLogCatArguments = userProvidedLogCatArguments; |
| 34 | this._childProcess = childProcess; |
| 35 | |
| 36 | this._logger = LogCatMonitor.getLogger(`LogCat - ${deviceId}`); |
| 37 | } |
| 38 | |
| 39 | public start(): Q.Promise<void> { |
| 40 | const logCatArguments = this.getLogCatArguments(); |
| 41 | const adbParameters = ["-s", this._deviceId, "logcat"].concat(logCatArguments); |
| 42 | this._logger.logMessage(`Monitoring LogCat for device ${this._deviceId} with arguments: ${logCatArguments}`); |
| 43 | |
| 44 | this._logCatSpawn = new ChildProcess().spawn("adb", adbParameters); |
| 45 | |
| 46 | /* LogCat has a buffer and prints old messages when first called. To ignore them, |
| 47 | we won't print messages for the first 0.5 seconds */ |
| 48 | const filter = new ExecutionsFilterBeforeTimestamp(/*delayInSeconds*/ 0.5); |
| 49 | this._logCatSpawn.stderr.on("data", (data: Buffer) => { |
| 50 | filter.execute(() => this._logger.logMessage(data.toString(), /*formatMessage*/ false)); |
| 51 | }); |
| 52 | |
| 53 | this._logCatSpawn.stdout.on("data", (data: Buffer) => { |
| 54 | filter.execute(() => this._logger.logMessage(data.toString(), /*formatMessage*/ false)); |
| 55 | }); |
| 56 | |
| 57 | return this._logCatSpawn.outcome.then( |
| 58 | () => |
| 59 | this._logger.logMessage("LogCat monitoring stopped because the process exited."), |
| 60 | (reason) => { |
| 61 | if (!this._logCatSpawn) { // We stopped log cat ourselves |
| 62 | this._logger.logMessage("LogCat monitoring stopped because the debugging session finished"); |
| 63 | return Q.resolve(void 0); |
| 64 | } else { |
| 65 | return Q.reject<void>(reason); // Unkown error. Pass it up the promise chain |
| 66 | } |
| 67 | }).finally(() => { |
| 68 | this._logCatSpawn = null; |
| 69 | }); |
| 70 | } |
| 71 | |
| 72 | public dispose(): void { |
| 73 | if (this._logCatSpawn) { |
| 74 | const logCatSpawn = this._logCatSpawn; |
| 75 | this._logCatSpawn = null; |
| 76 | logCatSpawn.spawnedProcess.kill(); |
| 77 | } |
| 78 | |
| 79 | for (let name of Object.keys(LogCatMonitor.loggers)) { |
| 80 | LogCatMonitor.loggers[name].getOutputChannel().dispose(); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | private getLogCatArguments(): string[] { |
| 85 | // We use the setting if it's defined, or the defaults if it's not |
| 86 | return this.isNullOrUndefined(this._userProvidedLogCatArguments) // "" is a valid value, so we can't just if () this |
| 87 | ? LogCatMonitor.DEFAULT_PARAMETERS |
| 88 | : ("" + this._userProvidedLogCatArguments).split(" "); // Parse string and split into string[] |
| 89 | } |
| 90 | |
| 91 | private isNullOrUndefined(value: any): boolean { |
| 92 | return typeof value === "undefined" || value === null; |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Fabric method to create new output channels and reuse old |
| 97 | */ |
| 98 | private static getLogger(name: string): OutputChannelLogger { |
| 99 | if (!LogCatMonitor.loggers[name]) { |
| 100 | LogCatMonitor.loggers[name] = new OutputChannelLogger(vscode.window.createOutputChannel(name)); |
| 101 | } |
| 102 | LogCatMonitor.loggers[name].getOutputChannel().clear(); |
| 103 | return LogCatMonitor.loggers[name]; |
| 104 | } |
| 105 | } |