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

Handle missing logfiles correctly #199

Merged
merged 2 commits into from
Nov 16, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 24 additions & 26 deletions src/emulator/logs.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,37 +20,35 @@ const path = require('path');
const readline = require('readline');

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