microsoft/vscode-react-native

Public

mirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
0.5.1

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/extension/android/logCatMonitor.ts

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