microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
8df5011e47ada44be3951868ba32e5fae98f48bb

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 vscode from "vscode";
5
6import { ISpawnResult } from "../../common/node/childProcess";
7import { OutputChannelLogger } from "../log/OutputChannelLogger";
8import { ExecutionsFilterBeforeTimestamp } from "../../common/executionsLimiter";
9import { AdbHelper } from "./adb";
10import * as nls from "vscode-nls";
11nls.config({ messageFormat: nls.MessageFormat.bundle, bundleFormat: nls.BundleFormat.standalone })();
12const localize = nls.loadMessageBundle();
13
14/* This class will print the LogCat messages to an Output Channel. The configuration for logcat can be cutomized in
15 the .vscode/launch.json file by defining a setting named logCatArguments for the configuration being used. The
16 setting accepts values as:
17 1. an array: ["*:S", "ReactNative:V", "ReactNativeJS:V"]
18 2. a string: "*:S ReactNative:V ReactNativeJS:V"
19 Type `adb logcat --help` to see the parameters and usage of logcat
20*/
21export class LogCatMonitor implements vscode.Disposable {
22 private static DEFAULT_PARAMETERS = ["*:S", "ReactNative:V", "ReactNativeJS:V"];
23
24 private _logger: OutputChannelLogger;
25
26
27 private _userProvidedLogCatArguments: any; // This is user input, we don't know what's here
28
29 private _logCatSpawn: ISpawnResult | null;
30 private adbHelper: AdbHelper;
31 public deviceId: string;
32
33 constructor(deviceId: string, adbHelper: AdbHelper, userProvidedLogCatArguments?: string[]) {
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(): 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 Promise.resolve();
65 } else {
66 return Promise.reject(reason); // Unknown 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}
94