cc-haxe/tools/console.js

70 lines
1.3 KiB
JavaScript

const http = require('http');
function time() {
let now = new Date();
return `${now.getHours().toString().padStart(2, "0")}:${now.getMinutes().toString().padStart(2, 0)}:${now.getSeconds().toString().padStart(2, 0)}`;
}
const Reset = "\x1b[0m";
const FgBlack = "\x1b[30m"
const FgRed = "\x1b[31m"
const FgGreen = "\x1b[32m"
const FgYellow = "\x1b[33m"
const FgBlue = "\x1b[34m"
const FgMagenta = "\x1b[35m"
const FgCyan = "\x1b[36m"
const FgWhite = "\x1b[37m"
const FgGray = "\x1b[90m"
function log(id, lvl, msg) {
var logLvl = "UNKN";
switch (lvl) {
case "0":
logLvl = "INFO";
break;
case "1":
logLvl = FgYellow + "WARN" + Reset;
break;
case "2":
logLvl = FgRed + "ERRO" + Reset;
break;
case "3":
logLvl = FgGray + "DEBG" + Reset;
break;
case "4":
logLvl = FgWhite + "SILY" + Reset;
break;
}
console.log(`(${id})[${logLvl}]${msg}`);
}
const server = http.createServer((req, res) => {
if (req.method != "POST") {
res.writeHead(400);
return;
}
var urlParts = req.url.split("/");
var id = urlParts[2];
var lvl = urlParts[3];
let data = "";
req.on('data', chunk => {
data += chunk;
})
req.on('end', () => {
log(id, lvl, data);
res.writeHead(200);
res.end();
})
});
console.log("Webconsole running on port 8080");
server.listen(8080);