microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
ff70c05a6c821e99785e3f91dca1daf27094e4fd

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/extension/android/logCatMonitor.ts

93lines · modecode

1// 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";
5import * as vscode from "vscode";
6
7import { ISpawnResult } from "../../common/node/childProcess";
8import { OutputChannelLogger } from "../log/OutputChannelLogger";
9import { ExecutionsFilterBeforeTimestamp } from "../../common/executionsLimiter";
10import { AdbHelper } from "./adb";
11import * as nls from "vscode-nls";
12nls.config({ messageFormat: nls.MessageFormat.bundle, bundleFormat: nls.BundleFormat.standalone })();
13const localize = nls.loadMessageBundle();
14
15/* This class will print the LogCat messages to an Output Channel. The configuration for logcat can be cutomized in
16 the .vscode/launch.json file by defining a setting named logCatArguments for the configuration being used. The
17 setting accepts values as:
18 1. an array: ["*:S", "ReactNative:V", "ReactNativeJS:V"]
19 2. a string: "*:S ReactNative:V ReactNativeJS:V"
20 Type `adb logcat --help` to see the parameters and usage of logcat
21*/
22export class LogCatMonitor implements vscode.Disposable {
23 private static DEFAULT_PARAMETERS = ["*:S", "ReactNative:V", "ReactNativeJS:V"];
24
25 private _logger: OutputChannelLogger;
26
27 private _deviceId: string;
28 private _userProvidedLogCatArguments: any; // This is user input, we don't know what's here
29
30 private _logCatSpawn: ISpawnResult | null;
31 private adbHelper: AdbHelper;
32
33 constructor(deviceId: string, userProvidedLogCatArguments: string, adbHelper: AdbHelper) {
34 this._deviceId = deviceId;
35 this._userProvidedLogCatArguments = userProvidedLogCatArguments;
36
37 this._logger = OutputChannelLogger.getChannel(`LogCat - ${deviceId}`);
38 this.adbHelper = adbHelper;
39 }
40
41 public start(): Q.Promise<void> {
42 const logCatArguments = this.getLogCatArguments();
43 const adbParameters = ["-s", this._deviceId, "logcat"].concat(logCatArguments);
44 this._logger.debug(`Monitoring LogCat for device ${this._deviceId} with arguments: ${logCatArguments}`);
45
46 this._logCatSpawn = this.adbHelper.startLogCat(adbParameters);
47
48 /* LogCat has a buffer and prints old messages when first called. To ignore them,
49 we won't print messages for the first 0.5 seconds */
50 const filter = new ExecutionsFilterBeforeTimestamp(/*delayInSeconds*/ 0.5);
51 this._logCatSpawn.stderr.on("data", (data: Buffer) => {
52 filter.execute(() => this._logger.info(data.toString()));
53 });
54
55 this._logCatSpawn.stdout.on("data", (data: Buffer) => {
56 filter.execute(() => this._logger.info(data.toString()));
57 });
58 return this._logCatSpawn.outcome.then(
59 () =>
60 this._logger.info(localize("LogCatMonitoringStoppedBecauseTheProcessExited", "LogCat monitoring stopped because the process exited.")),
61 (reason) => {
62 if (!this._logCatSpawn) { // We stopped log cat ourselves
63 this._logger.info(localize("LogCatMonitoringStoppedBecauseTheDebuggingSessionFinished", "LogCat monitoring stopped because the debugging session finished"));
64 return Q.resolve(void 0);
65 } else {
66 return Q.reject<void>(reason); // Unkown error. Pass it up the promise chain
67 }
68 }).finally(() => {
69 this._logCatSpawn = null;
70 });
71 }
72
73 public dispose(): void {
74 if (this._logCatSpawn) {
75 const logCatSpawn = this._logCatSpawn;
76 this._logCatSpawn = null;
77 logCatSpawn.spawnedProcess.kill();
78 }
79
80 OutputChannelLogger.disposeChannel(this._logger.channelName);
81 }
82
83 private getLogCatArguments(): string[] {
84 // We use the setting if it's defined, or the defaults if it's not
85 return this.isNullOrUndefined(this._userProvidedLogCatArguments) // "" is a valid value, so we can't just if () this
86 ? LogCatMonitor.DEFAULT_PARAMETERS
87 : ("" + this._userProvidedLogCatArguments).split(" "); // Parse string and split into string[]
88 }
89
90 private isNullOrUndefined(value: any): boolean {
91 return typeof value === "undefined" || value === null;
92 }
93}