11var childProcess = require ( 'child_process' ) ,
22 ZipBinary = require ( './ZipBinary' ) ,
3- Helper = require ( './helper' ) ;
3+ Helper = require ( './helper' ) ,
4+ Tail = require ( 'tail' ) . Tail ;
45
56function BrowserStackTunnel ( options ) {
67 var helper = new Helper . helper ( ) ;
@@ -19,7 +20,11 @@ function BrowserStackTunnel(options) {
1920 }
2021 } ,
2122 doneStop = function ( params ) {
22- if ( stopCallback !== null ) {
23+ if ( this . tail ) {
24+ this . tail . unwatch ( ) ;
25+ this . tail = null ;
26+ }
27+ if ( stopCallback ) {
2328 stopCallback ( params ) ;
2429 }
2530 } ;
@@ -28,6 +33,7 @@ function BrowserStackTunnel(options) {
2833
2934 this . stdoutData = '' ;
3035 this . tunnel = null ;
36+ this . tail = null ;
3137
3238 if ( options . hosts ) {
3339 params . push ( options . hosts ) ;
@@ -83,7 +89,6 @@ function BrowserStackTunnel(options) {
8389 } ;
8490
8591 this . updateState = function ( data ) {
86- helper . logBinaryOutput ( data . toString ( ) ) ;
8792 var state ;
8893 this . stdoutData += data . toString ( ) ;
8994 for ( state in this . stateMatchers ) {
@@ -124,6 +129,10 @@ function BrowserStackTunnel(options) {
124129 this . tunnel . kill ( ) ;
125130 this . tunnel = null ;
126131 }
132+ if ( this . tail ) {
133+ this . tail . unwatch ( ) ;
134+ this . tail = null ;
135+ }
127136 } ;
128137
129138 this . exit = function ( ) {
@@ -142,10 +151,18 @@ function BrowserStackTunnel(options) {
142151 this . _startTunnel = function ( ) {
143152 var binaryPath = helper . getBinaryPath ( ) ;
144153 this . cleanUp ( ) ;
154+
155+ var logFilePath = helper . getLogFilePath ( ) ;
156+ params . push ( '-logFile' , logFilePath ) ;
157+
145158 log . info ( 'Local Binary is located at ' + binaryPath ) ;
146159 log . info ( 'Local started with args: ' + JSON . stringify ( params ) ) ;
160+
161+ this . tail = new Tail ( logFilePath ) ;
162+ this . tail . on ( 'line' , this . updateState . bind ( this ) ) ;
163+ this . tail . on ( 'error' , this . updateState . bind ( this ) ) ;
164+
147165 this . tunnel = childProcess . spawn ( binaryPath , binary . args . concat ( [ options . key ] ) . concat ( params ) ) ;
148- this . tunnel . stdout . on ( 'data' , this . updateState . bind ( this ) ) ;
149166 this . tunnel . stderr . on ( 'data' , this . updateState . bind ( this ) ) ;
150167 this . tunnel . on ( 'error' , this . killTunnel . bind ( this ) ) ;
151168 this . tunnel . on ( 'exit' , this . exit . bind ( this ) ) ;
@@ -181,6 +198,10 @@ function BrowserStackTunnel(options) {
181198 stopCallback = callback ;
182199 } else if ( this . state !== 'started' ) {
183200 var err = new Error ( 'child not started' ) ;
201+ if ( this . tail ) {
202+ this . tail . unwatch ( ) ;
203+ this . tail = null ;
204+ }
184205 callback ( err ) ;
185206 }
186207
0 commit comments