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

Commit 33354a9

Browse files
committed
Filter android logs by process id
1 parent c6898cf commit 33354a9

File tree

7 files changed

+47
-12
lines changed

7 files changed

+47
-12
lines changed

definitions/mobile.d.ts

Lines changed: 2 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
/**
@@ -313,6 +313,7 @@ declare module Mobile {
313313
}
314314

315315
interface IDeviceAndroidDebugBridge extends IAndroidDebugBridge {
316+
identifier: string;
316317
executeShellCommand(args: string[], options?: IAndroidDebugBridgeCommandOptions): Promise<any>;
317318
sendBroadcastToDevice(action: string, extras?: IStringDictionary): Promise<number>;
318319
}

mobile/android/android-application-manager.ts

Lines changed: 30 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,10 +50,39 @@ export class AndroidApplicationManager extends ApplicationManagerBase {
4950
"1"]);
5051

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

63+
private async getProcessId(appId: string): Promise<string> {
64+
// Read the process id of the application that was just started
65+
// Information is returned in the following format
66+
// USER PID PPID VSIZE RSS WCHAN PC NAME
67+
// u0_a64 4831 488 851380 94888 ep_poll f1e43bb9 S org.nativescript.myApplication
68+
let processes = await this.adb.executeShellCommand(["ps"]);
69+
processes = processes.trim()
70+
.split("\n")
71+
.map((line: string) => line.replace(/\s+/g, ' ')
72+
.split(" ")
73+
.filter((entry: string) => /\S/.test(entry)));
74+
75+
let currentProcessLine = processes.find((line: Array<string>) => line[line.length - 1] === appId);
76+
77+
if (!currentProcessLine) {
78+
return Promise.resolve(null);
79+
}
80+
81+
let pid = currentProcessLine[1];
82+
83+
return pid;
84+
}
85+
5686
public stopApplication(appIdentifier: string): Promise<void> {
5787
return this.adb.executeShellCommand(["am", "force-stop", `${appIdentifier}`]);
5888
}

mobile/android/android-log-filter.ts

Lines changed: 12 additions & 7 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 {
14+
public filterData(data: string, logLevel: string, pid: string): string {
1515
let specifiedLogLevel = (logLevel || '').toUpperCase();
1616
if (specifiedLogLevel === this.$loggingLevels.info) {
17-
let log = this.getConsoleLogFromLine(data);
17+
let log = this.getConsoleLogFromLine(data, pid);
1818
if (log) {
1919
if (log.tag) {
2020
return `${log.tag}: ${log.message}` + os.EOL;
@@ -29,15 +29,20 @@ 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"];
32+
private getConsoleLogFromLine(lineText: string, pid: string): any {
33+
// filter log line if it does not belong to the current application process id
34+
if (lineText.indexOf(pid) < 0) {
35+
return null;
36+
}
37+
38+
let acceptedTags = ["chromium", "Web Console", "JS", "ActivityManager", "System.err"];
3439
let match = lineText.match(AndroidLogFilter.LINE_REGEX) || lineText.match(AndroidLogFilter.API_LEVEL_23_LINE_REGEX);
3540

3641
if (match && acceptedTags.indexOf(match[1].trim()) !== -1) {
3742
return { tag: match[1].trim(), message: match[2] };
3843
}
39-
let matchingTag = _.some(acceptedTags, (tag: string) => { return lineText.indexOf(tag) !== -1; });
40-
return matchingTag ? { message: lineText } : null;
44+
45+
return null;
4146
}
4247
}
4348
$injector.register("androidLogFilter", AndroidLogFilter);

mobile/android/device-android-debug-bridge.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ interface IComposeCommandResult {
66
}
77

88
export class DeviceAndroidDebugBridge extends AndroidDebugBridge implements Mobile.IDeviceAndroidDebugBridge {
9-
constructor(private identifier: string,
9+
constructor(public identifier: string,
1010
protected $childProcess: IChildProcess,
1111
protected $errors: IErrors,
1212
protected $logger: ILogger,

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
}

0 commit comments

Comments
 (0)