microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
0.4.0

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/extension/android/logCatMonitor.ts

103lines · 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
474b4b72Dmitry Zinovyev9 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
474b4b72Dmitry Zinovyev9 years ago29private static loggers: { [loggerName: string]: OutputChannelLogger } = {};
30
c2bf3c4fdigeff10 years ago31constructor(deviceId: string, userProvidedLogCatArguments: string, { childProcess = new ChildProcess() } = {}) {
32this._deviceId = deviceId;
710f8655digeff10 years ago33this._userProvidedLogCatArguments = userProvidedLogCatArguments;
c2bf3c4fdigeff10 years ago34this._childProcess = childProcess;
474b4b72Dmitry Zinovyev9 years ago35
36this._logger = LogCatMonitor.getLogger(`LogCat - ${deviceId}`);
710f8655digeff10 years ago37}
38
39public start(): Q.Promise<void> {
28ce21b5digeff10 years ago40const logCatArguments = this.getLogCatArguments();
c2bf3c4fdigeff10 years ago41const adbParameters = ["-s", this._deviceId, "logcat"].concat(logCatArguments);
42this._logger.logMessage(`Monitoring LogCat for device ${this._deviceId} with arguments: ${logCatArguments}`);
710f8655digeff10 years ago43
9596aa53digeff10 years ago44this._logCatSpawn = new ChildProcess().spawn("adb", adbParameters);
c2bf3c4fdigeff10 years ago45
46/* LogCat has a buffer and prints old messages when first called. To ignore them,
47we won't print messages for the first 0.5 seconds */
48const filter = new ExecutionsFilterBeforeTimestamp(/*delayInSeconds*/ 0.5);
49this._logCatSpawn.stderr.on("data", (data: Buffer) => {
50filter.execute(() => this._logger.logMessage(data.toString(), /*formatMessage*/ false));
51});
52
53this._logCatSpawn.stdout.on("data", (data: Buffer) => {
54filter.execute(() => this._logger.logMessage(data.toString(), /*formatMessage*/ false));
55});
56
57return this._logCatSpawn.outcome.then(
58() =>
59this._logger.logMessage("LogCat monitoring stopped because the process exited."),
60reason => {
61if (!this._logCatSpawn) { // We stopped log cat ourselves
62this._logger.logMessage("LogCat monitoring stopped because the debugging session finished");
63} else {
64return Q.reject<void>(reason); // Unkown error. Pass it up the promise chain
65}
66}).finally(() =>
67this._logCatSpawn = null);
710f8655digeff10 years ago68}
69
70public dispose(): void {
71if (this._logCatSpawn) {
c2bf3c4fdigeff10 years ago72const logCatSpawn = this._logCatSpawn;
710f8655digeff10 years ago73this._logCatSpawn = null;
c2bf3c4fdigeff10 years ago74logCatSpawn.spawnedProcess.kill();
710f8655digeff10 years ago75}
474b4b72Dmitry Zinovyev9 years ago76
77for (let name of Object.keys(LogCatMonitor.loggers)) {
78LogCatMonitor.loggers[name].getOutputChannel().dispose();
79}
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}
474b4b72Dmitry Zinovyev9 years ago92
93/**
94* Fabric method to create new output channels and reuse old
95*/
96private static getLogger(name: string): OutputChannelLogger {
97if (!LogCatMonitor.loggers[name]) {
98LogCatMonitor.loggers[name] = new OutputChannelLogger(vscode.window.createOutputChannel(name));
99}
100LogCatMonitor.loggers[name].getOutputChannel().clear();
101return LogCatMonitor.loggers[name];
102}
710f8655digeff10 years ago103}