microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/extension/android/logCatMonitor.ts
92lines · 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 | |
db6fd42aRuslan Bikkinin7 years ago | 7 | import { ISpawnResult } from "../../common/node/childProcess"; |
0a68f8dbArtem Egorov8 years ago | 8 | import { OutputChannelLogger } from "../log/OutputChannelLogger"; |
474b4b72Dmitry Zinovyev9 years ago | 9 | import { ExecutionsFilterBeforeTimestamp } from "../../common/executionsLimiter"; |
db6fd42aRuslan Bikkinin7 years ago | 10 | import { AdbHelper } from "./adb"; |
d7d405aeYuri Skorokhodov7 years ago | 11 | import * as nls from "vscode-nls"; |
| 12 | const localize = nls.loadMessageBundle(); | |
710f8655digeff10 years ago | 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 | | |
52f3873ddigeff10 years ago | 24 | private _logger: OutputChannelLogger; |
710f8655digeff10 years ago | 25 | |
c2bf3c4fdigeff10 years ago | 26 | private _deviceId: string; |
| 27 | private _userProvidedLogCatArguments: any; // This is user input, we don't know what's here | |
710f8655digeff10 years ago | 28 | |
5c8365a6Artem Egorov8 years ago | 29 | private _logCatSpawn: ISpawnResult | null; |
db6fd42aRuslan Bikkinin7 years ago | 30 | private adbHelper: AdbHelper; |
710f8655digeff10 years ago | 31 | |
db6fd42aRuslan Bikkinin7 years ago | 32 | constructor(deviceId: string, userProvidedLogCatArguments: string, adbHelper: AdbHelper) { |
c2bf3c4fdigeff10 years ago | 33 | this._deviceId = deviceId; |
710f8655digeff10 years ago | 34 | this._userProvidedLogCatArguments = userProvidedLogCatArguments; |
474b4b72Dmitry Zinovyev9 years ago | 35 | |
0a68f8dbArtem Egorov8 years ago | 36 | this._logger = OutputChannelLogger.getChannel(`LogCat - ${deviceId}`); |
db6fd42aRuslan Bikkinin7 years ago | 37 | this.adbHelper = adbHelper; |
710f8655digeff10 years ago | 38 | } |
| 39 | | |
| 40 | public start(): Q.Promise<void> { | |
28ce21b5digeff10 years ago | 41 | const logCatArguments = this.getLogCatArguments(); |
c2bf3c4fdigeff10 years ago | 42 | const adbParameters = ["-s", this._deviceId, "logcat"].concat(logCatArguments); |
0a68f8dbArtem Egorov8 years ago | 43 | this._logger.debug(`Monitoring LogCat for device ${this._deviceId} with arguments: ${logCatArguments}`); |
710f8655digeff10 years ago | 44 | |
db6fd42aRuslan Bikkinin7 years ago | 45 | this._logCatSpawn = this.adbHelper.startLogCat(adbParameters); |
c2bf3c4fdigeff10 years ago | 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) => { | |
0a68f8dbArtem Egorov8 years ago | 51 | filter.execute(() => this._logger.info(data.toString())); |
c2bf3c4fdigeff10 years ago | 52 | }); |
| 53 | | |
| 54 | this._logCatSpawn.stdout.on("data", (data: Buffer) => { | |
0a68f8dbArtem Egorov8 years ago | 55 | filter.execute(() => this._logger.info(data.toString())); |
c2bf3c4fdigeff10 years ago | 56 | }); |
| 57 | return this._logCatSpawn.outcome.then( | |
| 58 | () => | |
d7d405aeYuri Skorokhodov7 years ago | 59 | this._logger.info(localize("LogCatMonitoringStoppedBecauseTheProcessExited", "LogCat monitoring stopped because the process exited.")), |
5c8365a6Artem Egorov8 years ago | 60 | (reason) => { |
c2bf3c4fdigeff10 years ago | 61 | if (!this._logCatSpawn) { // We stopped log cat ourselves |
d7d405aeYuri Skorokhodov7 years ago | 62 | this._logger.info(localize("LogCatMonitoringStoppedBecauseTheDebuggingSessionFinished", "LogCat monitoring stopped because the debugging session finished")); |
5c8365a6Artem Egorov8 years ago | 63 | return Q.resolve(void 0); |
c2bf3c4fdigeff10 years ago | 64 | } else { |
| 65 | return Q.reject<void>(reason); // Unkown error. Pass it up the promise chain | |
| 66 | } | |
5c8365a6Artem Egorov8 years ago | 67 | }).finally(() => { |
| 68 | this._logCatSpawn = null; | |
| 69 | }); | |
710f8655digeff10 years ago | 70 | } |
| 71 | | |
| 72 | public dispose(): void { | |
| 73 | if (this._logCatSpawn) { | |
c2bf3c4fdigeff10 years ago | 74 | const logCatSpawn = this._logCatSpawn; |
710f8655digeff10 years ago | 75 | this._logCatSpawn = null; |
c2bf3c4fdigeff10 years ago | 76 | logCatSpawn.spawnedProcess.kill(); |
710f8655digeff10 years ago | 77 | } |
474b4b72Dmitry Zinovyev9 years ago | 78 | |
0a68f8dbArtem Egorov8 years ago | 79 | OutputChannelLogger.disposeChannel(this._logger.channelName); |
710f8655digeff10 years ago | 80 | } |
| 81 | | |
28ce21b5digeff10 years ago | 82 | private getLogCatArguments(): string[] { |
c2bf3c4fdigeff10 years ago | 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[] | |
710f8655digeff10 years ago | 87 | } |
| 88 | | |
| 89 | private isNullOrUndefined(value: any): boolean { | |
| 90 | return typeof value === "undefined" || value === null; | |
| 91 | } | |
| 92 | } |