Skip to content

Commit b4a9fdb

Browse files
author
Kristian D. Dimitrov
committed
Fix log in iOS11 simulators.
1 parent 0b52a9c commit b4a9fdb

File tree

4 files changed

+75
-51
lines changed

4 files changed

+75
-51
lines changed

lib/declarations.ts

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ interface ISimctl {
3333
uninstall(deviceId: string, applicationIdentifier: string, opts?: any): void;
3434
notifyPost(deviceId: string, notification: string): void;
3535
getDevices(): IDevice[];
36+
getLog(deviceId: string): any;
3637
getAppContainer(deviceId: string, applicationIdentifier: string): string;
3738
}
3839

lib/iphone-simulator-common.ts

-48
Original file line numberDiff line numberDiff line change
@@ -36,54 +36,6 @@ export function getInstalledApplications(deviceId: string): IApplication[] {
3636
return result;
3737
}
3838

39-
export function printDeviceLog(deviceId: string, launchResult?: string): any {
40-
if (launchResult) {
41-
pid = launchResult.split(":")[1].trim();
42-
}
43-
44-
if (!isDeviceLogOperationStarted) {
45-
deviceLogChildProcess = this.getDeviceLogProcess(deviceId);
46-
if (deviceLogChildProcess.stdout) {
47-
deviceLogChildProcess.stdout.on("data", (data: NodeBuffer) => {
48-
let dataAsString = data.toString();
49-
if (pid) {
50-
if (dataAsString.indexOf(`[${pid}]`) > -1) {
51-
process.stdout.write(dataAsString);
52-
}
53-
} else {
54-
process.stdout.write(dataAsString);
55-
}
56-
});
57-
}
58-
59-
if (deviceLogChildProcess.stderr) {
60-
deviceLogChildProcess.stderr.on("data", (data: string) => {
61-
let dataAsString = data.toString();
62-
if (pid) {
63-
if (dataAsString.indexOf(`[${pid}]`) > -1) {
64-
process.stdout.write(dataAsString);
65-
}
66-
} else {
67-
process.stdout.write(dataAsString);
68-
}
69-
process.stdout.write(data.toString());
70-
});
71-
}
72-
}
73-
74-
return deviceLogChildProcess;
75-
}
76-
77-
export function getDeviceLogProcess(deviceId: string): any {
78-
if (!isDeviceLogOperationStarted) {
79-
let logFilePath = path.join(osenv.home(), "Library", "Logs", "CoreSimulator", deviceId, "system.log");
80-
deviceLogChildProcess = require("child_process").spawn("tail", ['-f', '-n', '1', logFilePath]);
81-
isDeviceLogOperationStarted = true;
82-
}
83-
84-
return deviceLogChildProcess;
85-
}
86-
8739
export function startSimulator(deviceId: string): void {
8840
let simulatorPath = path.resolve(xcode.getPathFromXcodeSelect(), "Applications", "Simulator.app");
8941
let args = ["open", simulatorPath, '--args', '-CurrentDeviceUDID', deviceId];

lib/iphone-simulator-xcode-simctl.ts

+64-2
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,12 @@ import * as _ from "lodash";
1515

1616
import { IPhoneSimulatorNameGetter } from "./iphone-simulator-name-getter";
1717

18+
const osenv = require("osenv");
19+
1820
export class XCodeSimctlSimulator extends IPhoneSimulatorNameGetter implements ISimulator {
1921
private static DEVICE_IDENTIFIER_PREFIX = "com.apple.CoreSimulator.SimDeviceType";
22+
private deviceLogChildProcess: any = null;
23+
private isDeviceLogOperationStarted = false;
2024
public defaultDeviceIdentifier = "iPhone 6";
2125

2226
private simctl: ISimctl = null;
@@ -116,11 +120,63 @@ export class XCodeSimctlSimulator extends IPhoneSimulatorNameGetter implements I
116120
}
117121

118122
public printDeviceLog(deviceId: string, launchResult?: string): any {
119-
return common.printDeviceLog(deviceId, launchResult);
123+
let pid = "";
124+
let deviceLogChildProcess;
125+
126+
if (launchResult) {
127+
pid = launchResult.split(":")[1].trim();
128+
}
129+
130+
if (!this.isDeviceLogOperationStarted) {
131+
deviceLogChildProcess = this.getDeviceLogProcess(deviceId);
132+
if (deviceLogChildProcess.stdout) {
133+
deviceLogChildProcess.stdout.on("data", (data: NodeBuffer) => {
134+
let dataAsString = data.toString();
135+
if (pid) {
136+
if (dataAsString.indexOf(`[${pid}]`) > -1 || dataAsString.indexOf(` ${pid} `) > -1) {
137+
process.stdout.write(dataAsString);
138+
}
139+
} else {
140+
process.stdout.write(dataAsString);
141+
}
142+
});
143+
}
144+
145+
if (deviceLogChildProcess.stderr) {
146+
deviceLogChildProcess.stderr.on("data", (data: string) => {
147+
let dataAsString = data.toString();
148+
if (pid) {
149+
if (dataAsString.indexOf(`[${pid}]`) > -1 || dataAsString.indexOf(` ${pid} `) > -1) {
150+
process.stdout.write(dataAsString);
151+
}
152+
} else {
153+
process.stdout.write(dataAsString);
154+
}
155+
process.stdout.write(data.toString());
156+
});
157+
}
158+
}
159+
160+
return deviceLogChildProcess;
120161
}
121162

122163
public getDeviceLogProcess(deviceId: string): any {
123-
return common.getDeviceLogProcess(deviceId);
164+
const device = this.getDeviceFromIdentifier(deviceId) || {};
165+
const deviceVersion = this.getDeviceFromIdentifier(deviceId).runtimeVersion || "";
166+
const majorVersion = deviceVersion.split(".")[0];
167+
168+
if (!this.isDeviceLogOperationStarted) {
169+
if(majorVersion && parseInt(majorVersion) >= 11) {
170+
this.deviceLogChildProcess = this.simctl.getLog(deviceId);
171+
} else {
172+
let logFilePath = path.join(osenv.home(), "Library", "Logs", "CoreSimulator", deviceId, "system.log");
173+
this.deviceLogChildProcess = require("child_process").spawn("tail", ['-f', '-n', '1', logFilePath]);
174+
}
175+
176+
this.isDeviceLogOperationStarted = true;
177+
}
178+
179+
return this.deviceLogChildProcess;
124180
}
125181

126182
private getDeviceToRun(): IDevice {
@@ -195,6 +251,12 @@ export class XCodeSimctlSimulator extends IPhoneSimulatorNameGetter implements I
195251
}
196252
}
197253

