Add endpoint to obtain logs

It goes through a WebSocket; the secret is sent, and if valid, the logs
are sent back.
This commit is contained in:
Thaddee Tyl
2017-11-08 23:39:44 +01:00
parent e61373e739
commit 9cb9113a34
3 changed files with 57 additions and 9 deletions

View File

@@ -1,5 +1,7 @@
'use strict';
const listeners = [];
// Zero-pad a number in a string.
// eg. 4 becomes 04 but 17 stays 17.
function pad(string) {
@@ -19,9 +21,23 @@ function date() {
}
module.exports = function log(...msg) {
console.log(date(), ...msg);
const d = date();
listeners.forEach(f => f(d, ...msg))
console.log(d, ...msg);
};
module.exports.error = function error(...msg) {
console.error(date(), ...msg);
const d = date();
listeners.forEach(f => f(d, ...msg))
console.error(d, ...msg);
};
module.exports.addListener = function addListener(func) {
listeners.push(func);
};
module.exports.removeListener = function removeListener(func) {
const index = listeners.indexOf(func);
if (index < 0) { return; }
listeners.splice(index, 1);
};