Skip to content

Commit cdddbb1

Browse files
committed
Use File Watcher
1 parent 4e7f797 commit cdddbb1

File tree

4 files changed

+38
-9
lines changed

4 files changed

+38
-9
lines changed

BrowserStack.dll

512 Bytes
Binary file not shown.

BrowserStack/BrowserStack/BrowserStackTunnel.cs

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -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

BrowserStack/BrowserStack/Local.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ private void addArgs(string key, string value)
7878
result = valueCommands.Find(pair => pair.Key == key);
7979
if (!result.Equals(emptyStringPair))
8080
{
81-
argumentString += result.Value + " '" + value + "' ";
81+
argumentString += result.Value + " " + value + " ";
8282
return;
8383
}
8484

BrowserStackExample/BrowserStackExample/Example.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ static void Main(string[] args)
4646

4747
driver.Quit();
4848
local.stop();
49+
Console.WriteLine("Test Completed.");
4950
Console.ReadLine();
5051
}
5152
}

0 commit comments

Comments
 (0)