forked from appium/appium-xcuitest-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: remove idevicesyslog dependency for real devices(appium#1000)
- Loading branch information
Showing
5 changed files
with
231 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { logger } from 'appium-support'; | ||
import { IOSLog } from './ios-log'; | ||
import { services } from 'appium-ios-device'; | ||
|
||
const log = logger.getLogger('IOSDeviceLog'); | ||
|
||
class IOSDeviceLog extends IOSLog { | ||
|
||
constructor (opts) { | ||
super(); | ||
this.udid = opts.udid; | ||
this.showLogs = !!opts.showLogs; | ||
this.service = null; | ||
} | ||
|
||
async startCapture () { | ||
if (this.service) { | ||
return; | ||
} | ||
this.service = await services.startSyslogService(this.udid); | ||
this.service.start(this.onLog.bind(this)); | ||
} | ||
|
||
onLog (logLine) { | ||
this.broadcast(logLine); | ||
if (this.showLogs) { | ||
log.info(logLine); | ||
} | ||
} | ||
|
||
get isCapturing () { | ||
return !!this.service; | ||
} | ||
|
||
stopCapture () { | ||
if (!this.service) { | ||
return; | ||
} | ||
this.service.close(); | ||
this.service = null; | ||
} | ||
} | ||
|
||
export { IOSDeviceLog }; | ||
export default IOSDeviceLog; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
import _ from 'lodash'; | ||
import { IOSLog } from './ios-log'; | ||
import { logger } from 'appium-support'; | ||
import { SubProcess, exec } from 'teen_process'; | ||
|
||
const log = logger.getLogger('IOSSimulatorLog'); | ||
|
||
const START_TIMEOUT = 10000; | ||
|
||
class IOSSimulatorLog extends IOSLog { | ||
constructor (opts) { | ||
super(); | ||
this.sim = opts.sim; | ||
this.showLogs = !!opts.showLogs; | ||
this.xcodeVersion = opts.xcodeVersion; | ||
this.proc = null; | ||
} | ||
|
||
async startCapture () { | ||
if (_.isUndefined(this.sim.udid)) { | ||
throw new Error(`Log capture requires a sim udid`); | ||
} | ||
|
||
if (!await this.sim.isRunning()) { | ||
throw new Error(`iOS Simulator with udid ${this.sim.udid} is not running`); | ||
} | ||
const tool = 'xcrun'; | ||
const args = ['simctl', 'spawn', this.sim.udid, 'log', 'stream', '--style', 'compact']; | ||
log.debug(`Starting log capture for iOS Simulator with udid '${this.sim.udid}', ` + | ||
`using '${tool} ${args.join(' ')}'`); | ||
try { | ||
// cleanup existing listeners if the previous session has not been terminated properly | ||
await exec('pkill', ['-xf', [tool, ...args].join(' ')]); | ||
} catch (ign) {} | ||
try { | ||
this.proc = new SubProcess(tool, args); | ||
await this.finishStartingLogCapture(); | ||
} catch (e) { | ||
throw new Error(`Simulator log capture failed. Original error: ${e.message}`); | ||
} | ||
} | ||
|
||
async finishStartingLogCapture () { | ||
if (!this.proc) { | ||
log.errorAndThrow('Could not capture simulator log'); | ||
} | ||
let firstLine = true; | ||
let logRow = ''; | ||
this.proc.on('output', (stdout, stderr) => { | ||
if (stdout) { | ||
if (firstLine) { | ||
if (stdout.endsWith('\n')) { | ||
// don't store the first line of the log because it came before the sim was launched | ||
firstLine = false; | ||
} | ||
} else { | ||
logRow += stdout; | ||
if (stdout.endsWith('\n')) { | ||
this.onOutput(logRow); | ||
logRow = ''; | ||
} | ||
} | ||
} | ||
if (stderr) { | ||
this.onOutput(logRow, 'STDERR'); | ||
} | ||
}); | ||
|
||
let sd = (stdout, stderr) => { | ||
if (/execvp\(\)/.test(stderr)) { | ||
throw new Error('iOS log capture process failed to start'); | ||
} | ||
return stdout || stderr; | ||
}; | ||
await this.proc.start(sd, START_TIMEOUT); | ||
} | ||
|
||
async stopCapture () { | ||
if (!this.proc) { | ||
return; | ||
} | ||
await this.killLogSubProcess(); | ||
this.proc = null; | ||
} | ||
|
||
async killLogSubProcess () { | ||
if (!this.proc.isRunning) { | ||
return; | ||
} | ||
log.debug('Stopping iOS log capture'); | ||
try { | ||
await this.proc.stop('SIGTERM', 1000); | ||
} catch (e) { | ||
if (!this.proc.isRunning) { | ||
return; | ||
} | ||
logger.warn('Cannot stop log capture process. Sending SIGKILL...'); | ||
await this.proc.stop('SIGKILL'); | ||
} | ||
} | ||
|
||
get isCapturing () { | ||
return this.proc && this.proc.isRunning; | ||
} | ||
|
||
onOutput (logRow, prefix = '') { | ||
const logs = _.cloneDeep(logRow.split('\n')); | ||
for (const logLine of logs) { | ||
if (!logLine) continue; // eslint-disable-line curly | ||
this.broadcast(logLine); | ||
if (this.showLogs) { | ||
const space = prefix.length > 0 ? ' ' : ''; | ||
log.info(`[IOS_SYSLOG_ROW${space}${prefix}] ${logLine}`); | ||
} | ||
} | ||
} | ||
} | ||
|
||
export { IOSSimulatorLog }; | ||
export default IOSSimulatorLog; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters