Skip to content

Commit 4e4367a

Browse files
fix: Device logs are not properly shown with debug command
The console.log from application should be shown in the terminal when `tns debug <platform>` command is executed. When `tns debug android --start` is used, the console.log messages are not visible as noone has started reading them. Add logic to read and filter them based on the PID of the application. When `tns debug ios [--start]` is used with older runtime (4.0.1 for example) with iOS Simulator, the device logs are not shown as the filtering is based on the projectName, but noone has set this projectName to the filter. So we filter everything. Fix this by setting the projectName to the filter instance. Fix the `tns debug ios --justlaunch` and `tns debug ios --start --justlaunch` which are always printing console.logs, while in fact they shouldn't.
1 parent 47c4c70 commit 4e4367a

File tree

4 files changed

+42
-12
lines changed

4 files changed

+42
-12
lines changed

lib/services/android-debug-service.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,27 @@ export class AndroidDebugService extends DebugServiceBase implements IPlatformDe
1414
private $androidDeviceDiscovery: Mobile.IDeviceDiscovery,
1515
private $androidProcessService: Mobile.IAndroidProcessService,
1616
private $net: INet,
17-
private $projectDataService: IProjectDataService) {
17+
private $projectDataService: IProjectDataService,
18+
private $deviceLogProvider: Mobile.IDeviceLogProvider) {
1819
super(device, $devicesService);
1920
}
2021

2122
public async debug(debugData: IDebugData, debugOptions: IDebugOptions): Promise<string> {
2223
this._packageName = debugData.applicationIdentifier;
23-
return debugOptions.emulator
24-
? this.debugOnEmulator(debugData, debugOptions)
25-
: this.debugOnDevice(debugData, debugOptions);
24+
const result = debugOptions.emulator
25+
? await this.debugOnEmulator(debugData, debugOptions)
26+
: await this.debugOnDevice(debugData, debugOptions);
27+
28+
if (!debugOptions.justlaunch) {
29+
const pid = await this.$androidProcessService.getAppProcessId(debugData.deviceIdentifier, debugData.applicationIdentifier);
30+
if (pid) {
31+
this.$deviceLogProvider.setApplicationPidForDevice(debugData.deviceIdentifier, pid);
32+
const device = await this.$devicesService.getDevice(debugData.deviceIdentifier);
33+
await device.openDeviceLogStream();
34+
}
35+
}
36+
37+
return result;
2638
}
2739

2840
public async debugStart(debugData: IDebugData, debugOptions: IDebugOptions): Promise<void> {

lib/services/ios-debug-service.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ export class IOSDebugService extends DebugServiceBase implements IPlatformDebugS
2929
private $iOSSocketRequestExecutor: IiOSSocketRequestExecutor,
3030
private $processService: IProcessService,
3131
private $socketProxyFactory: ISocketProxyFactory,
32-
private $projectDataService: IProjectDataService) {
32+
private $projectDataService: IProjectDataService,
33+
private $deviceLogProvider: Mobile.IDeviceLogProvider) {
3334
super(device, $devicesService);
3435
this.$processService.attachToProcessExitSignals(this, this.debugStop);
3536
this.$socketProxyFactory.on(CONNECTION_ERROR_EVENT_NAME, (e: Error) => this.emit(CONNECTION_ERROR_EVENT_NAME, e));
@@ -40,8 +41,6 @@ export class IOSDebugService extends DebugServiceBase implements IPlatformDebugS
4041
}
4142

4243
public async debug(debugData: IDebugData, debugOptions: IDebugOptions): Promise<string> {
43-
await this.device.openDeviceLogStream();
44-
4544
if (debugOptions.debugBrk && debugOptions.start) {
4645
this.$errors.failWithoutHelp("Expected exactly one of the --debug-brk or --start options.");
4746
}
@@ -50,6 +49,20 @@ export class IOSDebugService extends DebugServiceBase implements IPlatformDebugS
5049
debugOptions.emulator = true;
5150
}
5251

52+
if (!debugOptions.justlaunch) {
53+
let projectName = debugData.projectName;
54+
if (!projectName && debugData.projectDir) {
55+
const projectData = this.$projectDataService.getProjectData(debugData.projectDir);
56+
projectName = projectData.projectName;
57+
}
58+
59+
if (projectName) {
60+
this.$deviceLogProvider.setProjectNameForDevice(debugData.deviceIdentifier, projectName);
61+
}
62+
63+
await this.device.openDeviceLogStream();
64+
}
65+
5366
if (debugOptions.emulator) {
5467
if (debugOptions.start) {
5568
return this.emulatorStart(debugData, debugOptions);
@@ -108,7 +121,7 @@ export class IOSDebugService extends DebugServiceBase implements IPlatformDebugS
108121

109122
private async emulatorDebugBrk(debugData: IDebugData, debugOptions: IDebugOptions): Promise<string> {
110123
const args = debugOptions.debugBrk ? "--nativescript-debug-brk" : "--nativescript-debug-start";
111-
const launchResult = await this.$iOSEmulatorServices.runApplicationOnEmulator(debugData.pathToAppPackage, {
124+
const launchResult = await this.$iOSEmulatorServices.runApplicationOnEmulator(debugData.pathToAppPackage, {
112125
waitForDebugger: true,
113126
captureStdin: true,
114127
args: args,

test/services/android-debug-service.ts

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

1920
public getChromeDebugUrl(debugOptions: IDebugOptions, port: number): string {
@@ -30,6 +31,7 @@ const createTestInjector = (): IInjector => {
3031
testInjector.register("androidProcessService", {});
3132
testInjector.register("net", {});
3233
testInjector.register("projectDataService", {});
34+
testInjector.register("deviceLogProvider", {});
3335

3436
return testInjector;
3537
};

test/services/ios-debug-service.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,11 @@ class IOSDebugServiceInheritor extends IOSDebugService {
2020
$processService: IProcessService,
2121
$socketProxyFactory: ISocketProxyFactory,
2222
$net: INet,
23-
$projectDataService: IProjectDataService) {
23+
$projectDataService: IProjectDataService,
24+
$deviceLogProvider: Mobile.IDeviceLogProvider) {
2425
super(<any>{}, $devicesService, $platformService, $iOSEmulatorServices, $childProcess, $hostInfo, $logger, $errors,
25-
$npmInstallationManager, $iOSDebuggerPortService, $iOSNotification, $iOSSocketRequestExecutor, $processService, $socketProxyFactory, $projectDataService);
26+
$npmInstallationManager, $iOSDebuggerPortService, $iOSNotification, $iOSSocketRequestExecutor, $processService,
27+
$socketProxyFactory, $projectDataService, $deviceLogProvider);
2628
}
2729

2830
public getChromeDebugUrl(debugOptions: IDebugOptions, port: number): string {
@@ -58,6 +60,7 @@ const createTestInjector = (): IInjector => {
5860

5961
testInjector.register("projectDataService", {});
6062
testInjector.register("iOSDebuggerPortService", {});
63+
testInjector.register("deviceLogProvider", {});
6164

6265
return testInjector;
6366
};

0 commit comments

Comments
 (0)