Skip to content

Commit cfb2d97

Browse files
committed
Use file-logging directly from local binary
1 parent 85d4136 commit cfb2d97

File tree

6 files changed

+133
-68
lines changed

6 files changed

+133
-68
lines changed

lib/browserStackTunnel.js

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
var childProcess = require('child_process'),
22
ZipBinary = require('./ZipBinary'),
3-
Helper = require('./helper');
3+
Helper = require('./helper'),
4+
Tail = require('tail').Tail;
45

56
function 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

lib/helper.js

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
var fs = require('fs'),
2-
path = require('path'),
1+
var path = require('path'),
32
log = require('npmlog'),
43
os = require('os');
54

@@ -21,8 +20,7 @@ function helper() {
2120
const osHomedir = require('os-homedir');
2221
const basePaths = [ path.join(osHomedir(), '.browserstack'), path.join(__dirname, '..'), getTempDir() ];
2322
var currentBaseIndex = 0;
24-
var basePath = basePaths[0],
25-
appendLogs = true;
23+
var basePath = basePaths[0];
2624

2725
const localBinaryName = 'BrowserStackLocal' + ( this.getPlatform() === 'win32' ? '.exe' : '' );
2826
const logFileName = 'local.log';
@@ -49,23 +47,12 @@ function helper() {
4947

5048
if(process.env.NODE_ENV === 'testing') {
5149
log.level = 'silent';
52-
appendLogs = false;
5350
}
5451
this.log = log;
5552

56-
var getLogFilePath = function() {
53+
this.getLogFilePath = function() {
5754
return path.resolve(path.join(basePath, logFileName));
5855
};
59-
this.logBinaryOutput = function(log) {
60-
if(appendLogs) {
61-
fs.appendFile(getLogFilePath(), log, function (err) {
62-
if(err) {
63-
exports.log.warn('Error Ssaving log file to ' + getLogFilePath());
64-
appendLogs = false;
65-
}
66-
});
67-
}
68-
};
6956
}
7057

7158
exports.helper = helper;

node-example.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ var options = {
3232
};
3333

3434
local.start(options, function(error) {
35+
if(error) {
36+
console.log("Got Error From Local " + error);
37+
process.exit();
38+
}
3539
console.log('Is Running ' + local.isRunning());
3640
console.log('Started');
3741

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
"dependencies": {
1818
"npmlog": "2.0.2",
1919
"os-homedir": "^1.0.1",
20+
"tail": "^0.4.0",
2021
"unzip": "0.1.11"
2122
},
2223
"devDependencies": {

0 commit comments

Comments
 (0)