1
0
mirror of https://github.com/KubaPro010/fm-dx-webserver.git synced 2026-02-26 22:13:53 +01:00
Files
fm-dx-webserver/console.js
2024-02-06 22:22:06 +01:00

62 lines
2.0 KiB
JavaScript

const verboseMode = process.argv.includes('--debug');
const getCurrentTime = () => {
const currentTime = new Date();
const hours = currentTime.getHours().toString().padStart(2, '0');
const minutes = currentTime.getMinutes().toString().padStart(2, '0');
return `\x1b[90m[${hours}:${minutes}]\x1b[0m`;
};
const MESSAGE_PREFIX = {
DEBUG: "\x1b[36m[DEBUG]\x1b[0m",
ERROR: "\x1b[31m[ERROR]\x1b[0m",
INFO: "\x1b[32m[INFO]\x1b[0m",
WARN: "\x1b[33m[WARN]\x1b[0m",
};
// Initialize an array to store logs
const logs = [];
const maxLogLines = 100;
const logDebug = (...messages) => {
if (verboseMode) {
const logMessage = `${getCurrentTime()} ${MESSAGE_PREFIX.DEBUG} ${messages.join(' ')}`;
logs.push(logMessage);
if (logs.length > maxLogLines) {
logs.shift(); // Remove the oldest log if the array exceeds the maximum number of lines
}
console.log(logMessage);
}
};
const logError = (...messages) => {
const logMessage = `${getCurrentTime()} ${MESSAGE_PREFIX.ERROR} ${messages.join(' ')}`;
logs.push(logMessage);
if (logs.length > maxLogLines) {
logs.shift(); // Remove the oldest log if the array exceeds the maximum number of lines
}
console.log(logMessage);
};
const logInfo = (...messages) => {
const logMessage = `${getCurrentTime()} ${MESSAGE_PREFIX.INFO} ${messages.join(' ')}`;
logs.push(logMessage);
if (logs.length > maxLogLines) {
logs.shift(); // Remove the oldest log if the array exceeds the maximum number of lines
}
console.log(logMessage);
};
const logWarn = (...messages) => {
const logMessage = `${getCurrentTime()} ${MESSAGE_PREFIX.WARN} ${messages.join(' ')}`;
logs.push(logMessage);
if (logs.length > maxLogLines) {
logs.shift(); // Remove the oldest log if the array exceeds the maximum number of lines
}
console.log(logMessage);
};
module.exports = {
logError, logDebug, logInfo, logWarn, logs
};