Skip to content
This repository was archived by the owner on Feb 2, 2021. It is now read-only.

Commit 2ec92a7

Browse files
committed
Filter android logs by process id
1 parent c6898cf commit 2ec92a7

8 files changed

+182
-20
lines changed

definitions/mobile.d.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ declare module Mobile {
170170
* @param {string} deviceIdentifier The unique identifier of the device.
171171
* @param {string} pid The Process ID of the currently running application for which we need the logs.
172172
*/
173-
setApplictionPidForDevice(deviceIdentifier: string, pid: string): void;
173+
setApplicationPidForDevice(deviceIdentifier: string, pid: string): void;
174174
}
175175

176176
/**
@@ -448,6 +448,14 @@ declare module Mobile {
448448
* @return {Promise<IDictionary<number>>} Dictionary, where the keys are app identifiers and the values are local ports.
449449
*/
450450
getMappedAbstractToTcpPorts(deviceIdentifier: string, appIdentifiers: string[], framework: string): Promise<IDictionary<number>>;
451+
452+
/**
453+
* Gets the PID of a running application.
454+
* @param deviceIdentifier {string} The identifier of the device.
455+
* @param appIdentifier The identifier of the application.
456+
* @return {string} Returns the process id matching the application identifier in the device process list.
457+
*/
458+
getAppProcessId(deviceIdentifier: string, appIdentifier: string): Promise<string>;
451459
}
452460

