mirror of
https://github.com/shanmiteko/LotteryAutoScript.git
synced 2026-06-04 21:01:17 +08:00
feat: 日志细节优化
- 下载进度条 - iso+08 - 结构调整缓存变量
This commit is contained in:
parent
c56ec7a2c2
commit
4d90d83b67
48
lib/utils.js
48
lib/utils.js
@ -103,11 +103,7 @@ const utils = {
|
||||
*/
|
||||
delay(time = 1000) {
|
||||
utils.log.info('时延', `${~~time}ms`);
|
||||
return new Promise(resolve => {
|
||||
setTimeout(() => {
|
||||
resolve();
|
||||
}, time);
|
||||
});
|
||||
return new Promise(resolve => setTimeout(resolve, time));
|
||||
},
|
||||
/**
|
||||
* 计数器 0..Infinity
|
||||
@ -185,13 +181,19 @@ const utils = {
|
||||
},
|
||||
/**日志 */
|
||||
log: {
|
||||
level: 3,
|
||||
_level: 3,
|
||||
_colors: [
|
||||
chalk.hex('#64B3FF'), chalk.grey, chalk.hex('#FFA500'),
|
||||
chalk.hex('#0070BB'), chalk.hex('#48BB31'), chalk.hex('#BBBB23'), chalk.hex('#FF0006')
|
||||
],
|
||||
_iso_time: () => new Date(Date.now() + 288e5).toISOString().slice(0, -1) + '+08'
|
||||
,
|
||||
/**
|
||||
* 初始化默认level为3
|
||||
*/
|
||||
init() {
|
||||
let _level = Number(process.env.LOTTERY_LOG_LEVEL);
|
||||
this.level = isNaN(_level) ? 3 : _level;
|
||||
this._level = isNaN(_level) ? 3 : _level;
|
||||
},
|
||||
/**
|
||||
* @param {String|Array<String>} msg
|
||||
@ -211,26 +213,36 @@ const utils = {
|
||||
const
|
||||
colors = ['red', 'yellow', 'green', 'cyan', 'blue', 'magenta'],
|
||||
colorsCount = colors.length;
|
||||
|
||||
this.proPrint(msg.map(it => it.split('').map((l, i) => chalk[colors[i % colorsCount]](l)).join('')), '\n')
|
||||
},
|
||||
/**
|
||||
* @param {number} done
|
||||
* @param {number} total
|
||||
* @param {number} size
|
||||
*/
|
||||
progress_bar(done, total, size = 30) {
|
||||
let perc = done >= total ? 1 : done / total,
|
||||
bar = ~~(perc * size),
|
||||
status_bar = `\r[${"=".repeat(bar) + ">" + " ".repeat(size - bar)}] ${(perc * 100 + ' ').slice(0, 4)}%`
|
||||
process.stdout.write(status_bar)
|
||||
},
|
||||
debug(context, msg) {
|
||||
if (this.level > 3) {
|
||||
if (this._level > 3) {
|
||||
if (msg instanceof Object) msg = JSON.stringify(msg, null, 4);
|
||||
this.proPrint([chalk.hex('#64B3FF')(`[${new Date(Date.now() + 288e5).toISOString()}]`), chalk.grey("[Debug]"), chalk.hex('#FFA500')(`[${context}]`), chalk.hex('#0070BB')(`[\n${msg}\n]`)])
|
||||
this.proPrint([this._colors[0](`[${this._iso_time()}]`), this._colors[1]("[Debug]"), this._colors[2](`[${context}]`), this._colors[3](`[\n${msg}\n]`)])
|
||||
}
|
||||
},
|
||||
info(context, msg) {
|
||||
if (this.level > 2)
|
||||
this.proPrint([chalk.hex('#64B3FF')(`[${new Date(Date.now() + 288e5).toISOString()}]`), chalk.grey("[Info]"), chalk.hex('#FFA500')(`[${context}]`), chalk.hex('#48BB31')(`[${msg}]`)])
|
||||
if (this._level > 2)
|
||||
this.proPrint([this._colors[0](`[${this._iso_time()}]`), this._colors[1]("[Info]"), this._colors[2](`[${context}]`), this._colors[4](`[${msg}]`)])
|
||||
},
|
||||
warn(context, msg) {
|
||||
if (this.level > 1)
|
||||
this.proPrint([chalk.hex('#64B3FF')(`[${new Date(Date.now() + 288e5).toISOString()}]`), chalk.grey("[Warn]"), chalk.hex('#FFA500')(`[${context}]`), chalk.hex('#BBBB23')(`[\n${msg}\n]`)])
|
||||
if (this._level > 1)
|
||||
this.proPrint([this._colors[0](`[${this._iso_time()}]`), this._colors[1]("[Warn]"), this._colors[2](`[${context}]`), this._colors[5](`[\n${msg}\n]`)])
|
||||
},
|
||||
error(context, msg) {
|
||||
if (this.level > 0)
|
||||
this.proPrint([chalk.hex('#64B3FF')(`[${new Date(Date.now() + 288e5).toISOString()}]`), chalk.grey("[Error]"), chalk.hex('#FFA500')(`[${context}]`), chalk.hex('#FF0006')(`[\n${msg}\n]`)])
|
||||
if (this._level > 0)
|
||||
this.proPrint([this._colors[0](`[${this._iso_time()}]`), this._colors[1]("[Error]"), this._colors[2](`[${context}]`), this._colors[6](`[\n${msg}\n]`)])
|
||||
}
|
||||
},
|
||||
/**
|
||||
@ -249,12 +261,12 @@ const utils = {
|
||||
retry: false
|
||||
},
|
||||
success: ({ headers, resStream }) => {
|
||||
const total_len = Number(headers["content-length"]);
|
||||
const total_len = Number(headers["content-length"]) || 16000000;
|
||||
let recv_length = 0;
|
||||
const wtbs = fs.createWriteStream(file_name);
|
||||
resStream.on('data', chuck => {
|
||||
recv_length += chuck.length
|
||||
utils.log.proPrint(total_len ? `已下载${(recv_length * 100 / total_len + '').slice(0, 5)}%` : `已收到:${recv_length} Bytes`)
|
||||
utils.log.progress_bar(recv_length, total_len)
|
||||
})
|
||||
resStream.pipe(wtbs)
|
||||
wtbs.on('finish', () => {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user