From 9072183dd1dd6a727cdc0cfca974507d41e12ab6 Mon Sep 17 00:00:00 2001 From: Tamius Han Date: Tue, 16 Jul 2019 20:59:12 +0200 Subject: [PATCH] Added logger --- src/ext/lib/Logger.js | 103 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100644 src/ext/lib/Logger.js diff --git a/src/ext/lib/Logger.js b/src/ext/lib/Logger.js new file mode 100644 index 0000000..08b6d0b --- /dev/null +++ b/src/ext/lib/Logger.js @@ -0,0 +1,103 @@ +class Logger { + constructor(conf) { + this.conf = conf; + this.history = []; + this.startTime = performance.now(); + } + + clear() { + this.log = []; + this.startTime = performance.now(); + } + + setConf(conf) { + this.conf = conf; + } + + // allow syncing of start times between bg and page scripts. + // may result in negative times in the log file, but that doesn't + // really matter + getStartTime() { + return this.startTime; + } + setStartTime(startTime) { + if (startTime) { + this.startTime = startTime; + } else { + this.startTime = performance.now(); + } + } + + getLogFileString() { + let logfileStr = ''; + let logTs = ''; // number of seconds since extension started on a given page¸ + for (let i = 0; i < this.history.length; i++) { + logTs = ((this.history[i].ts - Math.floor(this.performance.now)) / 3).toFixed(3); + logfileStr = `${logfileStr}[@${logTs}] -- ${this.history[i].message}\n` + } + + return logfileStr; + } + + // level is unused as of now, but this may change in the future + log(level, component, ...message) { + if (this.conf.logToFile) { + if (this.conf.fileOptions[component]) { + let ts = performance.now(); + if (ts <= this.history[this.history.length - 1]) { + ts = this.history[this.history.length - 1] + 0.00001; + } + + this.history.push({ + ts: ts, + message: JSON.stringify(message), + }) + } + } + if (this.conf.logToConsole) { + if (this.conf.consoleOptions[component]) { + console.log(...message); + } + } + } + + // leaves a noticeable mark in the file log at the time it got triggered, as well as + // at the intervals of 1s and .5s before the trigger moment + cahen() { + if (this.conf.logToFile) { + let ts = performance.now(); + let secondMark = ts - 1000; + let halfSecondMark = ts - 500; + let i = this.history.length(); + + // correct ts _after_ secondMark and halfSecondMark were determined + if (ts <= this.history[this.history.length - 1]) { + ts = this.history[this.history.length - 1] + 0.00001; + } + + this.history.push({ + ts: ts, + message: "-------------------------------------- CAHEN --------------------------------------" + }); + + // find the spot for the half second mark. In this case, we don't really particularly care whether timestamps + // are duped due to cahen warnings + while (this.history[i--].ts > halfSecondMark) {} + this.history.push({ + ts: halfSecondMark, + message: "-------------------------------------- 0.5s to CAHEN --------------------------------------" + }); + + // repeat for one second warning + while (this.history[i--].ts > secondMark) {} + this.history.push({ + ts: secondMark, + message: "-------------------------------------- 1s to CAHEN --------------------------------------" + }); + } + } + + +} + +export default Logger;