forked from NativeScript/nativescript-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogcat-helper.ts
245 lines (209 loc) · 6.79 KB
/
logcat-helper.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
import * as byline from "byline";
import { ChildProcess } from "child_process";
import * as semver from "semver";
import { IDictionary } from "../../declarations";
import { IInjector } from "../../definitions/yok";
import { injector } from "../../yok";
import { DeviceAndroidDebugBridge } from "./device-android-debug-bridge";
interface IDeviceLoggingData {
loggingProcess: ChildProcess;
appStartTrackingProcess: ChildProcess;
lineStream: any;
rawLineStream: any;
keepSingleProcess: boolean;
}
export class LogcatHelper implements Mobile.ILogcatHelper {
private mapDevicesLoggingData: IDictionary<IDeviceLoggingData>;
constructor(
private $deviceLogProvider: Mobile.IDeviceLogProvider,
private $devicePlatformsConstants: Mobile.IDevicePlatformsConstants,
private $logger: ILogger,
private $injector: IInjector,
private $devicesService: Mobile.IDevicesService
) {
this.mapDevicesLoggingData = Object.create(null);
}
public async start(options: Mobile.ILogcatStartOptions): Promise<void> {
const deviceIdentifier = options.deviceIdentifier;
if (deviceIdentifier && !this.mapDevicesLoggingData[deviceIdentifier]) {
this.mapDevicesLoggingData[deviceIdentifier] = {
loggingProcess: null,
lineStream: null,
keepSingleProcess: options.keepSingleProcess,
appStartTrackingProcess: null,
rawLineStream: null,
};
const logcatStream = await this.getLogcatStream(
deviceIdentifier,
options.pid
);
const lineStream = byline(logcatStream.stdout);
this.mapDevicesLoggingData[deviceIdentifier].loggingProcess =
logcatStream;
this.mapDevicesLoggingData[deviceIdentifier].lineStream = lineStream;
logcatStream.stderr.on("data", (data: Buffer) => {
this.$logger.trace("ADB logcat stderr: " + data.toString());
});
logcatStream.on("close", (code: number) => {
try {
this.forceStop(deviceIdentifier);
if (code !== 0) {
this.$logger.trace(
"ADB process exited with code " + code.toString()
);
}
} catch (err) {
// Ignore the error, the process is dead.
}
});
lineStream.on("data", (lineBuffer: Buffer) => {
const lines = (lineBuffer.toString() || "").split("\n");
for (const line of lines) {
this.$deviceLogProvider.logData(
line,
this.$devicePlatformsConstants.Android,
deviceIdentifier
);
}
});
const appStartTrackingStream = await this.getAppStartTrackingLogcatStream(
deviceIdentifier,
options.appId
);
this.mapDevicesLoggingData[deviceIdentifier].appStartTrackingProcess =
appStartTrackingStream;
const rawLineStream = byline(appStartTrackingStream.stdout);
this.mapDevicesLoggingData[deviceIdentifier].rawLineStream =
rawLineStream;
rawLineStream.on("data", (lineBuffer: Buffer) => {
if (!this.mapDevicesLoggingData[deviceIdentifier]?.loggingProcess)
return;
const lines = (lineBuffer.toString() || "").split("\n");
for (let line of lines) {
// 2024-06-26 16:43:22.286 630-659 ActivityManager system_server I Start proc 8854:org.nativescript.uitestsapp/u0a190 for next-top-activity {org.nativescript.uitestsapp/com.tns.NativeScriptActivity}
const startProc = /Start proc (?<pid>[0-9]+):(?<appId>.+?)\//.exec(
line
);
if (
startProc &&
startProc.groups?.appId === options.appId &&
startProc.groups?.pid !== options.pid
) {
this.forceStop(deviceIdentifier);
options.onAppRestarted?.();
}
}
});
}
}
public async dump(deviceIdentifier: string): Promise<void> {
const adb: Mobile.IDeviceAndroidDebugBridge = this.$injector.resolve(
DeviceAndroidDebugBridge,
{ identifier: deviceIdentifier }
);
const logcatDumpStream = await adb.executeCommand(["logcat", "-d"], {
returnChildProcess: true,
});
const lineStream = byline(logcatDumpStream.stdout);
lineStream.on("data", (line: Buffer) => {
const lineText = line.toString();
this.$logger.trace(lineText);
});
logcatDumpStream.on("close", (code: number) => {
logcatDumpStream.removeAllListeners();
lineStream.removeAllListeners();
});
}
/**
* Stops the logcat process for the specified device if keepSingleProcess is not passed on start
*/
public stop(deviceIdentifier: string): void {
if (
this.mapDevicesLoggingData[deviceIdentifier] &&
!this.mapDevicesLoggingData[deviceIdentifier].keepSingleProcess
) {
this.forceStop(deviceIdentifier);
}
}
private forceStop(deviceIdentifier: string): void {
const loggingData = this.mapDevicesLoggingData[deviceIdentifier];
loggingData.loggingProcess?.removeAllListeners();
loggingData.loggingProcess?.kill("SIGINT");
loggingData.lineStream?.removeAllListeners();
loggingData.appStartTrackingProcess?.kill("SIGINT");
loggingData.lineStream?.removeAllListeners();
delete this.mapDevicesLoggingData[deviceIdentifier];
}
/**
* @deprecated - we likely don't need this anymore, and can simplify the code...
*/
private async isLogcatPidSupported(deviceIdentifier: string) {
const device = await this.$devicesService.getDevice(deviceIdentifier);
const minAndroidWithLogcatPidSupport = "7.0.0";
return (
!!device.deviceInfo.version &&
semver.gte(
semver.coerce(device.deviceInfo.version),
minAndroidWithLogcatPidSupport
)
);
}
private async getLogcatStream(deviceIdentifier: string, pid?: string) {
const isLogcatPidSupported = await this.isLogcatPidSupported(
deviceIdentifier
);
const adb: Mobile.IDeviceAndroidDebugBridge = this.$injector.resolve(
DeviceAndroidDebugBridge,
{ identifier: deviceIdentifier }
);
// -T 1 - shows only new logs after starting adb logcat
const logcatCommand = ["logcat", "-T", "1"];
const acceptedTags = [
"chromium",
'"Web Console"',
"JS",
"System.err",
"TNS.Native",
"TNS.Java",
];
if (pid && isLogcatPidSupported) {
logcatCommand.push(`--pid=${pid}`);
acceptedTags.forEach((tag) => {
// -s <tag> - shows only logs with the specified tag
logcatCommand.push("-s", tag);
});
}
const logcatStream = await adb.executeCommand(logcatCommand, {
returnChildProcess: true,
});
return logcatStream;
}
private async getAppStartTrackingLogcatStream(
deviceIdentifier: string,
appId?: string
) {
const adb: Mobile.IDeviceAndroidDebugBridge = this.$injector.resolve(
DeviceAndroidDebugBridge,
{ identifier: deviceIdentifier }
);
// -b system - shows the system buffer/logs only
// -T 1 - shows only new logs after starting adb logcat
const logcatCommand = [
`logcat`,
`-b`,
`system`,
`-T`,
`1`,
"-s",
"ActivityManager",
];
if (appId) {
logcatCommand.push(`--regex=Start.*${appId}`);
}
const appStartTrackingStream = await adb.executeCommand(logcatCommand, {
returnChildProcess: true,
});
return appStartTrackingStream;
}
}
injector.register("logcatHelper", LogcatHelper);