mirror of
https://github.com/burrow-dev/burrow-server.git
synced 2026-11-18 17:27:50 -05:00
Brought up to date with the private repository (a06d3c5)
This commit is contained in:
commit
eda992fcff
144 files changed
+31026
No files matched your search
+33
@@ -0,0 +1,33 @@
|
||||
"use strict";
|
||||
|
||||
// keeps the most recent lines the server printed, for the admin panel's log
|
||||
// view. requiring this module is what starts the capture.
|
||||
|
||||
const MAX_LINES = 5000;
|
||||
const MAX_LINE_LENGTH = 2000;
|
||||
|
||||
const lines = []; // {id, time, text}
|
||||
let nextId = 1;
|
||||
|
||||
const capture = original => (...args) => {
|
||||
original(...args);
|
||||
const text = args.map(arg => (typeof arg === "string" ? arg : String(arg && arg.stack ? arg.stack : JSON.stringify(arg)))).join(" ");
|
||||
for (const line of text.split("\n")) {
|
||||
lines.push({id: nextId++, time: Date.now(), text: line.slice(0, MAX_LINE_LENGTH)});
|
||||
}
|
||||
if (lines.length > MAX_LINES) {
|
||||
lines.splice(0, lines.length - MAX_LINES);
|
||||
}
|
||||
};
|
||||
|
||||
console.log = capture(console.log.bind(console));
|
||||
console.error = capture(console.error.bind(console));
|
||||
|
||||
// lines newer than id `after` whose text contains `filter` (any case), newest
|
||||
// `limit` of them, oldest first.
|
||||
const recent = (after = 0, filter = "", limit = 500) => {
|
||||
const wanted = filter.toLowerCase();
|
||||
return lines.filter(line => line.id > after && (!wanted || line.text.toLowerCase().includes(wanted))).slice(-limit);
|
||||
};
|
||||
|
||||
module.exports = {recent, count: () => nextId - 1};
|
||||
Reference in new issue
Block a user