microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
0.3.0

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/extension/android/logCatMonitor.ts

85lines · 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;
52f3873ddigeff10 years ago22private _logger: OutputChannelLogger;
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;
52f3873ddigeff10 years ago33this._logger = new OutputChannelLogger(vscode.window.createOutputChannel(`LogCat - ${deviceId}`));
710f8655digeff10 years ago34}
35
36public start(): Q.Promise<void> {
28ce21b5digeff10 years ago37const logCatArguments = this.getLogCatArguments();
c2bf3c4fdigeff10 years ago38const adbParameters = ["-s", this._deviceId, "logcat"].concat(logCatArguments);
39this._logger.logMessage(`Monitoring LogCat for device ${this._deviceId} with arguments: ${logCatArguments}`);
710f8655digeff10 years ago40
9596aa53digeff10 years ago41this._logCatSpawn = new ChildProcess().spawn("adb", adbParameters);
c2bf3c4fdigeff10 years ago42
43/* LogCat has a buffer and prints old messages when first called. To ignore them,
44we won't print messages for the first 0.5 seconds */
45const filter = new ExecutionsFilterBeforeTimestamp(/*delayInSeconds*/ 0.5);
46this._logCatSpawn.stderr.on("data", (data: Buffer) => {
47filter.execute(() => this._logger.logMessage(data.toString(), /*formatMessage*/ false));
48});
49
50this._logCatSpawn.stdout.on("data", (data: Buffer) => {
51filter.execute(() => this._logger.logMessage(data.toString(), /*formatMessage*/ false));
52});
53
54return this._logCatSpawn.outcome.then(
55() =>
56this._logger.logMessage("LogCat monitoring stopped because the process exited."),
57reason => {
58if (!this._logCatSpawn) { // We stopped log cat ourselves
59this._logger.logMessage("LogCat monitoring stopped because the debugging session finished");
60} else {
61return Q.reject<void>(reason); // Unkown error. Pass it up the promise chain
62}
63}).finally(() =>
64this._logCatSpawn = null);
710f8655digeff10 years ago65}
66
67public dispose(): void {
68if (this._logCatSpawn) {
c2bf3c4fdigeff10 years ago69const logCatSpawn = this._logCatSpawn;
710f8655digeff10 years ago70this._logCatSpawn = null;
c2bf3c4fdigeff10 years ago71logCatSpawn.spawnedProcess.kill();
710f8655digeff10 years ago72}
73}
74
28ce21b5digeff10 years ago75private getLogCatArguments(): string[] {
c2bf3c4fdigeff10 years ago76// We use the setting if it's defined, or the defaults if it's not
77return this.isNullOrUndefined(this._userProvidedLogCatArguments) // "" is a valid value, so we can't just if () this
78? LogCatMonitor.DEFAULT_PARAMETERS
79: ("" + this._userProvidedLogCatArguments).split(" "); // Parse string and split into string[]
710f8655digeff10 years ago80}
81
82private isNullOrUndefined(value: any): boolean {
83return typeof value === "undefined" || value === null;
84}
85}