@@ -30,6 +30,8 @@ public class BrowserStackTunnel : IDisposable
3030
3131 protected StringBuilder output ;
3232 public LocalState localState ;
33+ protected string logFilePath = "" ;
34+ protected FileSystemWatcher logfileWatcher ;
3335
3436 Job job = null ;
3537 Process process = null ;
@@ -120,6 +122,10 @@ public virtual void Run(string accessKey, string folder, string logFilePath)
120122 process . Close ( ) ;
121123 }
122124
125+ if ( File . Exists ( logFilePath ) )
126+ {
127+ File . WriteAllText ( logFilePath , string . Empty ) ;
128+ }
123129 Local . logger . Info ( "BrowserStackLocal binary is located at " + binaryAbsolute ) ;
124130 Local . logger . Info ( "Starting Binary with arguments " + arguments . Replace ( accessKey , "<access_key>" ) ) ;
125131 ProcessStartInfo processStartInfo = new ProcessStartInfo ( )
@@ -190,8 +196,30 @@ public virtual void Run(string accessKey, string folder, string logFilePath)
190196
191197 private void readFile ( string filename )
192198 {
193- using ( Stream fileStream = File . Open ( filename , FileMode . Create ) )
194- fileStream . Write ( new Byte [ 1 ] , 0 , 1 ) ;
199+ logFilePath = filename ;
200+ if ( File . Exists ( filename ) )
201+ {
202+ readAlreadyPresentFile ( filename ) ;
203+ }
204+ else
205+ {
206+ logfileWatcher = new FileSystemWatcher ( new FileInfo ( filename ) . Directory . FullName ) ;
207+ logfileWatcher . Created += readAlreadyPresentFile ;
208+ logfileWatcher . Changed += readAlreadyPresentFile ;
209+ logfileWatcher . EnableRaisingEvents = true ;
210+ }
211+ }
212+
213+ private void readAlreadyPresentFile ( object sender , FileSystemEventArgs e )
214+ {
215+ if ( e . FullPath . Equals ( logFilePath ) )
216+ {
217+ readAlreadyPresentFile ( e . FullPath ) ;
218+ }
219+ }
220+
221+ private void readAlreadyPresentFile ( string filename )
222+ {
195223 using ( FileStream fs = File . Open ( filename , FileMode . Open , FileAccess . Read , FileShare . ReadWrite ) )
196224 {
197225 byte [ ] bytes = new byte [ 1024 ] ;
@@ -224,7 +252,6 @@ private void TunnelStateChanged(LocalState prevState, LocalState state)
224252 {
225253 connectingEvent . Set ( ) ;
226254 }
227- Local . logger . Info ( "Current tunnel state " + state ) ;
228255 }
229256
230257 public bool IsConnected ( )
@@ -236,10 +263,12 @@ public virtual void Kill()
236263 {
237264 try
238265 {
239- this . process . Kill ( ) ;
240- this . process = null ;
241- this . job . Close ( ) ;
242- this . job = null ;
266+ if ( process != null )
267+ process . Kill ( ) ;
268+ process = null ;
269+ if ( job != null )
270+ job . Close ( ) ;
271+ job = null ;
243272 }
244273 catch ( Exception e )
245274 {
@@ -251,7 +280,6 @@ public virtual void Kill()
251280 TunnelStateChanged ( localState , LocalState . Disconnected ) ;
252281
253282 localState = LocalState . Disconnected ;
254- Local . logger . Info ( "TunnelState: " + localState . ToString ( ) ) ;
255283 }
256284 }
257285
0 commit comments