1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/**
 * dxLogger module
 * To replace the `console.log` function, allowing logs to be viewed in the corresponding VSCode plugin during debugging, 
 * with support for three levels of logging: `debug`,`info`, and `error`.
 * Supports printing various data types in JavaScript.
 */
import dxCommon from './dxCommon.js'
const logger = {}
 
logger.config = {
    level: 0, // default is all,if<0,no print
}
logger.debug = function (...data) {
    if (this.config.level === 0) {
        log("DEBUG ", data)
    }
}
logger.info = function (...data) {
    if ([0, 1].includes(this.config.level)) {
        log("INFO ", data)
    }
}
logger.error = function (...data) {
    if ([0, 1, 2].includes(this.config.level)) {
        log("ERROR ", data)
    }
}
//-----------------------------------private----------------------
function log(level, messages) {
    let message = messages.map(msg => getContent(msg)).join(' ');
    let content = `[${level}${getTime()}]: ${message}`
    dxCommon.systemBrief(`echo '${content}'`)
}
function getContent(message) {
    if (message === undefined) {
        return 'undefined'
    } else if (message === null) {
        return 'null'
    }
    if ((typeof message) == 'object') {
        if (Object.prototype.toString.call(message) === '[object Error]') {
            return message.message + '\n' + message.stack
        }
        return JSON.stringify(message)
    }
    return message
}
function getTime() {
    const now = new Date();
    const year = now.getFullYear();
    const month = ('0' + (now.getMonth() + 1)).slice(-2);
    const day = ('0' + now.getDate()).slice(-2);
    const hours = ('0' + now.getHours()).slice(-2);
    const minutes = ('0' + now.getMinutes()).slice(-2);
    const seconds = ('0' + now.getSeconds()).slice(-2);
    const milliseconds = ('0' + now.getMilliseconds()).slice(-3);
    return year + '-' + month + '-' + day + ' ' + hours + ':' + minutes + ':' + seconds + '.' + milliseconds;
}
export default logger