microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/extension/android/logCatMonitor.ts
84lines · modeblame
710f8655digeff10 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 | | |
| 4 | import * as Q from "q"; | |
c2bf3c4fdigeff10 years ago | 5 | import * as vscode from "vscode"; |
710f8655digeff10 years ago | 6 | |
c2bf3c4fdigeff10 years ago | 7 | import {ChildProcess, ISpawnResult} from "../../common/node/childProcess"; |
| 8 | import {OutputChannelLogger} from "../outputChannelLogger"; | |
| 9 | import {ExecutionsFilterBeforeTimestamp} from "../../common/executionsLimiter"; | |
710f8655digeff10 years ago | 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 | | |
c2bf3c4fdigeff10 years ago | 21 | private _childProcess: ChildProcess; |
| 22 | private _logger = new OutputChannelLogger(vscode.window.createOutputChannel("LogCat")); | |
710f8655digeff10 years ago | 23 | |
c2bf3c4fdigeff10 years ago | 24 | private _deviceId: string; |
| 25 | private _userProvidedLogCatArguments: any; // This is user input, we don't know what's here | |
710f8655digeff10 years ago | 26 | |
| 27 | private _logCatSpawn: ISpawnResult; | |
| 28 | | |
c2bf3c4fdigeff10 years ago | 29 | constructor(deviceId: string, userProvidedLogCatArguments: string, { childProcess = new ChildProcess() } = {}) { |
| 30 | this._deviceId = deviceId; | |
710f8655digeff10 years ago | 31 | this._userProvidedLogCatArguments = userProvidedLogCatArguments; |
c2bf3c4fdigeff10 years ago | 32 | this._childProcess = childProcess; |
710f8655digeff10 years ago | 33 | } |
| 34 | | |
| 35 | public start(): Q.Promise<void> { | |
28ce21b5digeff10 years ago | 36 | const logCatArguments = this.getLogCatArguments(); |
c2bf3c4fdigeff10 years ago | 37 | const adbParameters = ["-s", this._deviceId, "logcat"].concat(logCatArguments); |
| 38 | this._logger.logMessage(`Monitoring LogCat for device ${this._deviceId} with arguments: ${logCatArguments}`); | |
710f8655digeff10 years ago | 39 | |
9596aa53digeff10 years ago | 40 | this._logCatSpawn = new ChildProcess().spawn("adb", adbParameters); |
c2bf3c4fdigeff10 years ago | 41 | |
| 42 | /* LogCat has a buffer and prints old messages when first called. To ignore them, | |
| 43 | we won't print messages for the first 0.5 seconds */ | |
| 44 | const filter = new ExecutionsFilterBeforeTimestamp(/*delayInSeconds*/ 0.5); | |
| 45 | this._logCatSpawn.stderr.on("data", (data: Buffer) => { | |
| 46 | filter.execute(() => this._logger.logMessage(data.toString(), /*formatMessage*/ false)); | |
| 47 | }); | |
| 48 | | |
| 49 | this._logCatSpawn.stdout.on("data", (data: Buffer) => { | |
| 50 | filter.execute(() => this._logger.logMessage(data.toString(), /*formatMessage*/ false)); | |
| 51 | }); | |
| 52 | | |
| 53 | return this._logCatSpawn.outcome.then( | |
| 54 | () => | |
| 55 | this._logger.logMessage("LogCat monitoring stopped because the process exited."), | |
| 56 | reason => { | |
| 57 | if (!this._logCatSpawn) { // We stopped log cat ourselves | |
| 58 | this._logger.logMessage("LogCat monitoring stopped because the debugging session finished"); | |
| 59 | } else { | |
| 60 | return Q.reject<void>(reason); // Unkown error. Pass it up the promise chain | |
| 61 | } | |
| 62 | }).finally(() => | |
| 63 | this._logCatSpawn = null); | |
710f8655digeff10 years ago | 64 | } |
| 65 | | |
| 66 | public dispose(): void { | |
| 67 | if (this._logCatSpawn) { | |
c2bf3c4fdigeff10 years ago | 68 | const logCatSpawn = this._logCatSpawn; |
710f8655digeff10 years ago | 69 | this._logCatSpawn = null; |
c2bf3c4fdigeff10 years ago | 70 | logCatSpawn.spawnedProcess.kill(); |
710f8655digeff10 years ago | 71 | } |
| 72 | } | |
| 73 | | |
28ce21b5digeff10 years ago | 74 | private getLogCatArguments(): string[] { |
c2bf3c4fdigeff10 years ago | 75 | // We use the setting if it's defined, or the defaults if it's not |
| 76 | return this.isNullOrUndefined(this._userProvidedLogCatArguments) // "" is a valid value, so we can't just if () this | |
| 77 | ? LogCatMonitor.DEFAULT_PARAMETERS | |
| 78 | : ("" + this._userProvidedLogCatArguments).split(" "); // Parse string and split into string[] | |
710f8655digeff10 years ago | 79 | } |
| 80 | | |
| 81 | private isNullOrUndefined(value: any): boolean { | |
| 82 | return typeof value === "undefined" || value === null; | |
| 83 | } | |
| 84 | } |