254+
private getDeviceFromIdentifier(deviceId: string) {
255+
const availableDevices = this.getDevices();
256+
257+
return _.find(availableDevices, { id: deviceId });
258+
}
259+
198260
private killSimulator(): void {
199261
childProcess.execSync("pkill -9 -f Simulator");
200262
}

lib/simctl.ts

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import childProcess = require("./child-process");
2+
import * as child_process from "child_process";
23
import errors = require("./errors");
34
import options = require("./options");
45
import * as _ from "lodash";
@@ -120,11 +121,19 @@ export class Simctl implements ISimctl {
120121
return devices;
121122
}
122123

124+
public getLog(deviceId: string): any {
125+
return this.simctlSpawn("spawn", [deviceId, "log", "stream"]);
126+
}
127+
123128
private simctlExec(command: string, args: string[], opts?: any): any {
124129
let result = childProcess.spawnSync("xcrun", ["simctl", command].concat(args), opts);
125-
if(result && result.stdout) {
130+
if (result && result.stdout) {
126131
return result.stdout.toString().trim();
127132
}
128133
return '';
129134
}
135+
136+
private simctlSpawn(command: string, args: string[], opts?: any): any {
137+
return child_process.spawn("xcrun", ["simctl", command].concat(args), opts);
138+
}
130139
}

0 commit comments

Comments
 (0)