Skip to content
This repository has been archived by the owner on May 15, 2019. It is now read-only.

Commit

Permalink
Handle missing logfiles correctly (#199)
Browse files Browse the repository at this point in the history
* Fix missing logfile error handling not working

* Fix lint
  • Loading branch information
Ace Nassri authored and jmdobry committed Nov 16, 2018
1 parent d38d0a0 commit ca9200b
Showing 1 changed file with 24 additions and 26 deletions.
50 changes: 24 additions & 26 deletions src/emulator/logs.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,37 +27,35 @@ const pkg = require('../../package.json');
const defaultLogsDir = path.join(xdgBasedir.config || os.tmpdir(), pkg.name);

function readLogLines (filePath, linesToRead, output) {
try {
const parts = path.parse(filePath);
const files = fs
.readdirSync(parts.dir)
.filter((file) => file && file.includes(parts.name));
files.sort();
const parts = path.parse(filePath);
const files = fs
.readdirSync(parts.dir)
.filter((file) => file && file.includes(parts.name));
files.sort();

// Here, we naively select the newest log file, even if the user wants to
// display more lines than are available in the newest log file.
const rl = readline.createInterface({
input: fs.createReadStream(path.join(parts.dir, files[files.length - 1])),
terminal: false
});
const lines = [];
rl
.on('line', (line) => {
lines.push(line);
})
.on('close', () => {
lines
.slice(lines.length - linesToRead)
.forEach((line) => output(`${line}\n`));
});
} catch (err) {
// Here, we naively select the newest log file, even if the user wants to
// display more lines than are available in the newest log file.
const stream = fs.createReadStream(path.join(parts.dir, files[files.length - 1] || ''));
stream.on('error', (err) => {
if (err.code === 'ENOENT') {
output('');
return;
}
});

throw err;
}
const rl = readline.createInterface({
input: stream,
terminal: false
});
const lines = [];
rl
.on('line', (line) => {
lines.push(line);
})
.on('close', () => {
lines
.slice(lines.length - linesToRead)
.forEach((line) => output(`${line}\n`));
});
}

module.exports = {
Expand Down

0 comments on commit ca9200b

Please sign in to comment.