microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
0.1.4

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/extension/android/logCatMonitor.ts

84lines · modeblame

710f8655digeff10 years ago1// 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";
c2bf3c4fdigeff10 years ago5import * as vscode from "vscode";
710f8655digeff10 years ago6
c2bf3c4fdigeff10 years ago7import {ChildProcess, ISpawnResult} from "../../common/node/childProcess";
8import {OutputChannelLogger} from "../outputChannelLogger";
9import {ExecutionsFilterBeforeTimestamp} from "../../common/executionsLimiter";
710f8655digeff10 years ago10
11/* This class will print the LogCat messages to an Output Channel. The configuration for logcat can be cutomized in
12the .vscode/launch.json file by defining a setting named logCatArguments for the configuration being used. The
13setting accepts values as:
141. an array: ["*:S", "ReactNative:V", "ReactNativeJS:V"]
152. a string: "*:S ReactNative:V ReactNativeJS:V"
16Type `adb logcat --help` to see the parameters and usage of logcat
17*/
18export class LogCatMonitor implements vscode.Disposable {
19private static DEFAULT_PARAMETERS = ["*:S", "ReactNative:V", "ReactNativeJS:V"];
20
c2bf3c4fdigeff10 years ago21private _childProcess: ChildProcess;
22private _logger = new OutputChannelLogger(vscode.window.createOutputChannel("LogCat"));
710f8655digeff10 years ago23
c2bf3c4fdigeff10 years ago24private _deviceId: string;
25private _userProvidedLogCatArguments: any; // This is user input, we don't know what's here
710f8655digeff10 years ago26
27private _logCatSpawn: ISpawnResult;
28
c2bf3c4fdigeff10 years ago29constructor(deviceId: string, userProvidedLogCatArguments: string, { childProcess = new ChildProcess() } = {}) {
30this._deviceId = deviceId;
710f8655digeff10 years ago31this._userProvidedLogCatArguments = userProvidedLogCatArguments;
c2bf3c4fdigeff10 years ago32this._childProcess = childProcess;
710f8655digeff10 years ago33}
34
35public start(): Q.Promise<void> {
28ce21b5digeff10 years ago36const logCatArguments = this.getLogCatArguments();
c2bf3c4fdigeff10 years ago37const adbParameters = ["-s", this._deviceId, "logcat"].concat(logCatArguments);
38this._logger.logMessage(`Monitoring LogCat for device ${this._deviceId} with arguments: ${logCatArguments}`);
710f8655digeff10 years ago39
9596aa53digeff10 years ago40this._logCatSpawn = new ChildProcess().spawn("adb", adbParameters);
c2bf3c4fdigeff10 years ago41
42/* LogCat has a buffer and prints old messages when first called. To ignore them,
43we won't print messages for the first 0.5 seconds */
44const filter = new ExecutionsFilterBeforeTimestamp(/*delayInSeconds*/ 0.5);
45this._logCatSpawn.stderr.on("data", (data: Buffer) => {
46filter.execute(() => this._logger.logMessage(data.toString(), /*formatMessage*/ false));
47});
48
49this._logCatSpawn.stdout.on("data", (data: Buffer) => {
50filter.execute(() => this._logger.logMessage(data.toString(), /*formatMessage*/ false));
51});
52
53return this._logCatSpawn.outcome.then(
54() =>
55this._logger.logMessage("LogCat monitoring stopped because the process exited."),
56reason => {
57if (!this._logCatSpawn) { // We stopped log cat ourselves
58this._logger.logMessage("LogCat monitoring stopped because the debugging session finished");
59} else {
60return Q.reject<void>(reason); // Unkown error. Pass it up the promise chain
61}
62}).finally(() =>
63this._logCatSpawn = null);
710f8655digeff10 years ago64}
65
66public dispose(): void {
67if (this._logCatSpawn) {
c2bf3c4fdigeff10 years ago68const logCatSpawn = this._logCatSpawn;
710f8655digeff10 years ago69this._logCatSpawn = null;
c2bf3c4fdigeff10 years ago70logCatSpawn.spawnedProcess.kill();
710f8655digeff10 years ago71}
72}
73
28ce21b5digeff10 years ago74private getLogCatArguments(): string[] {
c2bf3c4fdigeff10 years ago75// We use the setting if it's defined, or the defaults if it's not
76return this.isNullOrUndefined(this._userProvidedLogCatArguments) // "" is a valid value, so we can't just if () this
77? LogCatMonitor.DEFAULT_PARAMETERS
78: ("" + this._userProvidedLogCatArguments).split(" "); // Parse string and split into string[]
710f8655digeff10 years ago79}
80
81private isNullOrUndefined(value: any): boolean {
82return typeof value === "undefined" || value === null;
83}
84}