std/index.js

66 lines
1.9 KiB
JavaScript
Raw Permalink Normal View History

2023-01-24 22:50:10 +01:00
const { appendFile, existsSync } = require('node:fs');
const { mkdir } = require('node:fs/promises')
2023-01-24 22:52:53 +01:00
var readlineSync = require('@thundernetworkrad/readline-sync');
2023-01-24 22:50:10 +01:00
var debug, logs1;
let time = getTime()
const name = `${time.year}.${time.month}.${time.day}.${time.hours}.${time.minutes}.${time.seconds}.log`;
2022-11-02 19:48:49 +01:00
function getTime () {
const date_time = new Date();
const year = date_time.getFullYear();
const month = ("0" + (date_time.getMonth() + 1)).slice(-2);
const day = ("0" + date_time.getDate()).slice(-2);
const hours = date_time.getHours();
const minutes = date_time.getMinutes();
const seconds = date_time.getSeconds();
return({year, month, day, hours, minutes, seconds})
};
function log (string) {
2023-01-24 22:50:10 +01:00
if (!existsSync('./logs')) mkdir('./logs', { recursive: true });
appendFile(`./logs/${name}`, string+'\n', err => {
if (err) {
throw err
}
2022-11-02 19:48:49 +01:00
})
};
function cin (question, hide) {
return new Promise((resolve, reject) => {
try {
var answer = readlineSync.question(`${question} `, {
hideEchoBack: hide || false
})
return resolve(answer);
} catch (e) {
console.log(e)
return resolve('error');
}
})
};
2023-01-24 17:04:50 +01:00
class createCout {
constructor (debugLevel, logs) {
2023-01-24 17:29:16 +01:00
if (!debugLevel) debugLevel = 0;
if (!logs) logs = false;
2023-01-24 17:16:59 +01:00
debug = debugLevel;
logs1 = logs;
2023-01-24 17:04:50 +01:00
}
cout (string, debugLevel) {
if (!debugLevel) debugLevel = 0;
var time = getTime();
2023-01-24 17:16:59 +01:00
if (debug >= debugLevel) {
2023-01-24 17:04:50 +01:00
console.log(`[${time.year}.${time.month}.${time.day}-${time.hours}:${time.minutes}:${time.seconds}] | ${String(string)}`);
2023-01-24 17:16:59 +01:00
if (logs1) {
2023-01-24 22:50:10 +01:00
log(`[${time.year}.${time.month}.${time.day}-${time.hours}:${time.minutes}:${time.seconds}] | ${String(string)}`);
2023-01-24 17:04:50 +01:00
}
}
return;
};
}
2022-11-02 19:48:49 +01:00
2023-01-24 22:50:10 +01:00
module.exports = { cin, createCout, log };