/*
 * Displayes log info in the response 
 * Created by: Michael Pastushkov <michael@pastushkov.com>
 */

const utils = require('./utils');

async function logcat(req, res) {

    res.setHeader('Content-Type', 'text/event-stream')
    res.setHeader('Cache-Control', 'no-cache')

    res.write('Logcat session open\n');
    res.flush(); 

    let listener = function(data) {
        res.write(`${data}\n`);
        res.flush(); 
    }

    utils.add_logcat(listener);

    res.on('close', function () {
        utils.remove_logcat(listener);
        utils.log('logcat connection closed');
    });

}

module.exports.run = logcat;

