-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherixConf.js
60 lines (49 loc) · 1.63 KB
/
erixConf.js
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
60
const fs = require('fs');
const moment = require('moment');
exports.erixConf = class {
constructor(type, log) {
if (!fs.existsSync('./log')) {
fs.mkdirSync('./log');
}
this.cfgPath = './cfg.' + type + '.json';
this.loadConf();
this.log = log;
}
loadConf() {
try {
var rows = fs.readFileSync(this.cfgPath);
this.conf = JSON.parse(rows);
} catch (ex) {
this.saveLog(3, 'Failed read config file ' + this.cfgPath + ':' + ex.message);
}
}
saveConf() {
try {
fs.writeFileSync(this.cfgPath, JSON.stringify(this.conf, null, '\t'));
}
catch (ex) {
this.saveLog(3, 'Failed write config file ' + this.cfgPath + ':' + ex.message);
}
}
saveLog(level, title, msg) {
//3:error, 2:warning, 1: notice
try {
var datePart = moment().format('yyyy-MM-DD') + ' ' + moment().format('HH:mm:ss')
title = datePart + "\t" + title;
if (!fs.existsSync(this.log.path)) {
fs.writeFileSync(this.log.path, datePart + "\t" + "Begin Log\r\n");
}
fs.appendFileSync(this.log.path, title + '\r\n');
if (msg) {
fs.appendFileSync(this.log.path, JSON.stringify(msg, null, '\t') + '\r\n');
}
if (this.log.show) {
console.log(title);
if (msg) { console.log(JSON.stringify(msg, null, '\t')); }
}
}
catch (ex) {
console.log("Failed log file:" + ex.message);
}
};
}