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

class ProcessLogsApi {
  constructor({embark, processName, silent}) {
    this.embark = embark;
    this.processName = processName;
    this.logger = this.embark.logger;
    this.events = this.embark.events;
    this.logHandler = new LogHandler({events: this.events, logger: this.logger, processName: this.processName, silent});

    this.registerAPICalls();
  }

  registerAPICalls() {
    const apiRoute = '/embark-api/process-logs/' + this.processName;
    this.embark.registerAPICall(
      'ws',
      apiRoute,
      (ws, _req) => {
        this.events.on('process-log-' + this.processName, function (log) {
          ws.send(JSON.stringify(log), () => {});
        });
      }
    );
    this.embark.registerAPICall(
      'get',
      '/embark-api/process-logs/' + this.processName,
      (req, res) => {
        let limit = parseInt(req.query.limit, 10);
        if (!Number.isInteger(limit)) limit = 0;
        const result = this.logHandler.logs.map((log, id) => Object.assign(log, {id})).slice(limit);
        res.send(JSON.stringify(result));
      }
    );
  }
}

module.exports = ProcessLogsApi;

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

class ProcessLogsApi {
  constructor({embark, processName, silent}) {
    this.embark = embark;
    this.processName = processName;
    this.logger = this.embark.logger;
    this.events = this.embark.events;
    this.logHandler = new LogHandler({events: this.events, logger: this.logger, processName: this.processName, silent});

    this.registerAPICalls();
  }

  registerAPICalls() {
    const apiRoute = '/embark-api/process-logs/' + this.processName;
    this.embark.registerAPICall(
      'ws',
      apiRoute,
      (ws, _req) => {
        this.events.on('process-log-' + this.processName, function (log) {
          ws.send(JSON.stringify(log), () => {});
        });
      }
    );
    this.embark.registerAPICall(
      'get',
      '/embark-api/process-logs/' + this.processName,
      (req, res) => {
        let limit = parseInt(req.query.limit, 10);
        if (!Number.isInteger(limit)) limit = 0;
        const result = this.logHandler.logs.slice(limit * -1);
        res.send(JSON.stringify(result));
      }
    );
  }
}

module.exports = ProcessLogsApi;

Fix process logs not returning
Fix process logs not returning

The API endpoint listening for a dump of process logs was not returning logs properly for two reasons:
1. The `id` field was being appended to each log. This had been moved to the `handleLog` function of the `LogHandler`.
2. The slice needed to grab logs from the end, so the `limit` was made negative on the `.slice()`.
