<commit_before>"use strict";

var http  = require("http"),
    faye  = require("faye"),
    cp = require("child_process"),
    formatio = require("formatio"),
    logger = require("evented-logger"),
    closeWindows = require("./close-windows");


function processLog(error, stdout, stderr) {
    /*jshint validthis: true */
    if (stdout) {
        this._logger.debug(stdout);
    }
    if (stderr) {
        this._logger.error(stderr);
    }
    if (error) {
        this._logger.error(error);
    }
}

function browserClosedLog(browser) {
    /*jshint validthis: true */
    this._logger.info("browser " + browser + " closed");
}

function startBrowser(browserName, browser, url, id) {
    /*jshint validthis: true */
    if (browser.prepareStart) {
        this._logger.info("prepare start");
        var prepareStartCommand = browser.prepareStart;
        this._logger.debug(prepareStartCommand);
        cp.exec(
            prepareStartCommand,
            processLog.bind(this)
        );
    }

    var startCommand = browser.start;
    var startArgs = browser.startArgs;
    startArgs = startArgs ? startArgs.slice(0) : [];
    if (url) {
        url = id ? url + "?id=" + id : url;
        startArgs.push(url);
    }

    this._logger.info("start browser " + browserName);
    
    var process = cp.spawn(startCommand, startArgs);
    process.stdout.on('data', this._logger.debug);
    process.stderr.on('data', this._logger.error);
    process.on('close', browserClosedLog.bind(this, browserName));
    browser.process = process;
}

function stopBrowser(browserName, browser) {
    /*jshint validthis: true */
    if (browser.stop) {
        if (browser.stop.command) {
            this._logger.info("stop browser " + browserName + " by command");<commit_msg>Log the child process stdout and stderr properly<commit_after>"use strict";

var http  = require("http"),
    faye  = require("faye"),
    cp = require("child_process"),
    formatio = require("formatio"),
    logger = require("evented-logger"),
    closeWindows = require("./close-windows");


function processLog(error, stdout, stderr) {
    /*jshint validthis: true */
    if (stdout) {
        this._logger.debug(stdout);
    }
    if (stderr) {
        this._logger.error(stderr);
    }
    if (error) {
        this._logger.error(error);
    }
}

function browserClosedLog(browser) {
    /*jshint validthis: true */
    this._logger.info("browser " + browser + " closed");
}

function startBrowser(browserName, browser, url, id) {
    /*jshint validthis: true */
    var logger = this._logger;
    
    if (browser.prepareStart) {
        logger.info("prepare start");
        var prepareStartCommand = browser.prepareStart;
        logger.debug(prepareStartCommand);
        cp.exec(
            prepareStartCommand,
            processLog.bind(this)
        );
    }

    var startCommand = browser.start;
    var startArgs = browser.startArgs;
    startArgs = startArgs ? startArgs.slice(0) : [];
    if (url) {
        url = id ? url + "?id=" + id : url;
        startArgs.push(url);
    }

    logger.info("start browser " + browserName);
    
    var process = cp.spawn(startCommand, startArgs);

    process.stdout.on('data', function(buffer){
        logger.debug(buffer.toString('utf8'))
    });
    process.stderr.on('data', function(buffer){
        logger.error(buffer.toString('utf8'))
    });

    process.on('close', browserClosedLog.bind(this, browserName));
    browser.process = process;
}

function stopBrowser(browserName, browser) {
    /*jshint validthis: true */
    if (browser.stop) {
        if (browser.stop.command) {
            this._logger.info("stop browser " + browserName + " by command");
{'fraction_non_alphanumeric': 0.1426641448257228, 'fraction_numerical': 0.0010807889759524452, 'mean_word_length': 2.743174924165824, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1141469', 'n_tokens_mistral': 1117, 'n_tokens_neox': 1112, 'n_words': 225}