feat(log): error display with separate color #31

This commit is contained in:
Alexey Kasyanchuk 2018-08-07 22:54:36 +03:00
parent 297baac3d3
commit ddfd43498d
9 changed files with 27 additions and 15 deletions

View File

@ -106,7 +106,7 @@ module.exports = async ({
sphinx.query('SELECT count(*) AS torrents, sum(size) AS sz FROM `torrents`', function (error, rows, fields) {
if(!rows) {
console.error(error)
logTE('statistic', error)
callback(undefined)
return;
}
@ -115,7 +115,7 @@ module.exports = async ({
sphinx.query('SELECT count(*) AS files FROM `files`', function (error, rows, fields) {
if(!rows) {
console.error(error)
logTE('statistic', error)
callback(undefined)
return;
}

View File

@ -103,6 +103,12 @@ global.logT = (type, ...d) => {
logStdout.write(colors.fg.codes[Math.abs(stringHashCode(type)) % 256] + `[${type}]` + colors.reset + ' ' + util.format(...d) + '\n');
}
global.logTE = (type, ...d) => {
const date = (new Date).toLocaleTimeString()
logFile.write(`\n[${date}] [ERROR] [${type}] ` + util.format(...d) + '\n\n');
logStdout.write(colors.fg.codes[Math.abs(stringHashCode(type)) % 256] + `[${type}]` + colors.reset + ' ' + colors.fg.codes[9] + util.format(...d) + colors.reset + '\n');
}
// print os info
logT('system', 'Rats', app.getVersion())
logT('system', 'Platform:', os.platform())
@ -118,7 +124,7 @@ if(portative)
logT('system', 'portative compability')
// handle promise rejections
process.on('unhandledRejection', r => console.log('Rejection:', r));
process.on('unhandledRejection', r => logTE('system', 'Rejection:', r));
const shouldQuit = app.makeSingleInstance(function(commandLine, workingDirectory) {
// Someone tried to run a second instance, we should focus our window.

View File

@ -313,7 +313,7 @@ module.exports = async (callback, mainWindow, sphinxApp) => {
sphinx.query('select * from version', async (err, version) => {
if(err)
{
logT('patcher', 'error on version get on db patch')
logTE('patcher', 'error on version get on db patch')
return
}

View File

@ -3,7 +3,7 @@ module.exports = (sphinx, table, callback, doneCallback, max = 1000, where = '')
sphinx.query(`SELECT * FROM ${table} WHERE id > ${index} ${where} LIMIT ${max}`, (err, torrents) => {
const finish = () => {
if(err)
logT('sql', 'big table parse error', err)
logTE('sql', 'big table parse error', err)
if(doneCallback)
doneCallback(true)
done(true)

View File

@ -223,12 +223,12 @@ const single = (callback) => {
});
mysqlSingle._mysql.on('error', (err) => {
logT('sql', 'db error', err);
if(err.code === 'PROTOCOL_CONNECTION_LOST') { // Connection to the MySQL server is usually
logT('sql', 'restart single sql connection')
logT('sql', 'lost connection, restart single sql connection')
mysqlSingle._mysql = undefined
start(); // lost due to either server restart, or a
} else { // connnection idle timeout (the wait_timeout
logTE('sql', 'db error', err);
throw err; // server variable configures this)
}
});

View File

@ -32,6 +32,10 @@ global.logT = (type, ...d) => {
console.log(colors.fg.codes[Math.abs(stringHashCode(type)) % 256] + `[${type}]` + colors.reset + ' ' + util.format(...d));
}
global.logTE = (type, ...d) => {
console.log(colors.fg.codes[Math.abs(stringHashCode(type)) % 256] + `[${type}]` + colors.reset + ' ' + colors.fg.codes[9] + util.format(...d) + colors.reset + '\n');
}
server.listen(appConfig.httpPort);
logT('system', 'Rats v' + packageJson.version)
@ -48,7 +52,7 @@ logT('system', 'NodeJS:', process.version)
const majorVersion = /v?([0-9]+)\.?([0-9]+)?\.?([0-9]+)?\.?([0-9]+)?/.exec(process.version)[1]
if(majorVersion < 8)
{
logT('system', 'Minumum Node.JS version >= 8.0.0, please update and try again')
logTE('system', 'Minumum Node.JS version >= 8.0.0, please update and try again')
process.exit(1);
}

View File

@ -129,7 +129,7 @@ module.exports = function (send, recive, dataDirectory, version, env)
resolve(data.length > 0 && JSON.parse(data))
});
}).on("error", (err) => {
logT('http', `${url} error: ` + err.message)
logTE('http', `${url} error: ` + err.message)
resolve(false)
});
})
@ -225,7 +225,7 @@ module.exports = function (send, recive, dataDirectory, version, env)
sphinxSingle.query('UPDATE torrents SET seeders = ?, completed = ?, leechers = ?, trackersChecked = ? WHERE hash = ?', [seeders, completed, leechers, Math.floor(checkTime.getTime() / 1000), hash], function(err, result) {
if(!result) {
console.error(err);
logTE('udp-tracker', err);
return
}
@ -423,7 +423,7 @@ module.exports = function (send, recive, dataDirectory, version, env)
sphinxSingle.query("SELECT id FROM torrents WHERE hash = ?", torrent.hash, (err, single) => {
if(!single)
{
logT('add', err)
logTE('add', err)
resolve()
return
}
@ -462,8 +462,7 @@ module.exports = function (send, recive, dataDirectory, version, env)
}
else
{
logT('add', torrent);
console.error(err);
logTE('add', err);
}
resolve()
events.emit('insert', torrent)
@ -475,6 +474,7 @@ module.exports = function (send, recive, dataDirectory, version, env)
const {hash} = torrent
await sphinxSingle.query('DELETE FROM torrents WHERE hash = ?', hash)
await sphinxSingle.query('DELETE FROM files WHERE hash = ?', hash)
logT('remove', 'removed torrent', torrent.name || torrent.hash)
}
const updateTorrentToDB = async (torrent) => {
@ -491,6 +491,7 @@ module.exports = function (send, recive, dataDirectory, version, env)
delete torrent.filesList
await sphinxSingle.updateValues('torrents', torrent, {hash: torrent.hash})
logT('update', 'updated torrent', torrent.name)
}
const insertMetadata = (metadata, infohash, rinfo) => {
@ -551,7 +552,7 @@ module.exports = function (send, recive, dataDirectory, version, env)
{
disk.check(rootPath, function(err, info) {
if (err) {
logT('quota', err);
logTE('quota', err);
} else {
const {available, free, total} = info;

View File

@ -145,7 +145,7 @@ module.exports = class P2PStore extends EventEmitter {
(err) => {
if(err)
{
logT('store', err)
logTE('store', err)
return
}

View File

@ -1,5 +1,6 @@
import {startApplication, stopApplication} from "../tests/application";
global.logT = (...args) => {console.log(...args)}
global.logTE = (...args) => {console.log('error', ...args)}
describe("application", () => {
before(startApplication);