Skip to content

Commit aa4ecfe

Browse files
refactor: Set logFilter's project name when starting iOS application
When strating iOS Application, set the projectName to the log filter, so we can filter the logs by it. Same logic is used for Android, where we are using the PID of the started application. So introduce IDeviceLogOptions, where logLevel, pid and projectName can be set. Pass the projectName wherever is required in order to get it in startApplication.
1 parent eb4f8b5 commit aa4ecfe

11 files changed

+53
-39
lines changed

lib/definitions/platform.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ interface IPlatformService extends IBuildPlatformAction, NodeJS.EventEmitter {
125125
* @param {IProjectData} projectData DTO with information about the project.
126126
* @returns {void}
127127
*/
128-
startApplication(platform: string, runOptions: IRunPlatformOptions, projectId: string): Promise<void>;
128+
startApplication(platform: string, runOptions: IRunPlatformOptions, projectId: string, projectName: string): Promise<void>;
129129

130130
cleanDestinationApp(platformInfo: IPreparePlatformInfo): Promise<void>;
131131
validatePlatformInstalled(platform: string, projectData: IProjectData): void;

lib/helpers/livesync-command-helper.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ export class LiveSyncCommandHelper implements ILiveSyncCommandHelper {
151151
};
152152

153153
await this.$platformService.deployPlatform(deployPlatformInfo);
154-
await this.$platformService.startApplication(currentPlatform, runPlatformOptions, this.$projectData.projectId);
154+
await this.$platformService.startApplication(currentPlatform, runPlatformOptions, this.$projectData.projectId, this.$projectData.projectName);
155155
this.$platformService.trackProjectType(this.$projectData);
156156
}
157157
}

lib/services/android-debug-service.ts

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ export class AndroidDebugService extends DebugServiceBase implements IPlatformDe
1313
private $logger: ILogger,
1414
private $androidDeviceDiscovery: Mobile.IDeviceDiscovery,
1515
private $androidProcessService: Mobile.IAndroidProcessService,
16-
private $net: INet) {
16+
private $net: INet,
17+
private $projectDataService: IProjectDataService) {
1718
super(device, $devicesService);
1819
}
1920

@@ -25,8 +26,9 @@ export class AndroidDebugService extends DebugServiceBase implements IPlatformDe
2526
}
2627

2728
public async debugStart(debugData: IDebugData, debugOptions: IDebugOptions): Promise<void> {
29+
const projectData = this.$projectDataService.getProjectData(debugData.projectDir);
2830
await this.$devicesService.initialize({ platform: this.platform, deviceId: debugData.deviceIdentifier });
29-
const action = (device: Mobile.IAndroidDevice): Promise<void> => this.debugStartCore(debugData.applicationIdentifier, debugOptions);
31+
const action = (device: Mobile.IAndroidDevice): Promise<void> => this.debugStartCore(debugData.applicationIdentifier, debugOptions, projectData.projectName);
3032

3133
await this.$devicesService.execute(action, this.getCanExecuteAction(debugData.deviceIdentifier));
3234
}
@@ -91,23 +93,24 @@ export class AndroidDebugService extends DebugServiceBase implements IPlatformDe
9193

9294
await this.$devicesService.initialize({ platform: this.platform, deviceId: debugData.deviceIdentifier });
9395

94-
const action = (device: Mobile.IAndroidDevice): Promise<string> => this.debugCore(device, packageFile, debugData.applicationIdentifier, debugOptions);
96+
const packageName = this.$projectDataService.getProjectData(debugData.projectDir).projectName;
97+
const action = (device: Mobile.IAndroidDevice): Promise<string> => this.debugCore(device, packageFile, debugData.applicationIdentifier, packageName, debugOptions);
9598

9699
const deviceActionResult = await this.$devicesService.execute(action, this.getCanExecuteAction(debugData.deviceIdentifier));
97100
return deviceActionResult[0].result;
98101
}
99102

