From cc6995e0308c8dbc2fdb5d4d0826cdda8c14a284 Mon Sep 17 00:00:00 2001 From: "Kristian D. Dimitrov" Date: Tue, 3 Oct 2017 19:35:53 +0300 Subject: [PATCH 1/3] Fix log in iOS11 simulators. --- lib/declarations.ts | 1 + lib/iphone-simulator-common.ts | 48 -------------------- lib/iphone-simulator-xcode-simctl.ts | 66 +++++++++++++++++++++++++++- lib/simctl.ts | 9 ++++ 4 files changed, 74 insertions(+), 50 deletions(-) diff --git a/lib/declarations.ts b/lib/declarations.ts index 33fa51a..59319cb 100644 --- a/lib/declarations.ts +++ b/lib/declarations.ts @@ -33,6 +33,7 @@ interface ISimctl { uninstall(deviceId: string, applicationIdentifier: string, opts?: any): void; notifyPost(deviceId: string, notification: string): void; getDevices(): IDevice[]; + getLog(deviceId: string): any; getAppContainer(deviceId: string, applicationIdentifier: string): string; } diff --git a/lib/iphone-simulator-common.ts b/lib/iphone-simulator-common.ts index 3069681..f1d7eb2 100644 --- a/lib/iphone-simulator-common.ts +++ b/lib/iphone-simulator-common.ts @@ -36,54 +36,6 @@ export function getInstalledApplications(deviceId: string): IApplication[] { return result; } -export function printDeviceLog(deviceId: string, launchResult?: string): any { - if (launchResult) { - pid = launchResult.split(":")[1].trim(); - } - - if (!isDeviceLogOperationStarted) { - deviceLogChildProcess = this.getDeviceLogProcess(deviceId); - if (deviceLogChildProcess.stdout) { - deviceLogChildProcess.stdout.on("data", (data: NodeBuffer) => { - let dataAsString = data.toString(); - if (pid) { - if (dataAsString.indexOf(`[${pid}]`) > -1) { - process.stdout.write(dataAsString); - } - } else { - process.stdout.write(dataAsString); - } - }); - } - - if (deviceLogChildProcess.stderr) { - deviceLogChildProcess.stderr.on("data", (data: string) => { - let dataAsString = data.toString(); - if (pid) { - if (dataAsString.indexOf(`[${pid}]`) > -1) { - process.stdout.write(dataAsString); - } - } else { - process.stdout.write(dataAsString); - } - process.stdout.write(data.toString()); - }); - } - } - - return deviceLogChildProcess; -} - -export function getDeviceLogProcess(deviceId: string): any { - if (!isDeviceLogOperationStarted) { - let logFilePath = path.join(osenv.home(), "Library", "Logs", "CoreSimulator", deviceId, "system.log"); - deviceLogChildProcess = require("child_process").spawn("tail", ['-f', '-n', '1', logFilePath]); - isDeviceLogOperationStarted = true; - } - - return deviceLogChildProcess; -} - export function startSimulator(deviceId: string): void { let simulatorPath = path.resolve(xcode.getPathFromXcodeSelect(), "Applications", "Simulator.app"); let args = ["open", simulatorPath, '--args', '-CurrentDeviceUDID', deviceId]; diff --git a/lib/iphone-simulator-xcode-simctl.ts b/lib/iphone-simulator-xcode-simctl.ts index ae8b051..1ed3c27 100755 --- a/lib/iphone-simulator-xcode-simctl.ts +++ b/lib/iphone-simulator-xcode-simctl.ts @@ -15,8 +15,12 @@ import * as _ from "lodash"; import { IPhoneSimulatorNameGetter } from "./iphone-simulator-name-getter"; +const osenv = require("osenv"); + export class XCodeSimctlSimulator extends IPhoneSimulatorNameGetter implements ISimulator { private static DEVICE_IDENTIFIER_PREFIX = "com.apple.CoreSimulator.SimDeviceType"; + private deviceLogChildProcess: any = null; + private isDeviceLogOperationStarted = false; public defaultDeviceIdentifier = "iPhone 6"; private simctl: ISimctl = null; @@ -116,11 +120,63 @@ export class XCodeSimctlSimulator extends IPhoneSimulatorNameGetter implements I } public printDeviceLog(deviceId: string, launchResult?: string): any { - return common.printDeviceLog(deviceId, launchResult); + let pid = ""; + let deviceLogChildProcess; + + if (launchResult) { + pid = launchResult.split(":")[1].trim(); + } + + if (!this.isDeviceLogOperationStarted) { + deviceLogChildProcess = this.getDeviceLogProcess(deviceId); + if (deviceLogChildProcess.stdout) { + deviceLogChildProcess.stdout.on("data", (data: NodeBuffer) => { + let dataAsString = data.toString(); + if (pid) { + if (dataAsString.indexOf(`[${pid}]`) > -1 || dataAsString.indexOf(` ${pid} `) > -1) { + process.stdout.write(dataAsString); + } + } else { + process.stdout.write(dataAsString); + } + }); + } + + if (deviceLogChildProcess.stderr) { + deviceLogChildProcess.stderr.on("data", (data: string) => { + let dataAsString = data.toString(); + if (pid) { + if (dataAsString.indexOf(`[${pid}]`) > -1 || dataAsString.indexOf(` ${pid} `) > -1) { + process.stdout.write(dataAsString); + } + } else { + process.stdout.write(dataAsString); + } + process.stdout.write(data.toString()); + }); + } + } + + return deviceLogChildProcess; } public getDeviceLogProcess(deviceId: string): any { - return common.getDeviceLogProcess(deviceId); + const device = this.getDeviceFromIdentifier(deviceId) || {}; + const deviceVersion = this.getDeviceFromIdentifier(deviceId).runtimeVersion || ""; + const majorVersion = deviceVersion.split(".")[0]; + + if (!this.isDeviceLogOperationStarted) { + if(majorVersion && parseInt(majorVersion) >= 11) { + this.deviceLogChildProcess = this.simctl.getLog(deviceId); + } else { + let logFilePath = path.join(osenv.home(), "Library", "Logs", "CoreSimulator", deviceId, "system.log"); + this.deviceLogChildProcess = require("child_process").spawn("tail", ['-f', '-n', '1', logFilePath]); + } + + this.isDeviceLogOperationStarted = true; + } + + return this.deviceLogChildProcess; } private getDeviceToRun(): IDevice { @@ -195,6 +251,12 @@ export class XCodeSimctlSimulator extends IPhoneSimulatorNameGetter implements I } } + private getDeviceFromIdentifier(deviceId: string) { + const availableDevices = this.getDevices(); + + return _.find(availableDevices, { id: deviceId }); + } + private killSimulator(): void { childProcess.execSync("pkill -9 -f Simulator"); } diff --git a/lib/simctl.ts b/lib/simctl.ts index 9684d2a..8d05ce2 100644 --- a/lib/simctl.ts +++ b/lib/simctl.ts @@ -1,4 +1,5 @@ import childProcess = require("./child-process"); +import * as child_process from "child_process"; import errors = require("./errors"); import options = require("./options"); import * as _ from "lodash"; @@ -120,6 +121,10 @@ export class Simctl implements ISimctl { return devices; } + public getLog(deviceId: string): any { + return this.simctlSpawn("spawn", [deviceId, "log", "stream"]); + } + private simctlExec(command: string, args: string[], opts?: any): string { const result = childProcess.spawnSync("xcrun", ["simctl", command].concat(args), opts); if (result) { @@ -136,4 +141,8 @@ export class Simctl implements ISimctl { return ''; } + + private simctlSpawn(command: string, args: string[], opts?: any): any { + return child_process.spawn("xcrun", ["simctl", command].concat(args), opts); + } } From f2ccf131bc1250d14c799e460d23b1a249683e6c Mon Sep 17 00:00:00 2001 From: "Kristian D. Dimitrov" Date: Mon, 9 Oct 2017 17:57:10 +0300 Subject: [PATCH 2/3] Fix comments for iOS11 Simulator log. --- lib/declarations.ts | 2 +- lib/iphone-simulator-xcode-simctl.ts | 52 ++++++++++++---------------- lib/simctl.ts | 12 +++++-- 3 files changed, 33 insertions(+), 33 deletions(-) diff --git a/lib/declarations.ts b/lib/declarations.ts index 59319cb..4d1da80 100644 --- a/lib/declarations.ts +++ b/lib/declarations.ts @@ -33,7 +33,7 @@ interface ISimctl { uninstall(deviceId: string, applicationIdentifier: string, opts?: any): void; notifyPost(deviceId: string, notification: string): void; getDevices(): IDevice[]; - getLog(deviceId: string): any; + getLog(deviceId: string, predicate?: string): any; getAppContainer(deviceId: string, applicationIdentifier: string): string; } diff --git a/lib/iphone-simulator-xcode-simctl.ts b/lib/iphone-simulator-xcode-simctl.ts index 1ed3c27..b656fd0 100755 --- a/lib/iphone-simulator-xcode-simctl.ts +++ b/lib/iphone-simulator-xcode-simctl.ts @@ -2,6 +2,7 @@ "use strict"; import childProcess = require("./child-process"); +import * as child_process from "child_process"; import errors = require("./errors"); import common = require("./iphone-simulator-common"); @@ -119,7 +120,7 @@ export class XCodeSimctlSimulator extends IPhoneSimulatorNameGetter implements I } } - public printDeviceLog(deviceId: string, launchResult?: string): any { + public printDeviceLog(deviceId: string, launchResult?: string): child_process.ChildProcess { let pid = ""; let deviceLogChildProcess; @@ -130,46 +131,39 @@ export class XCodeSimctlSimulator extends IPhoneSimulatorNameGetter implements I if (!this.isDeviceLogOperationStarted) { deviceLogChildProcess = this.getDeviceLogProcess(deviceId); if (deviceLogChildProcess.stdout) { - deviceLogChildProcess.stdout.on("data", (data: NodeBuffer) => { - let dataAsString = data.toString(); - if (pid) { - if (dataAsString.indexOf(`[${pid}]`) > -1 || dataAsString.indexOf(` ${pid} `) > -1) { - process.stdout.write(dataAsString); - } - } else { - process.stdout.write(dataAsString); - } - }); + deviceLogChildProcess.stdout.on("data", this.logDataHandler.bind(this, pid)); } if (deviceLogChildProcess.stderr) { - deviceLogChildProcess.stderr.on("data", (data: string) => { - let dataAsString = data.toString(); - if (pid) { - if (dataAsString.indexOf(`[${pid}]`) > -1 || dataAsString.indexOf(` ${pid} `) > -1) { - process.stdout.write(dataAsString); - } - } else { - process.stdout.write(dataAsString); - } - process.stdout.write(data.toString()); - }); + deviceLogChildProcess.stderr.on("data", this.logDataHandler.bind(this, pid)); } } return deviceLogChildProcess; } - public getDeviceLogProcess(deviceId: string): any { - const device = this.getDeviceFromIdentifier(deviceId) || {}; - const deviceVersion = this.getDeviceFromIdentifier(deviceId).runtimeVersion || ""; - const majorVersion = deviceVersion.split(".")[0]; + private logDataHandler(pid: string, logData: NodeBuffer): void { + const dataAsString = logData.toString(); + if (pid) { + if (dataAsString.indexOf(`[${pid}]`) > -1) { + process.stdout.write(dataAsString); + } + } else { + process.stdout.write(dataAsString); + } + } + + public getDeviceLogProcess(deviceId: string, predicate?: string): child_process.ChildProcess { if (!this.isDeviceLogOperationStarted) { - if(majorVersion && parseInt(majorVersion) >= 11) { - this.deviceLogChildProcess = this.simctl.getLog(deviceId); + const device = this.getDeviceFromIdentifier(deviceId); + const deviceVersion = device ? device.runtimeVersion : ""; + const majorVersion = deviceVersion.split(".")[0]; + + if (majorVersion && parseInt(majorVersion) >= 11) { + this.deviceLogChildProcess = this.simctl.getLog(deviceId, predicate); } else { - let logFilePath = path.join(osenv.home(), "Library", "Logs", "CoreSimulator", deviceId, "system.log"); + const logFilePath = path.join(osenv.home(), "Library", "Logs", "CoreSimulator", deviceId, "system.log"); this.deviceLogChildProcess = require("child_process").spawn("tail", ['-f', '-n', '1', logFilePath]); } diff --git a/lib/simctl.ts b/lib/simctl.ts index 8d05ce2..83e6b04 100644 --- a/lib/simctl.ts +++ b/lib/simctl.ts @@ -121,8 +121,14 @@ export class Simctl implements ISimctl { return devices; } - public getLog(deviceId: string): any { - return this.simctlSpawn("spawn", [deviceId, "log", "stream"]); + public getLog(deviceId: string, predicate?: string): child_process.ChildProcess { + let predicateArgs: string[] = []; + + if (predicate) { + predicateArgs = ["--predicate", predicate]; + } + + return this.simctlSpawn("spawn", [deviceId, "log", "stream", "--style", "syslog"].concat(predicateArgs)); } private simctlExec(command: string, args: string[], opts?: any): string { @@ -142,7 +148,7 @@ export class Simctl implements ISimctl { return ''; } - private simctlSpawn(command: string, args: string[], opts?: any): any { + private simctlSpawn(command: string, args: string[], opts?: child_process.SpawnOptions): child_process.ChildProcess { return child_process.spawn("xcrun", ["simctl", command].concat(args), opts); } } From 65f86ef3a29e4bed9d05232891a8262a6dd7b5f2 Mon Sep 17 00:00:00 2001 From: "Kristian D. Dimitrov" Date: Fri, 13 Oct 2017 14:25:54 +0300 Subject: [PATCH 3/3] Bump version to 3.2.0 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 925557c..9b86867 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ios-sim-portable", - "version": "3.1.3", + "version": "3.2.0", "description": "", "main": "./lib/ios-sim.js", "scripts": {