microsoft/vscode-react-native

Public

mirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
dev/v-peq/removeNode10TodoComments

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/extension/android/logCatMonitor.ts

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