100-
private async debugCore(device: Mobile.IAndroidDevice, packageFile: string, packageName: string, debugOptions: IDebugOptions): Promise<string> {
101-
await this.printDebugPort(device.deviceInfo.identifier, packageName);
103+
private async debugCore(device: Mobile.IAndroidDevice, packageFile: string, appId: string, packageName: string, debugOptions: IDebugOptions): Promise<string> {
104+
await this.printDebugPort(device.deviceInfo.identifier, appId);
102105

103106
if (debugOptions.start) {
104-
return await this.attachDebugger(device.deviceInfo.identifier, packageName, debugOptions);
107+
return await this.attachDebugger(device.deviceInfo.identifier, appId, debugOptions);
105108
} else if (debugOptions.stop) {
106109
await this.removePortForwarding();
107110
return null;
108111
} else {
109-
await this.debugStartCore(packageName, debugOptions);
110-
return await this.attachDebugger(device.deviceInfo.identifier, packageName, debugOptions);
112+
await this.debugStartCore(appId, debugOptions, packageName);
113+
return await this.attachDebugger(device.deviceInfo.identifier, appId, debugOptions);
111114
}
112115
}
113116

@@ -126,22 +129,22 @@ export class AndroidDebugService extends DebugServiceBase implements IPlatformDe
126129
return this.getChromeDebugUrl(debugOptions, port);
127130
}
128131

