Skip to content

Commit bccdc5f

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.
1 parent 47c4c70 commit bccdc5f

File tree

4 files changed

+49
-14
lines changed

4 files changed

+49
-14
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: 24 additions & 6 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));
@@ -50,19 +51,36 @@ export class IOSDebugService extends DebugServiceBase implements IPlatformDebugS
5051
debugOptions.emulator = true;
5152
}
5253

54+
let action: (debugData: IDebugData, debugOptions: IDebugOptions) => Promise<string> = null;
5355
if (debugOptions.emulator) {
5456
if (debugOptions.start) {
55-
return this.emulatorStart(debugData, debugOptions);
57+
action = this.emulatorStart;
5658
} else {
57-
return this.emulatorDebugBrk(debugData, debugOptions);
59+
action = this.emulatorDebugBrk;
5860
}
5961
} else {
6062
if (debugOptions.start) {
61-
return this.deviceStart(debugData, debugOptions);
63+
action = this.deviceStart;
6264
} else {
63-
return this.deviceDebugBrk(debugData, debugOptions);
65+
action = this.deviceDebugBrk;
6466
}
6567
}
68+
69+
const result = await action.bind(this)(debugData, debugOptions);
70+
71+
if (!debugOptions.justlaunch) {
72+
let projectName = debugData.projectName;
73+
if (!projectName && debugData.projectDir) {
74+
const projectData = this.$projectDataService.getProjectData(debugData.projectDir);
75+
projectName = projectData.projectName;
76+
}
77+
78+
if (projectName) {
79+
this.$deviceLogProvider.setProjectNameForDevice(debugData.deviceIdentifier, projectName);
80+
}
81+
}
82+
83+
return result;
6684
}
6785

6886
public async debugStart(debugData: IDebugData, debugOptions: IDebugOptions): Promise<void> {
@@ -108,7 +126,7 @@ export class IOSDebugService extends DebugServiceBase implements IPlatformDebugS
108126

109127
private async emulatorDebugBrk(debugData: IDebugData, debugOptions: IDebugOptions): Promise<string> {
110128
const args = debugOptions.debugBrk ? "--nativescript-debug-brk" : "--nativescript-debug-start";
111-
const launchResult = await this.$iOSEmulatorServices.runApplicationOnEmulator(debugData.pathToAppPackage, {
129+
const launchResult = await this.$iOSEmulatorServices.runApplicationOnEmulator(debugData.pathToAppPackage, {
112130
waitForDebugger: true,
113131
captureStdin: true,
114132
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)