453461
/**

mobile/android/android-application-manager.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export class AndroidApplicationManager extends ApplicationManagerBase {
1111
private $logcatHelper: Mobile.ILogcatHelper,
1212
private $androidProcessService: Mobile.IAndroidProcessService,
1313
private $httpClient: Server.IHttpClient,
14+
private $deviceLogProvider: Mobile.IDeviceLogProvider,
1415
$logger: ILogger,
1516
$hooksService: IHooksService) {
1617
super($logger, $hooksService);
@@ -49,6 +50,12 @@ export class AndroidApplicationManager extends ApplicationManagerBase {
4950
"1"]);
5051

5152
if (!this.$options.justlaunch) {
53+
const deviceIdentifier = this.identifier;
54+
const processIdentifier = await this.$androidProcessService.getAppProcessId(deviceIdentifier, appIdentifier);
55+
if (processIdentifier) {
56+
this.$deviceLogProvider.setApplicationPidForDevice(deviceIdentifier, processIdentifier);
57+
}
58+
5259
await this.$logcatHelper.start(this.identifier);
5360
}
5461
}

mobile/android/android-log-filter.ts

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ export class AndroidLogFilter implements Mobile.IPlatformLogFilter {
77

88
// sample line is "11-23 12:39:07.310 1584 1597 I art : Background sticky concurrent mark sweep GC freed 21966(1780KB) AllocSpace objects, 4(80KB) LOS objects, 77% free, 840KB/3MB, paused 4.018ms total 158.629ms"
99
// or '12-28 10:45:08.020 3329 3329 W chromium: [WARNING:data_reduction_proxy_settings.cc(328)] SPDY proxy OFF at startup'
10-
private static API_LEVEL_23_LINE_REGEX = /.+?\s+?(?:[A-Z]\s+?)([A-Za-z ]+?)\s*?\: (.*)/;
10+
private static API_LEVEL_23_LINE_REGEX = /.+?\s+?(?:[A-Z]\s+?)([A-Za-z \.]+?)\s*?\: (.*)/;
1111

1212
constructor(private $loggingLevels: Mobile.ILoggingLevels) { }
1313

14-
public filterData(data: string, logLevel: string): string {
15-
let specifiedLogLevel = (logLevel || '').toUpperCase();
14+
public filterData(data: string, logLevel: string, pid: string): string {
15+
const specifiedLogLevel = (logLevel || '').toUpperCase();
1616
if (specifiedLogLevel === this.$loggingLevels.info) {
17-
let log = this.getConsoleLogFromLine(data);
17+
const log = this.getConsoleLogFromLine(data, pid);
1818
if (log) {
1919
if (log.tag) {
2020
return `${log.tag}: ${log.message}` + os.EOL;
@@ -29,15 +29,28 @@ export class AndroidLogFilter implements Mobile.IPlatformLogFilter {
2929
return data + os.EOL;
3030
}
3131

32-
private getConsoleLogFromLine(lineText: string): any {
33-
let acceptedTags = ["chromium", "Web Console", "JS"];
34-
let match = lineText.match(AndroidLogFilter.LINE_REGEX) || lineText.match(AndroidLogFilter.API_LEVEL_23_LINE_REGEX);
32+
private getConsoleLogFromLine(lineText: string, pid: string): any {
33+
const acceptedTags = ["chromium", "Web Console", "JS", "ActivityManager", "System.err"];
34+
35+
let consoleLogMessage: { tag?: string, message: string };
36+
37+
const match = lineText.match(AndroidLogFilter.LINE_REGEX) || lineText.match(AndroidLogFilter.API_LEVEL_23_LINE_REGEX);
3538

3639
if (match && acceptedTags.indexOf(match[1].trim()) !== -1) {
37-
return { tag: match[1].trim(), message: match[2] };
40+
consoleLogMessage = { tag: match[1].trim(), message: match[2] };
41+
}
42+
43+
if (!consoleLogMessage) {
44+
const matchingTag = _.some(acceptedTags, (tag: string) => { return lineText.indexOf(tag) !== -1; });
45+
consoleLogMessage = matchingTag ? { message: lineText } : null;
3846
}
39-
let matchingTag = _.some(acceptedTags, (tag: string) => { return lineText.indexOf(tag) !== -1; });
40-
return matchingTag ? { message: lineText } : null;
47+
48+
// filter log line if it does not belong to the current application process id
49+
if (pid && lineText.indexOf(pid) < 0) {
50+
return null;
51+
}
52+
53+
return consoleLogMessage;
4154
}
4255
}
4356
$injector.register("androidLogFilter", AndroidLogFilter);

mobile/device-log-provider-base.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export abstract class DeviceLogProviderBase extends EventEmitter implements Mobi
1313

1414
public abstract setLogLevel(logLevel: string, deviceIdentifier?: string): void;
1515

16-
public setApplictionPidForDevice(deviceIdentifier: string, pid: string): void {
16+
public setApplicationPidForDevice(deviceIdentifier: string, pid: string): void {
1717
this.setDeviceLogOptionsProperty(deviceIdentifier, (deviceLogOptions: Mobile.IDeviceLogOptions) => deviceLogOptions.applicationPid, pid);
1818
}
1919

mobile/device-log-provider.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ export class DeviceLogProvider extends DeviceLogProviderBase {
88

99
public logData(lineText: string, platform: string, deviceIdentifier: string): void {
1010
let applicationPid = this.getApplicationPidForDevice(deviceIdentifier);
11-
1211
let data = this.$logFilter.filterData(platform, lineText, applicationPid);
1312
if (data) {
1413
this.$logger.write(data);

mobile/ios/simulator/ios-simulator-application-manager.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export class IOSSimulatorApplicationManager extends ApplicationManagerBase {
4444

4545
if (!this.$options.justlaunch) {
4646
let pid = launchResult.split(":")[1].trim();
47-
this.$deviceLogProvider.setApplictionPidForDevice(this.identifier, pid);
47+
this.$deviceLogProvider.setApplicationPidForDevice(this.identifier, pid);
4848
this.$iOSSimulatorLogProvider.startLogProcess(this.identifier);
4949
}
5050
}

mobile/mobile-core/android-process-service.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,13 @@ export class AndroidProcessService implements Mobile.IAndroidProcessService {
118118
.value();
119119
}
120120

121+
public async getAppProcessId(deviceIdentifier: string, appIdentifier: string): Promise<string> {
122+
const adb = this.getAdb(deviceIdentifier);
123+
const processId = (await this.getProcessIds(adb, [appIdentifier]))[appIdentifier];
124+
125+
return processId ? processId.toString() : null;
126+
}
127+
121128
private async getApplicationInfoFromWebViewPortInformation(adb: Mobile.IDeviceAndroidDebugBridge, deviceIdentifier: string, information: string): Promise<Mobile.IDeviceApplicationInformation> {
122129
// Need to search by processId to check for old Android webviews (@webview_devtools_remote_<processId>).
123130
let processIdRegExp = /@webview_devtools_remote_(.+)/g,

0 commit comments

Comments
 (0)