feat(log): color log

This commit is contained in:
Alexey Kasyanchuk
2018-08-07 01:21:00 +03:00
parent e7b035a1a8
commit aa68200bc5
3 changed files with 36 additions and 11 deletions

7
package-lock.json generated
View File

@ -1,6 +1,6 @@
{
"name": "rats-search",
"version": "0.26.2",
"version": "0.27.0",
"lockfileVersion": 1,
"requires": true,
"dependencies": {
@ -1964,6 +1964,11 @@
"integrity": "sha1-SlKCrBZHKek2Gbz9OtFR+BfOkfU=",
"dev": true
},
"ansi-256-colors": {
"version": "1.1.0",
"resolved": "https://registry.npmjs.org/ansi-256-colors/-/ansi-256-colors-1.1.0.tgz",
"integrity": "sha1-kQ3lDvzHwJ49gvL4er1rcAwYgYo="
},
"ansi-align": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/ansi-align/-/ansi-align-2.0.0.tgz",

View File

@ -111,6 +111,7 @@
"buildweb": "node src/background/webpack.js"
},
"dependencies": {
"ansi-256-colors": "^1.1.0",
"bencode": "^2.0.0",
"bitfield": "^2.0.0",
"compare-versions": "^3.1.0",

View File

@ -78,25 +78,44 @@ if (!fs.existsSync(app.getPath("userData"))){
const logFile = fs.createWriteStream(app.getPath("userData") + '/rats.log', {flags : 'w'});
const logStdout = process.stdout;
const colors = require('ansi-256-colors');
const stringHashCode = (str) => {
let hash = 0, i, chr;
if (str.length === 0)
return hash;
for (i = 0; i < str.length; i++) {
chr = str.charCodeAt(i);
hash = ((hash << 5) - hash) + chr;
hash |= 0; // Convert to 32bit integer
}
return hash;
};
console.log = (...d) => {
const date = (new Date).toLocaleTimeString()
logFile.write(`[${date}] ` + util.format(...d) + '\n');
logStdout.write(util.format(...d) + '\n');
};
global.logT = (type, ...d) => {
const date = (new Date).toLocaleTimeString()
logFile.write(`[${date}] [${type}] ` + util.format(...d) + '\n');
logStdout.write(colors.fg.codes[Math.abs(stringHashCode(type)) % 256] + `[${type}]` + colors.reset + ' ' + util.format(...d) + '\n');
}
// print os info
console.log('Rats', app.getVersion())
console.log('Platform:', os.platform())
console.log('Arch:', os.arch())
console.log('OS Release:', os.release())
console.log('CPU:', os.cpus()[0].model)
console.log('CPU Logic cores:', os.cpus().length)
console.log('Total memory:', (os.totalmem() / (1024 * 1024)).toFixed(2), 'MB')
console.log('Free memory:', (os.freemem() / (1024 * 1024)).toFixed(2), 'MB')
console.log('NodeJS:', process.version)
logT('system', 'Rats', app.getVersion())
logT('system', 'Platform:', os.platform())
logT('system', 'Arch:', os.arch())
logT('system', 'OS Release:', os.release())
logT('system', 'CPU:', os.cpus()[0].model)
logT('system', 'CPU Logic cores:', os.cpus().length)
logT('system', 'Total memory:', (os.totalmem() / (1024 * 1024)).toFixed(2), 'MB')
logT('system', 'Free memory:', (os.freemem() / (1024 * 1024)).toFixed(2), 'MB')
logT('system', 'NodeJS:', process.version)
if(portative)
console.log('portative compability')
logT('system', 'portative compability')
// handle promise rejections
process.on('unhandledRejection', r => console.log('Rejection:', r));