microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
fix-ts-error1

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/extension/android/logCatMonitor.ts

111lines · 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
c2bf3c4fdigeff10 years ago4import * as vscode from "vscode";
710f8655digeff10 years ago5
09f6024fHeniker4 years ago6import * as nls from "vscode-nls";
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";
09f6024fHeniker4 years ago11
34472878RedMickey5 years ago12nls.config({
13messageFormat: nls.MessageFormat.bundle,
14bundleFormat: nls.BundleFormat.standalone,
15})();
d7d405aeYuri Skorokhodov7 years ago16const localize = nls.loadMessageBundle();
710f8655digeff10 years ago17
18/* This class will print the LogCat messages to an Output Channel. The configuration for logcat can be cutomized in
19the .vscode/launch.json file by defining a setting named logCatArguments for the configuration being used. The
20setting accepts values as:
211. an array: ["*:S", "ReactNative:V", "ReactNativeJS:V"]
222. a string: "*:S ReactNative:V ReactNativeJS:V"
23Type `adb logcat --help` to see the parameters and usage of logcat
24*/
25export class LogCatMonitor implements vscode.Disposable {
26private static DEFAULT_PARAMETERS = ["*:S", "ReactNative:V", "ReactNativeJS:V"];
27
52f3873ddigeff10 years ago28private _logger: OutputChannelLogger;
710f8655digeff10 years ago29
c2bf3c4fdigeff10 years ago30private _userProvidedLogCatArguments: any; // This is user input, we don't know what's here
710f8655digeff10 years ago31
5c8365a6Artem Egorov8 years ago32private _logCatSpawn: ISpawnResult | null;
db6fd42aRuslan Bikkinin7 years ago33private adbHelper: AdbHelper;
8df5011eYuri Skorokhodov5 years ago34public deviceId: string;
710f8655digeff10 years ago35
8df5011eYuri Skorokhodov5 years ago36constructor(deviceId: string, adbHelper: AdbHelper, userProvidedLogCatArguments?: string[]) {
37this.deviceId = deviceId;
710f8655digeff10 years ago38this._userProvidedLogCatArguments = userProvidedLogCatArguments;
474b4b72Dmitry Zinovyev9 years ago39
0a68f8dbArtem Egorov8 years ago40this._logger = OutputChannelLogger.getChannel(`LogCat - ${deviceId}`);
db6fd42aRuslan Bikkinin7 years ago41this.adbHelper = adbHelper;
710f8655digeff10 years ago42}
43
0d77292aJiglioNero4 years ago44public async start(): Promise<void> {
28ce21b5digeff10 years ago45const logCatArguments = this.getLogCatArguments();
8df5011eYuri Skorokhodov5 years ago46const adbParameters = ["-s", this.deviceId, "logcat"].concat(logCatArguments);
34472878RedMickey5 years ago47this._logger.debug(
09f6024fHeniker4 years ago48`Monitoring LogCat for device ${this.deviceId} with arguments: ${String(
49logCatArguments,
50)}`,
34472878RedMickey5 years ago51);
710f8655digeff10 years ago52
db6fd42aRuslan Bikkinin7 years ago53this._logCatSpawn = this.adbHelper.startLogCat(adbParameters);
c2bf3c4fdigeff10 years ago54
55/* LogCat has a buffer and prints old messages when first called. To ignore them,
56we won't print messages for the first 0.5 seconds */
09f6024fHeniker4 years ago57const filter = new ExecutionsFilterBeforeTimestamp(/* delayInSeconds*/ 0.5);
c2bf3c4fdigeff10 years ago58this._logCatSpawn.stderr.on("data", (data: Buffer) => {
0a68f8dbArtem Egorov8 years ago59filter.execute(() => this._logger.info(data.toString()));
c2bf3c4fdigeff10 years ago60});
61
62this._logCatSpawn.stdout.on("data", (data: Buffer) => {
0a68f8dbArtem Egorov8 years ago63filter.execute(() => this._logger.info(data.toString()));
c2bf3c4fdigeff10 years ago64});
0d77292aJiglioNero4 years ago65
66try {
67await this._logCatSpawn.outcome;
68this._logger.info(
69localize(
70"LogCatMonitoringStoppedBecauseTheProcessExited",
71"LogCat monitoring stopped because the process exited.",
72),
73);
74} catch (error) {
75if (!this._logCatSpawn) {
76// We stopped log cat ourselves
77this._logger.info(
78localize(
79"LogCatMonitoringStoppedBecauseTheDebuggingSessionFinished",
80"LogCat monitoring stopped because the debugging session finished",
34472878RedMickey5 years ago81),
0d77292aJiglioNero4 years ago82);
83} else {
84throw error; // Unknown error. Pass it up the promise chain
85}
86} finally {
87this._logCatSpawn = null;
88}
710f8655digeff10 years ago89}
90
91public dispose(): void {
92if (this._logCatSpawn) {
c2bf3c4fdigeff10 years ago93const logCatSpawn = this._logCatSpawn;
710f8655digeff10 years ago94this._logCatSpawn = null;
c2bf3c4fdigeff10 years ago95logCatSpawn.spawnedProcess.kill();
710f8655digeff10 years ago96}
474b4b72Dmitry Zinovyev9 years ago97
0a68f8dbArtem Egorov8 years ago98OutputChannelLogger.disposeChannel(this._logger.channelName);
710f8655digeff10 years ago99}
100
28ce21b5digeff10 years ago101private getLogCatArguments(): string[] {
c2bf3c4fdigeff10 years ago102// We use the setting if it's defined, or the defaults if it's not
103return this.isNullOrUndefined(this._userProvidedLogCatArguments) // "" is a valid value, so we can't just if () this
104? LogCatMonitor.DEFAULT_PARAMETERS
09f6024fHeniker4 years ago105: String(this._userProvidedLogCatArguments).split(" "); // Parse string and split into string[]
710f8655digeff10 years ago106}
107
108private isNullOrUndefined(value: any): boolean {
109return typeof value === "undefined" || value === null;
110}
111}