microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
0.6.14

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/extension/android/logCatMonitor.ts

91lines · 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
43e1a996Ruslan Bikkinin7 years ago7import { ISpawnResult } from "../../common/node/childProcess";
0a68f8dbArtem Egorov8 years ago8import { OutputChannelLogger } from "../log/OutputChannelLogger";
474b4b72Dmitry Zinovyev9 years ago9import { ExecutionsFilterBeforeTimestamp } from "../../common/executionsLimiter";
43e1a996Ruslan Bikkinin7 years ago10import { AdbHelper } from "./adb";
710f8655digeff10 years ago11
12/* This class will print the LogCat messages to an Output Channel. The configuration for logcat can be cutomized in
13the .vscode/launch.json file by defining a setting named logCatArguments for the configuration being used. The
14setting accepts values as:
151. an array: ["*:S", "ReactNative:V", "ReactNativeJS:V"]
162. a string: "*:S ReactNative:V ReactNativeJS:V"
17Type `adb logcat --help` to see the parameters and usage of logcat
18*/
19export class LogCatMonitor implements vscode.Disposable {
20private static DEFAULT_PARAMETERS = ["*:S", "ReactNative:V", "ReactNativeJS:V"];
21
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
5c8365a6Artem Egorov8 years ago27private _logCatSpawn: ISpawnResult | null;
43e1a996Ruslan Bikkinin7 years ago28private adbHelper: AdbHelper;
710f8655digeff10 years ago29
43e1a996Ruslan Bikkinin7 years ago30constructor(deviceId: string, userProvidedLogCatArguments: string, adbHelper: AdbHelper) {
c2bf3c4fdigeff10 years ago31this._deviceId = deviceId;
710f8655digeff10 years ago32this._userProvidedLogCatArguments = userProvidedLogCatArguments;
474b4b72Dmitry Zinovyev9 years ago33
0a68f8dbArtem Egorov8 years ago34this._logger = OutputChannelLogger.getChannel(`LogCat - ${deviceId}`);
43e1a996Ruslan Bikkinin7 years ago35this.adbHelper = adbHelper;
710f8655digeff10 years ago36}
37
38public start(): Q.Promise<void> {
28ce21b5digeff10 years ago39const logCatArguments = this.getLogCatArguments();
c2bf3c4fdigeff10 years ago40const adbParameters = ["-s", this._deviceId, "logcat"].concat(logCatArguments);
0a68f8dbArtem Egorov8 years ago41this._logger.debug(`Monitoring LogCat for device ${this._deviceId} with arguments: ${logCatArguments}`);
710f8655digeff10 years ago42
43e1a996Ruslan Bikkinin7 years ago43this._logCatSpawn = this.adbHelper.startLogCat(adbParameters);
c2bf3c4fdigeff10 years ago44
45/* LogCat has a buffer and prints old messages when first called. To ignore them,
46we won't print messages for the first 0.5 seconds */
47const filter = new ExecutionsFilterBeforeTimestamp(/*delayInSeconds*/ 0.5);
48this._logCatSpawn.stderr.on("data", (data: Buffer) => {
0a68f8dbArtem Egorov8 years ago49filter.execute(() => this._logger.info(data.toString()));
c2bf3c4fdigeff10 years ago50});
51
52this._logCatSpawn.stdout.on("data", (data: Buffer) => {
0a68f8dbArtem Egorov8 years ago53filter.execute(() => this._logger.info(data.toString()));
c2bf3c4fdigeff10 years ago54});
55
56return this._logCatSpawn.outcome.then(
57() =>
0a68f8dbArtem Egorov8 years ago58this._logger.info("LogCat monitoring stopped because the process exited."),
5c8365a6Artem Egorov8 years ago59(reason) => {
c2bf3c4fdigeff10 years ago60if (!this._logCatSpawn) { // We stopped log cat ourselves
0a68f8dbArtem Egorov8 years ago61this._logger.info("LogCat monitoring stopped because the debugging session finished");
5c8365a6Artem Egorov8 years ago62return Q.resolve(void 0);
c2bf3c4fdigeff10 years ago63} else {
64return Q.reject<void>(reason); // Unkown error. Pass it up the promise chain
65}
5c8365a6Artem Egorov8 years ago66}).finally(() => {
67this._logCatSpawn = null;
68});
710f8655digeff10 years ago69}
70
71public dispose(): void {
72if (this._logCatSpawn) {
c2bf3c4fdigeff10 years ago73const logCatSpawn = this._logCatSpawn;
710f8655digeff10 years ago74this._logCatSpawn = null;
c2bf3c4fdigeff10 years ago75logCatSpawn.spawnedProcess.kill();
710f8655digeff10 years ago76}
474b4b72Dmitry Zinovyev9 years ago77
0a68f8dbArtem Egorov8 years ago78OutputChannelLogger.disposeChannel(this._logger.channelName);
710f8655digeff10 years ago79}
80
28ce21b5digeff10 years ago81private getLogCatArguments(): string[] {
c2bf3c4fdigeff10 years ago82// We use the setting if it's defined, or the defaults if it's not
83return 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[]
710f8655digeff10 years ago86}
87
88private isNullOrUndefined(value: any): boolean {
89return typeof value === "undefined" || value === null;
90}
91}