129-
private async debugStartCore(packageName: string, debugOptions: IDebugOptions): Promise<void> {
132+
private async debugStartCore(appId: string, debugOptions: IDebugOptions, projectName: string): Promise<void> {
130133
// Arguments passed to executeShellCommand must be in array ([]), but it turned out adb shell "arg with intervals" still works correctly.
131134
// As we need to redirect output of a command on the device, keep using only one argument.
132135
// We could rewrite this with two calls - touch and rm -f , but -f flag is not available on old Android, so rm call will fail when file does not exist.
133136

134-
await this.device.applicationManager.stopApplication(packageName);
137+
await this.device.applicationManager.stopApplication(appId);
135138

136139
if (debugOptions.debugBrk) {
137-
await this.device.adb.executeShellCommand([`cat /dev/null > /data/local/tmp/${packageName}-debugbreak`]);
140+
await this.device.adb.executeShellCommand([`cat /dev/null > /data/local/tmp/${appId}-debugbreak`]);
138141
}
139142

140-
await this.device.adb.executeShellCommand([`cat /dev/null > /data/local/tmp/${packageName}-debugger-started`]);
143+
await this.device.adb.executeShellCommand([`cat /dev/null > /data/local/tmp/${appId}-debugger-started`]);
141144

142-
await this.device.applicationManager.startApplication(packageName);
145+
await this.device.applicationManager.startApplication(appId, projectName);
143146

144-
await this.waitForDebugger(packageName);
147+
await this.waitForDebugger(appId);
145148
}
146149

147150
private async waitForDebugger(packageName: String): Promise<void> {

lib/services/ios-debug-service.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ export class IOSDebugService extends DebugServiceBase implements IPlatformDebugS
3333
private $iOSSocketRequestExecutor: IiOSSocketRequestExecutor,
3434
private $processService: IProcessService,
3535
private $socketProxyFactory: ISocketProxyFactory,
36-
private $net: INet) {
36+
private $net: INet,
37+
private $projectDataService: IProjectDataService) {
3738
super(device, $devicesService);
3839
this.$processService.attachToProcessExitSignals(this, this.debugStop);
3940
this.$socketProxyFactory.on(CONNECTION_ERROR_EVENT_NAME, (e: Error) => this.emit(CONNECTION_ERROR_EVENT_NAME, e));
@@ -173,6 +174,7 @@ export class IOSDebugService extends DebugServiceBase implements IPlatformDebugS
173174

174175
private async deviceDebugBrk(debugData: IDebugData, debugOptions: IDebugOptions): Promise<string> {
175176
await this.$devicesService.initialize({ platform: this.platform, deviceId: debugData.deviceIdentifier });
177+
const projectData = this.$projectDataService.getProjectData(debugData.projectDir);
176178
const action = async (device: iOSDevice.IOSDevice): Promise<string> => {
177179
if (device.isEmulator) {
178180
return await this.emulatorDebugBrk(debugData, debugOptions);
@@ -185,7 +187,7 @@ export class IOSDebugService extends DebugServiceBase implements IPlatformDebugS
185187
};
186188

187189
const promisesResults = await Promise.all<any>([
188-
this.$platformService.startApplication(this.platform, runOptions, debugData.applicationIdentifier),
190+
this.$platformService.startApplication(this.platform, runOptions, debugData.applicationIdentifier, projectData.projectName),
189191
this.debugBrkCore(device, debugData, debugOptions)
190192
]);
191193

lib/services/ios-log-filter.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,15 @@ export class IOSLogFilter implements Mobile.IPlatformLogFilter {
1717

1818
private partialLine: string = null;
1919

20-
constructor(
20+
constructor(private $logger: ILogger,
2121
private $loggingLevels: Mobile.ILoggingLevels,
2222
private $fs: IFileSystem,
2323
private $projectData: IProjectData) {
2424
}
2525

26-
public filterData(data: string, logLevel: string, pid?: string): string {
27-
const specifiedLogLevel = (logLevel || '').toUpperCase();
26+
public filterData(data: string, loggingOptions: Mobile.IDeviceLogOptions = <any>{}): string {
27+
const specifiedLogLevel = (loggingOptions.logLevel || '').toUpperCase();
28+
this.$logger.trace("Logging options", loggingOptions);
2829

2930
if (specifiedLogLevel !== this.$loggingLevels.info || !data) {
3031
return data;
@@ -57,8 +58,8 @@ export class IOSLogFilter implements Mobile.IPlatformLogFilter {
5758
// Check if the name of the app equals the name of the CLI project and turn on the filter if not.
5859
// We call initializeProjectData in order to obtain the current project name as the instance
5960
// of this filter may be used accross multiple projects.
60-
this.$projectData.initializeProjectData();
61-
this.filterActive = matchResult[1] !== this.$projectData.projectName;
61+
const projectName = loggingOptions && loggingOptions.projectName;
62+
this.filterActive = matchResult[1] !== projectName;
6263
}
6364

6465
if (this.filterActive) {

lib/services/livesync/android-device-livesync-service.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export class AndroidDeviceLiveSyncService extends DeviceLiveSyncServiceBase impl
4242
(localToDevicePath: Mobile.ILocalToDevicePathData) => !this.canExecuteFastSync(localToDevicePath.getLocalPath(), projectData, this.device.deviceInfo.platform));
4343

4444
if (!canExecuteFastSync) {
45-
return this.restartApplication(deviceAppData);
45+
return this.restartApplication(deviceAppData, projectData.projectName);
4646
}
4747
}
4848

@@ -57,12 +57,12 @@ export class AndroidDeviceLiveSyncService extends DeviceLiveSyncServiceBase impl
5757
await this.$mobileHelper.buildDevicePath(deviceRootPath, LiveSyncPaths.REMOVEDSYNC_DIR_NAME)]);
5858
}
5959

60-
private async restartApplication(deviceAppData: Mobile.IDeviceAppData): Promise<void> {
60+
private async restartApplication(deviceAppData: Mobile.IDeviceAppData, projectName: string): Promise<void> {
6161
const devicePathRoot = `/data/data/${deviceAppData.appIdentifier}/files`;
6262
const devicePath = this.$mobileHelper.buildDevicePath(devicePathRoot, "code_cache", "secondary_dexes", "proxyThumb");
6363
await this.device.adb.executeShellCommand(["rm", "-rf", devicePath]);
6464

65-
await this.device.applicationManager.restartApplication(deviceAppData.appIdentifier);
65+
await this.device.applicationManager.restartApplication(deviceAppData.appIdentifier, projectName);
6666
}
6767

6868
public async beforeLiveSyncAction(deviceAppData: Mobile.IDeviceAppData): Promise<void> {

lib/services/platform-service.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -543,11 +543,11 @@ export class PlatformService extends EventEmitter implements IPlatformService {
543543
await this.$devicesService.execute(action, this.getCanExecuteAction(deployInfo.platform, deployInfo.deployOptions));
544544
}
545545

546-
public async startApplication(platform: string, runOptions: IRunPlatformOptions, projectId: string): Promise<void> {
546+
public async startApplication(platform: string, runOptions: IRunPlatformOptions, projectId: string, projectName: string): Promise<void> {
547547
this.$logger.out("Starting...");
548548

549549
const action = async (device: Mobile.IDevice) => {
550-
await device.applicationManager.startApplication(projectId);
550+
await device.applicationManager.startApplication(projectId, projectName);
551551
this.$logger.out(`Successfully started on device with identifier '${device.deviceInfo.identifier}'.`);
552552
};
553553

test/services/android-debug-service.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ class AndroidDebugServiceInheritor extends AndroidDebugService {
1111
$logger: ILogger,
1212
$androidDeviceDiscovery: Mobile.IDeviceDiscovery,
1313
$androidProcessService: Mobile.IAndroidProcessService,
14-
$net: INet) {
15-
super(<any>{}, $devicesService, $errors, $logger, $androidDeviceDiscovery, $androidProcessService, $net);
14+
$net: INet,
15+
$projectDataService: IProjectDataService) {
16+
super(<any>{}, $devicesService, $errors, $logger, $androidDeviceDiscovery, $androidProcessService, $net, $projectDataService);
1617
}
1718

1819
public getChromeDebugUrl(debugOptions: IDebugOptions, port: number): string {
@@ -28,6 +29,7 @@ const createTestInjector = (): IInjector => {
2829
testInjector.register("androidDeviceDiscovery", {});
2930
testInjector.register("androidProcessService", {});
3031
testInjector.register("net", {});
32+
testInjector.register("projectDataService", {});
3133

3234
return testInjector;
3335
};

test/services/ios-debug-service.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@ class IOSDebugServiceInheritor extends IOSDebugService {
1818
$iOSSocketRequestExecutor: IiOSSocketRequestExecutor,
1919
$processService: IProcessService,
2020
$socketProxyFactory: ISocketProxyFactory,
21-
$net: INet) {
21+
$net: INet,
22+
$projectDataService: IProjectDataService) {
2223
super(<any>{}, $devicesService, $platformService, $iOSEmulatorServices, $childProcess, $hostInfo, $logger, $errors,
23-
$npmInstallationManager, $iOSNotification, $iOSSocketRequestExecutor, $processService, $socketProxyFactory, $net);
24+
$npmInstallationManager, $iOSNotification, $iOSSocketRequestExecutor, $processService, $socketProxyFactory, $net, $projectDataService);
2425
}
2526

2627
public getChromeDebugUrl(debugOptions: IDebugOptions, port: number): string {
@@ -54,6 +55,8 @@ const createTestInjector = (): IInjector => {
5455
waitForPortToListen: async (opts: { port: number, timeout: number, interval?: number }): Promise<boolean> => true
5556
});
5657

58+
testInjector.register("projectDataService", {});
59+
5760
return testInjector;
5861
};
5962

test/services/ios-log-filter.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { IOSLogFilter } from "../../lib/services/ios-log-filter";
22
import { Yok } from "../../lib/common/yok";
33
import { LoggingLevels } from "../../lib/common/mobile/logging-levels";
4+
import { LoggerStub } from "../stubs";
45
import * as assert from "assert";
56

67
function createTestInjector(projectName: string): IInjector {
@@ -15,6 +16,8 @@ function createTestInjector(projectName: string): IInjector {
1516
projectName: projectName
1617
});
1718

19+
testInjector.register("logger", LoggerStub);
20+
1821
return testInjector;
1922
}
2023

@@ -189,7 +192,7 @@ describe("iOSLogFilter", () => {
189192
while (true) {
190193
const currentRange = Math.floor(Math.random() * maxRange);
191194
const currentFilterInput = input.substr(currentStart, currentRange);
192-
const tempOutput = logFilter.filterData(currentFilterInput, infoLogLevel, null);
195+
const tempOutput = logFilter.filterData(currentFilterInput, { logLevel: infoLogLevel, projectName: data.projectName });
193196
if (tempOutput !== null) {
194197
output += tempOutput;
195198
}
@@ -206,7 +209,7 @@ describe("iOSLogFilter", () => {
206209
it(`returns correct data when logLevel is ${fullLogLevel} on iOS ${data.version} and all data is passed at once`, () => {
207210
testInjector = createTestInjector(data.projectName);
208211
logFilter = testInjector.resolve(IOSLogFilter);
209-
const actualData = logFilter.filterData(data.originalDataArr.join("\n"), fullLogLevel, null);
212+
const actualData = logFilter.filterData(data.originalDataArr.join("\n"), { logLevel: fullLogLevel, projectName: data.projectName });
210213
const actualArr = actualData.split("\n").map(line => line.trim());
211214
const expectedArr = data.originalDataArr.map(line => line.trim()).filter(item => item !== null);
212215
assert.deepEqual(actualArr, expectedArr);
@@ -216,23 +219,23 @@ describe("iOSLogFilter", () => {
216219
data.originalDataArr.forEach(line => {
217220
testInjector = createTestInjector(data.projectName);
218221
logFilter = testInjector.resolve(IOSLogFilter);
219-
const actualData = logFilter.filterData(line, fullLogLevel, null);
222+
const actualData = logFilter.filterData(line, { logLevel: fullLogLevel, projectName: data.projectName });
220223
assert.deepEqual(actualData.trim(), line.trim());
221224
});
222225
});
223226

224227
it(`parses data incorrectly when logLevel is ${infoLogLevel} on iOS ${data.version} and all data is passed at once with pid(simulator)`, () => {
225228
testInjector = createTestInjector(data.simProjectName);
226229
logFilter = testInjector.resolve(IOSLogFilter);
227-
const actualData = logFilter.filterData(data.simulator.join("\n"), infoLogLevel, pid);
230+
const actualData = logFilter.filterData(data.simulator.join("\n"), { logLevel: infoLogLevel, projectName: data.simProjectName, applicationPid: pid });
228231
const actualArr = actualData.split("\n").map(line => line.trim());
229232
assert.deepEqual(actualArr, data.simulatorExpectedArr.filter(item => item !== null));
230233
});
231234

232235
it(`parses data incorrectly when logLevel is ${infoLogLevel} on iOS ${data.version} and all data is passed at once and pid is available`, () => {
233236
testInjector = createTestInjector(data.projectName);
234237
logFilter = testInjector.resolve(IOSLogFilter);
235-
const actualData = logFilter.filterData(data.originalDataArr.join("\n"), infoLogLevel, null);
238+
const actualData = logFilter.filterData(data.originalDataArr.join("\n"), { logLevel: infoLogLevel, projectName: data.projectName });
236239
const actualArr = actualData.split("\n").map(line => line.trim());
237240
const expectedArr = data.infoExpectedArr.filter(item => item !== null);
238241
assert.deepEqual(actualArr, expectedArr);
@@ -245,7 +248,7 @@ describe("iOSLogFilter", () => {
245248
if (line.length > 0) {
246249
line += "\n";
247250
}
248-
const actualData = logFilter.filterData(line, infoLogLevel, null);
251+
const actualData = logFilter.filterData(line, { logLevel: infoLogLevel, projectName: data.projectName });
249252
const expectedData = data.infoExpectedArr[index];
250253
assert.equal(actualData && actualData.trim(), expectedData);
251254
});

0 commit comments

Comments
 (0)