@@ -15,8 +15,12 @@ import * as _ from "lodash";
15
15
16
16
import { IPhoneSimulatorNameGetter } from "./iphone-simulator-name-getter" ;
17
17
18
+ const osenv = require ( "osenv" ) ;
19
+
18
20
export class XCodeSimctlSimulator extends IPhoneSimulatorNameGetter implements ISimulator {
19
21
private static DEVICE_IDENTIFIER_PREFIX = "com.apple.CoreSimulator.SimDeviceType" ;
22
+ private deviceLogChildProcess : any = null ;
23
+ private isDeviceLogOperationStarted = false ;
20
24
public defaultDeviceIdentifier = "iPhone 6" ;
21
25
22
26
private simctl : ISimctl = null ;
@@ -116,11 +120,63 @@ export class XCodeSimctlSimulator extends IPhoneSimulatorNameGetter implements I
116
120
}
117
121
118
122
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 ;
120
161
}
121
162
122
163
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 ;
124
180
}
125
181
126
182
private getDeviceToRun ( ) : IDevice {
@@ -195,6 +251,12 @@ export class XCodeSimctlSimulator extends IPhoneSimulatorNameGetter implements I
195
251
}
196
252
}
197
253
254
+ private getDeviceFromIdentifier ( deviceId : string ) {
255
+ const availableDevices = this . getDevices ( ) ;
256
+
257
+ return _ . find ( availableDevices , { id : deviceId } ) ;
258
+ }
259
+
198
260
private killSimulator ( ) : void {
199
261
childProcess . execSync ( "pkill -9 -f Simulator" ) ;
200
262
}
0 commit comments