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