microsoft/vscode-react-native
Publicmirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable
src/extension/android/logCatMonitor.ts
92lines · 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 { ISpawnResult } from "../../common/node/childProcess"; |
| 8 | import { OutputChannelLogger } from "../log/OutputChannelLogger"; |
| 9 | import { ExecutionsFilterBeforeTimestamp } from "../../common/executionsLimiter"; |
| 10 | import { AdbHelper } from "./adb"; |
| 11 | import * as nls from "vscode-nls"; |
| 12 | const localize = nls.loadMessageBundle(); |
| 13 | |
| 14 | /* This class will print the LogCat messages to an Output Channel. The configuration for logcat can be cutomized in |
| 15 | the .vscode/launch.json file by defining a setting named logCatArguments for the configuration being used. The |
| 16 | setting accepts values as: |
| 17 | 1. an array: ["*:S", "ReactNative:V", "ReactNativeJS:V"] |
| 18 | 2. a string: "*:S ReactNative:V ReactNativeJS:V" |
| 19 | Type `adb logcat --help` to see the parameters and usage of logcat |
| 20 | */ |
| 21 | export class LogCatMonitor implements vscode.Disposable { |
| 22 | private static DEFAULT_PARAMETERS = ["*:S", "ReactNative:V", "ReactNativeJS:V"]; |
| 23 | |
| 24 | private _logger: OutputChannelLogger; |
| 25 | |
| 26 | private _deviceId: string; |
| 27 | private _userProvidedLogCatArguments: any; // This is user input, we don't know what's here |
| 28 | |
| 29 | private _logCatSpawn: ISpawnResult | null; |
| 30 | private adbHelper: AdbHelper; |
| 31 | |
| 32 | constructor(deviceId: string, userProvidedLogCatArguments: string, adbHelper: AdbHelper) { |
| 33 | this._deviceId = deviceId; |
| 34 | this._userProvidedLogCatArguments = userProvidedLogCatArguments; |
| 35 | |
| 36 | this._logger = OutputChannelLogger.getChannel(`LogCat - ${deviceId}`); |
| 37 | this.adbHelper = adbHelper; |
| 38 | } |
| 39 | |
| 40 | public start(): Q.Promise<void> { |
| 41 | const logCatArguments = this.getLogCatArguments(); |
| 42 | const adbParameters = ["-s", this._deviceId, "logcat"].concat(logCatArguments); |
| 43 | this._logger.debug(`Monitoring LogCat for device ${this._deviceId} with arguments: ${logCatArguments}`); |
| 44 | |
| 45 | this._logCatSpawn = this.adbHelper.startLogCat(adbParameters); |
| 46 | |
| 47 | /* LogCat has a buffer and prints old messages when first called. To ignore them, |
| 48 | we won't print messages for the first 0.5 seconds */ |
| 49 | const filter = new ExecutionsFilterBeforeTimestamp(/*delayInSeconds*/ 0.5); |
| 50 | this._logCatSpawn.stderr.on("data", (data: Buffer) => { |
| 51 | filter.execute(() => this._logger.info(data.toString())); |
| 52 | }); |
| 53 | |
| 54 | this._logCatSpawn.stdout.on("data", (data: Buffer) => { |
| 55 | filter.execute(() => this._logger.info(data.toString())); |
| 56 | }); |
| 57 | return this._logCatSpawn.outcome.then( |
| 58 | () => |
| 59 | this._logger.info(localize("LogCatMonitoringStoppedBecauseTheProcessExited", "LogCat monitoring stopped because the process exited.")), |
| 60 | (reason) => { |
| 61 | if (!this._logCatSpawn) { // We stopped log cat ourselves |
| 62 | this._logger.info(localize("LogCatMonitoringStoppedBecauseTheDebuggingSessionFinished", "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 | OutputChannelLogger.disposeChannel(this._logger.channelName); |
| 80 | } |
| 81 | |
| 82 | private getLogCatArguments(): string[] { |
| 83 | // We use the setting if it's defined, or the defaults if it's not |
| 84 | return this.isNullOrUndefined(this._userProvidedLogCatArguments) // "" is a valid value, so we can't just if () this |
| 85 | ? LogCatMonitor.DEFAULT_PARAMETERS |
| 86 | : ("" + this._userProvidedLogCatArguments).split(" "); // Parse string and split into string[] |
| 87 | } |
| 88 | |
| 89 | private isNullOrUndefined(value: any): boolean { |
| 90 | return typeof value === "undefined" || value === null; |
| 91 | } |
| 92 | } |
| 93 | |