Skip to content

Commit 5153663

Browse files
Merge pull request #95 from telerik/kddimitrov/ios11-xcode9
Fix log in iOS11 simulators.
2 parents f7004e3 + 65f86ef commit 5153663

5 files changed

+77
-53
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, predicate?: 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

+60-4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"use strict";
33

44
import childProcess = require("./child-process");
5+
import * as child_process from "child_process";
56
import errors = require("./errors");
67

78
import common = require("./iphone-simulator-common");
@@ -15,8 +16,12 @@ import * as _ from "lodash";
1516

1617
import { IPhoneSimulatorNameGetter } from "./iphone-simulator-name-getter";
1718

19+
const osenv = require("osenv");
20+
1821
export class XCodeSimctlSimulator extends IPhoneSimulatorNameGetter implements ISimulator {
1922
private static DEVICE_IDENTIFIER_PREFIX = "com.apple.CoreSimulator.SimDeviceType";
23+
private deviceLogChildProcess: any = null;
24+
private isDeviceLogOperationStarted = false;
2025
public defaultDeviceIdentifier = "iPhone 6";
2126

2227
private simctl: ISimctl = null;
@@ -115,12 +120,57 @@ export class XCodeSimctlSimulator extends IPhoneSimulatorNameGetter implements I
115120
}
116121
}
117122

118-
public printDeviceLog(deviceId: string, launchResult?: string): any {
119-
return common.printDeviceLog(deviceId, launchResult);
123+
public printDeviceLog(deviceId: string, launchResult?: string): child_process.ChildProcess {
124+
let pid = "";
125+
let deviceLogChildProcess;
126+
127+
if (launchResult) {
128+
pid = launchResult.split(":")[1].trim();
129+
}
130+
131+
if (!this.isDeviceLogOperationStarted) {
132+
deviceLogChildProcess = this.getDeviceLogProcess(deviceId);
133+
if (deviceLogChildProcess.stdout) {
134+
deviceLogChildProcess.stdout.on("data", this.logDataHandler.bind(this, pid));
135+
}
136+
137+
if (deviceLogChildProcess.stderr) {
138+
deviceLogChildProcess.stderr.on("data", this.logDataHandler.bind(this, pid));
139+
}
140+
}
141+
142+
return deviceLogChildProcess;
143+
}
144+
145+
private logDataHandler(pid: string, logData: NodeBuffer): void {
146+
const dataAsString = logData.toString();
147+
148+
if (pid) {
149+
if (dataAsString.indexOf(`[${pid}]`) > -1) {
150+
process.stdout.write(dataAsString);
151+
}
152+
} else {
153+
process.stdout.write(dataAsString);
154+
}
120155
}
121156

122-
public getDeviceLogProcess(deviceId: string): any {
123-
return common.getDeviceLogProcess(deviceId);
157+
public getDeviceLogProcess(deviceId: string, predicate?: string): child_process.ChildProcess {
158+
if (!this.isDeviceLogOperationStarted) {
159+
const device = this.getDeviceFromIdentifier(deviceId);
160+
const deviceVersion = device ? device.runtimeVersion : "";
161+
const majorVersion = deviceVersion.split(".")[0];
162+
163+
if (majorVersion && parseInt(majorVersion) >= 11) {
164+
this.deviceLogChildProcess = this.simctl.getLog(deviceId, predicate);
165+
} else {
166+
const logFilePath = path.join(osenv.home(), "Library", "Logs", "CoreSimulator", deviceId, "system.log");
167+
this.deviceLogChildProcess = require("child_process").spawn("tail", ['-f', '-n', '1', logFilePath]);
168+
}
169+
170+
this.isDeviceLogOperationStarted = true;
171+
}
172+
173+
return this.deviceLogChildProcess;
124174
}
125175

126176
private getDeviceToRun(): IDevice {
@@ -195,6 +245,12 @@ export class XCodeSimctlSimulator extends IPhoneSimulatorNameGetter implements I
195245
}
196246
}
197247

248+
private getDeviceFromIdentifier(deviceId: string) {
249+
const availableDevices = this.getDevices();
250+
251+
return _.find(availableDevices, { id: deviceId });
252+
}
253+
198254
private killSimulator(): void {
199255
childProcess.execSync("pkill -9 -f Simulator");
200256
}

lib/simctl.ts

+15
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,6 +121,16 @@ export class Simctl implements ISimctl {
120121
return devices;
121122
}
122123

124+
public getLog(deviceId: string, predicate?: string): child_process.ChildProcess {
125+
let predicateArgs: string[] = [];
126+
127+
if (predicate) {
128+
predicateArgs = ["--predicate", predicate];
129+
}
130+
131+
return this.simctlSpawn("spawn", [deviceId, "log", "stream", "--style", "syslog"].concat(predicateArgs));
132+
}
133+
123134
private simctlExec(command: string, args: string[], opts?: any): string {
124135
const result = childProcess.spawnSync("xcrun", ["simctl", command].concat(args), opts);
125136
if (result) {
@@ -136,4 +147,8 @@ export class Simctl implements ISimctl {
136147

137148
return '';
138149
}
150+
151+
private simctlSpawn(command: string, args: string[], opts?: child_process.SpawnOptions): child_process.ChildProcess {
152+
return child_process.spawn("xcrun", ["simctl", command].concat(args), opts);
153+
}
139154
}

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ios-sim-portable",
3-
"version": "3.1.3",
3+
"version": "3.2.0",
44
"description": "",
55
"main": "./lib/ios-sim.js",
66
"scripts": {

0 commit comments

Comments
 (0)