microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
0.17.0

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/extension/android/logCatMonitor.ts

92lines · 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
db6fd42aRuslan Bikkinin7 years ago7import { ISpawnResult } from "../../common/node/childProcess";
0a68f8dbArtem Egorov8 years ago8import { OutputChannelLogger } from "../log/OutputChannelLogger";
474b4b72Dmitry Zinovyev9 years ago9import { ExecutionsFilterBeforeTimestamp } from "../../common/executionsLimiter";
db6fd42aRuslan Bikkinin7 years ago10import { AdbHelper } from "./adb";
d7d405aeYuri Skorokhodov7 years ago11import * as nls from "vscode-nls";
12const localize = nls.loadMessageBundle();
710f8655digeff10 years ago13
14/* This class will print the LogCat messages to an Output Channel. The configuration for logcat can be cutomized in
15the .vscode/launch.json file by defining a setting named logCatArguments for the configuration being used. The
16setting accepts values as:
171. an array: ["*:S", "ReactNative:V", "ReactNativeJS:V"]
182. a string: "*:S ReactNative:V ReactNativeJS:V"
19Type `adb logcat --help` to see the parameters and usage of logcat
20*/
21export class LogCatMonitor implements vscode.Disposable {
22private static DEFAULT_PARAMETERS = ["*:S", "ReactNative:V", "ReactNativeJS:V"];
23
52f3873ddigeff10 years ago24private _logger: OutputChannelLogger;
710f8655digeff10 years ago25
c2bf3c4fdigeff10 years ago26private _deviceId: string;
27private _userProvidedLogCatArguments: any; // This is user input, we don't know what's here
710f8655digeff10 years ago28
5c8365a6Artem Egorov8 years ago29private _logCatSpawn: ISpawnResult | null;
db6fd42aRuslan Bikkinin7 years ago30private adbHelper: AdbHelper;
710f8655digeff10 years ago31
db6fd42aRuslan Bikkinin7 years ago32constructor(deviceId: string, userProvidedLogCatArguments: string, adbHelper: AdbHelper) {
c2bf3c4fdigeff10 years ago33this._deviceId = deviceId;
710f8655digeff10 years ago34this._userProvidedLogCatArguments = userProvidedLogCatArguments;
474b4b72Dmitry Zinovyev9 years ago35
0a68f8dbArtem Egorov8 years ago36this._logger = OutputChannelLogger.getChannel(`LogCat - ${deviceId}`);
db6fd42aRuslan Bikkinin7 years ago37this.adbHelper = adbHelper;
710f8655digeff10 years ago38}
39
40public start(): Q.Promise<void> {
28ce21b5digeff10 years ago41const logCatArguments = this.getLogCatArguments();
c2bf3c4fdigeff10 years ago42const adbParameters = ["-s", this._deviceId, "logcat"].concat(logCatArguments);
0a68f8dbArtem Egorov8 years ago43this._logger.debug(`Monitoring LogCat for device ${this._deviceId} with arguments: ${logCatArguments}`);
710f8655digeff10 years ago44
db6fd42aRuslan Bikkinin7 years ago45this._logCatSpawn = this.adbHelper.startLogCat(adbParameters);
c2bf3c4fdigeff10 years ago46
47/* LogCat has a buffer and prints old messages when first called. To ignore them,
48we won't print messages for the first 0.5 seconds */
49const filter = new ExecutionsFilterBeforeTimestamp(/*delayInSeconds*/ 0.5);
50this._logCatSpawn.stderr.on("data", (data: Buffer) => {
0a68f8dbArtem Egorov8 years ago51filter.execute(() => this._logger.info(data.toString()));
c2bf3c4fdigeff10 years ago52});
53
54this._logCatSpawn.stdout.on("data", (data: Buffer) => {
0a68f8dbArtem Egorov8 years ago55filter.execute(() => this._logger.info(data.toString()));
c2bf3c4fdigeff10 years ago56});
57return this._logCatSpawn.outcome.then(
58() =>
d7d405aeYuri Skorokhodov7 years ago59this._logger.info(localize("LogCatMonitoringStoppedBecauseTheProcessExited", "LogCat monitoring stopped because the process exited.")),
5c8365a6Artem Egorov8 years ago60(reason) => {
c2bf3c4fdigeff10 years ago61if (!this._logCatSpawn) { // We stopped log cat ourselves
d7d405aeYuri Skorokhodov7 years ago62this._logger.info(localize("LogCatMonitoringStoppedBecauseTheDebuggingSessionFinished", "LogCat monitoring stopped because the debugging session finished"));
5c8365a6Artem Egorov8 years ago63return Q.resolve(void 0);
c2bf3c4fdigeff10 years ago64} else {
65return Q.reject<void>(reason); // Unkown error. Pass it up the promise chain
66}
5c8365a6Artem Egorov8 years ago67}).finally(() => {
68this._logCatSpawn = null;
69});
710f8655digeff10 years ago70}
71
72public dispose(): void {
73if (this._logCatSpawn) {
c2bf3c4fdigeff10 years ago74const logCatSpawn = this._logCatSpawn;
710f8655digeff10 years ago75this._logCatSpawn = null;
c2bf3c4fdigeff10 years ago76logCatSpawn.spawnedProcess.kill();
710f8655digeff10 years ago77}
474b4b72Dmitry Zinovyev9 years ago78
0a68f8dbArtem Egorov8 years ago79OutputChannelLogger.disposeChannel(this._logger.channelName);
710f8655digeff10 years ago80}
81
28ce21b5digeff10 years ago82private getLogCatArguments(): string[] {
c2bf3c4fdigeff10 years ago83// We use the setting if it's defined, or the defaults if it's not
84return this.isNullOrUndefined(this._userProvidedLogCatArguments) // "" is a valid value, so we can't just if () this
85? LogCatMonitor.DEFAULT_PARAMETERS
86: ("" + this._userProvidedLogCatArguments).split(" "); // Parse string and split into string[]
710f8655digeff10 years ago87}
88
89private isNullOrUndefined(value: any): boolean {
90return typeof value === "undefined" || value === null;
91}
92}