2
2
"use strict" ;
3
3
4
4
import childProcess = require( "./child-process" ) ;
5
+ import * as child_process from "child_process" ;
5
6
import errors = require( "./errors" ) ;
6
7
7
8
import common = require( "./iphone-simulator-common" ) ;
@@ -15,8 +16,12 @@ import * as _ from "lodash";
15
16
16
17
import { IPhoneSimulatorNameGetter } from "./iphone-simulator-name-getter" ;
17
18
19
+ const osenv = require ( "osenv" ) ;
20
+
18
21
export class XCodeSimctlSimulator extends IPhoneSimulatorNameGetter implements ISimulator {
19
22
private static DEVICE_IDENTIFIER_PREFIX = "com.apple.CoreSimulator.SimDeviceType" ;
23
+ private deviceLogChildProcess : any = null ;
24
+ private isDeviceLogOperationStarted = false ;
20
25
public defaultDeviceIdentifier = "iPhone 6" ;
21
26
22
27
private simctl : ISimctl = null ;
@@ -115,12 +120,57 @@ export class XCodeSimctlSimulator extends IPhoneSimulatorNameGetter implements I
115
120
}
116
121
}
117
122
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
+ }
120
155
}
121
156
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 ;
124
174
}
125
175
126
176
private getDeviceToRun ( ) : IDevice {
@@ -195,6 +245,12 @@ export class XCodeSimctlSimulator extends IPhoneSimulatorNameGetter implements I
195
245
}
196
246
}
197
247
248
+ private getDeviceFromIdentifier ( deviceId : string ) {
249
+ const availableDevices = this . getDevices ( ) ;
250
+
251
+ return _ . find ( availableDevices , { id : deviceId } ) ;
252
+ }
253
+
198
254
private killSimulator ( ) : void {
199
255
childProcess . execSync ( "pkill -9 -f Simulator" ) ;
200
256
}
0 commit comments