microsoft/vscode-react-native

Public

mirrored from https://github.com/microsoft/vscode-react-nativeAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
fcfec28246e15af16c364bf72e0a29eb79219873

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

src/extension/android/logCatMonitor.ts

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