microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/extension/android/logCatMonitor.ts
111lines · 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 | | |
c2bf3c4fdigeff10 years ago | 4 | import * as vscode from "vscode"; |
710f8655digeff10 years ago | 5 | |
09f6024fHeniker4 years ago | 6 | import * as nls from "vscode-nls"; |
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"; |
09f6024fHeniker4 years ago | 11 | |
34472878RedMickey5 years ago | 12 | nls.config({ |
| 13 | messageFormat: nls.MessageFormat.bundle, | |
| 14 | bundleFormat: nls.BundleFormat.standalone, | |
| 15 | })(); | |
d7d405aeYuri Skorokhodov7 years ago | 16 | const localize = nls.loadMessageBundle(); |
710f8655digeff10 years ago | 17 | |
| 18 | /* This class will print the LogCat messages to an Output Channel. The configuration for logcat can be cutomized in | |
| 19 | the .vscode/launch.json file by defining a setting named logCatArguments for the configuration being used. The | |
| 20 | setting accepts values as: | |
| 21 | 1. an array: ["*:S", "ReactNative:V", "ReactNativeJS:V"] | |
| 22 | 2. a string: "*:S ReactNative:V ReactNativeJS:V" | |
| 23 | Type `adb logcat --help` to see the parameters and usage of logcat | |
| 24 | */ | |
| 25 | export class LogCatMonitor implements vscode.Disposable { | |
| 26 | private static DEFAULT_PARAMETERS = ["*:S", "ReactNative:V", "ReactNativeJS:V"]; | |
| 27 | | |
52f3873ddigeff10 years ago | 28 | private _logger: OutputChannelLogger; |
710f8655digeff10 years ago | 29 | |
c2bf3c4fdigeff10 years ago | 30 | private _userProvidedLogCatArguments: any; // This is user input, we don't know what's here |
710f8655digeff10 years ago | 31 | |
5c8365a6Artem Egorov8 years ago | 32 | private _logCatSpawn: ISpawnResult | null; |
db6fd42aRuslan Bikkinin7 years ago | 33 | private adbHelper: AdbHelper; |
8df5011eYuri Skorokhodov5 years ago | 34 | public deviceId: string; |
710f8655digeff10 years ago | 35 | |
8df5011eYuri Skorokhodov5 years ago | 36 | constructor(deviceId: string, adbHelper: AdbHelper, userProvidedLogCatArguments?: string[]) { |
| 37 | this.deviceId = deviceId; | |
710f8655digeff10 years ago | 38 | this._userProvidedLogCatArguments = userProvidedLogCatArguments; |
474b4b72Dmitry Zinovyev9 years ago | 39 | |
0a68f8dbArtem Egorov8 years ago | 40 | this._logger = OutputChannelLogger.getChannel(`LogCat - ${deviceId}`); |
db6fd42aRuslan Bikkinin7 years ago | 41 | this.adbHelper = adbHelper; |
710f8655digeff10 years ago | 42 | } |
| 43 | | |
0d77292aJiglioNero4 years ago | 44 | public async start(): Promise<void> { |
28ce21b5digeff10 years ago | 45 | const logCatArguments = this.getLogCatArguments(); |
8df5011eYuri Skorokhodov5 years ago | 46 | const adbParameters = ["-s", this.deviceId, "logcat"].concat(logCatArguments); |
34472878RedMickey5 years ago | 47 | this._logger.debug( |
09f6024fHeniker4 years ago | 48 | `Monitoring LogCat for device ${this.deviceId} with arguments: ${String( |
| 49 | logCatArguments, | |
| 50 | )}`, | |
34472878RedMickey5 years ago | 51 | ); |
710f8655digeff10 years ago | 52 | |
db6fd42aRuslan Bikkinin7 years ago | 53 | this._logCatSpawn = this.adbHelper.startLogCat(adbParameters); |
c2bf3c4fdigeff10 years ago | 54 | |
| 55 | /* LogCat has a buffer and prints old messages when first called. To ignore them, | |
| 56 | we won't print messages for the first 0.5 seconds */ | |
09f6024fHeniker4 years ago | 57 | const filter = new ExecutionsFilterBeforeTimestamp(/* delayInSeconds*/ 0.5); |
c2bf3c4fdigeff10 years ago | 58 | this._logCatSpawn.stderr.on("data", (data: Buffer) => { |
0a68f8dbArtem Egorov8 years ago | 59 | filter.execute(() => this._logger.info(data.toString())); |
c2bf3c4fdigeff10 years ago | 60 | }); |
| 61 | | |
| 62 | this._logCatSpawn.stdout.on("data", (data: Buffer) => { | |
0a68f8dbArtem Egorov8 years ago | 63 | filter.execute(() => this._logger.info(data.toString())); |
c2bf3c4fdigeff10 years ago | 64 | }); |
0d77292aJiglioNero4 years ago | 65 | |
| 66 | try { | |
| 67 | await this._logCatSpawn.outcome; | |
| 68 | this._logger.info( | |
| 69 | localize( | |
| 70 | "LogCatMonitoringStoppedBecauseTheProcessExited", | |
| 71 | "LogCat monitoring stopped because the process exited.", | |
| 72 | ), | |
| 73 | ); | |
| 74 | } catch (error) { | |
| 75 | if (!this._logCatSpawn) { | |
| 76 | // We stopped log cat ourselves | |
| 77 | this._logger.info( | |
| 78 | localize( | |
| 79 | "LogCatMonitoringStoppedBecauseTheDebuggingSessionFinished", | |
| 80 | "LogCat monitoring stopped because the debugging session finished", | |
34472878RedMickey5 years ago | 81 | ), |
0d77292aJiglioNero4 years ago | 82 | ); |
| 83 | } else { | |
| 84 | throw error; // Unknown error. Pass it up the promise chain | |
| 85 | } | |
| 86 | } finally { | |
| 87 | this._logCatSpawn = null; | |
| 88 | } | |
710f8655digeff10 years ago | 89 | } |
| 90 | | |
| 91 | public dispose(): void { | |
| 92 | if (this._logCatSpawn) { | |
c2bf3c4fdigeff10 years ago | 93 | const logCatSpawn = this._logCatSpawn; |
710f8655digeff10 years ago | 94 | this._logCatSpawn = null; |
c2bf3c4fdigeff10 years ago | 95 | logCatSpawn.spawnedProcess.kill(); |
710f8655digeff10 years ago | 96 | } |
474b4b72Dmitry Zinovyev9 years ago | 97 | |
0a68f8dbArtem Egorov8 years ago | 98 | OutputChannelLogger.disposeChannel(this._logger.channelName); |
710f8655digeff10 years ago | 99 | } |
| 100 | | |
28ce21b5digeff10 years ago | 101 | private getLogCatArguments(): string[] { |
c2bf3c4fdigeff10 years ago | 102 | // We use the setting if it's defined, or the defaults if it's not |
| 103 | return this.isNullOrUndefined(this._userProvidedLogCatArguments) // "" is a valid value, so we can't just if () this | |
| 104 | ? LogCatMonitor.DEFAULT_PARAMETERS | |
09f6024fHeniker4 years ago | 105 | : String(this._userProvidedLogCatArguments).split(" "); // Parse string and split into string[] |
710f8655digeff10 years ago | 106 | } |
| 107 | | |
| 108 | private isNullOrUndefined(value: any): boolean { | |
| 109 | return typeof value === "undefined" || value === null; | |
| 110 | } | |
| 111 | } |