fix(log): color log (part 3)

This commit is contained in:
Alexey Kasyanchuk 2018-08-07 02:41:28 +03:00
parent bf62a2de16
commit 9981c97065
5 changed files with 38 additions and 38 deletions

View File

@ -285,7 +285,7 @@ class Spider extends Emiter {
this.udp = dgram.createSocket('udp4') this.udp = dgram.createSocket('udp4')
this.udp.bind(port) this.udp.bind(port)
this.udp.on('listening', () => { this.udp.on('listening', () => {
console.log(`Listen DHT protocol on ${this.udp.address().address}:${this.udp.address().port}`) logT('spider', `Listen DHT protocol on ${this.udp.address().address}:${this.udp.address().port}`)
}) })
this.udp.on('message', (data, addr) => { this.udp.on('message', (data, addr) => {
this.parse(data, addr) this.parse(data, addr)

View File

@ -15,7 +15,7 @@ const requests = {};
let message = function (buf, host, port) { let message = function (buf, host, port) {
server.send(buf, 0, buf.length, port, host, function(err, bytes) { server.send(buf, 0, buf.length, port, host, function(err, bytes) {
if (err) { if (err) {
console.log(err.message); logT('udp-tracker', err.message);
} }
}); });
}; };
@ -69,7 +69,7 @@ let scrapeTorrent = function (connectionIdHigh, connectionIdLow, transactionId)
message(buffer, connection.host, connection.port); message(buffer, connection.host, connection.port);
} catch(error) } catch(error)
{ {
console.log('ERROR on scrape', error) logT('udp-tracker', 'ERROR on scrape', error)
} }
}; };
@ -112,7 +112,7 @@ server.on("message", function (msg, rinfo) {
delete requests[transactionId]; delete requests[transactionId];
} else if (action === ACTION_ERROR) { } else if (action === ACTION_ERROR) {
delete requests[transactionId]; delete requests[transactionId];
console.log("error in scrape response"); logT('udp-tracker', "error in scrape response");
} }
}); });
@ -125,7 +125,7 @@ let getPeersStatistic = (host, port, hash, callback) => {
server.on("listening", function () { server.on("listening", function () {
var address = server.address(); var address = server.address();
console.log("listening udp tracker respose on " + address.address + ":" + address.port); logT('udp-tracker', "listening udp tracker respose on " + address.address + ":" + address.port);
}); });
server.bind(config.udpTrackersPort); server.bind(config.udpTrackersPort);

View File

@ -103,12 +103,12 @@ module.exports = async (callback, mainWindow, sphinxApp) => {
} }
const patch = async (version) => { const patch = async (version) => {
console.log('db version', version) logT('patcher', 'db version', version)
switch(version) switch(version)
{ {
case 1: case 1:
{ {
console.log('patch db to version 2') logT('patcher', 'patch db to version 2')
openPatchWindow() openPatchWindow()
let i = 1 let i = 1
@ -116,7 +116,7 @@ module.exports = async (callback, mainWindow, sphinxApp) => {
const files = (await sphinx.query("SELECT COUNT(*) AS c FROM files"))[0].c const files = (await sphinx.query("SELECT COUNT(*) AS c FROM files"))[0].c
await forBigTable(sphinx, 'torrents', async (torrent) => { await forBigTable(sphinx, 'torrents', async (torrent) => {
console.log('update index', torrent.id, torrent.name, '[', i, 'of', torrents, ']') logT('patcher', 'update index', torrent.id, torrent.name, '[', i, 'of', torrents, ']')
if(patchWindow) if(patchWindow)
patchWindow.webContents.send('reindex', {field: torrent.name, index: i++, all: torrents, torrent: true}) patchWindow.webContents.send('reindex', {field: torrent.name, index: i++, all: torrents, torrent: true})
@ -126,7 +126,7 @@ module.exports = async (callback, mainWindow, sphinxApp) => {
}) })
i = 1 i = 1
await forBigTable(sphinx, 'files', async (file) => { await forBigTable(sphinx, 'files', async (file) => {
console.log('update index', file.id, file.path, '[', i, 'of', files, ']') logT('patcher', 'update index', file.id, file.path, '[', i, 'of', files, ']')
if(patchWindow) if(patchWindow)
patchWindow.webContents.send('reindex', {field: file.path, index: i++, all: files}) patchWindow.webContents.send('reindex', {field: file.path, index: i++, all: files})
@ -141,13 +141,13 @@ module.exports = async (callback, mainWindow, sphinxApp) => {
{ {
openPatchWindow() openPatchWindow()
console.log('optimizing torrents') logT('patcher', 'optimizing torrents')
if(patchWindow) if(patchWindow)
patchWindow.webContents.send('optimize', {field: 'torrents'}) patchWindow.webContents.send('optimize', {field: 'torrents'})
sphinx.query(`OPTIMIZE INDEX torrents`) sphinx.query(`OPTIMIZE INDEX torrents`)
await sphinxApp.waitOptimized('torrents') await sphinxApp.waitOptimized('torrents')
console.log('optimizing files') logT('patcher', 'optimizing files')
if(patchWindow) if(patchWindow)
patchWindow.webContents.send('optimize', {field: 'files'}) patchWindow.webContents.send('optimize', {field: 'files'})
sphinx.query(`OPTIMIZE INDEX files`) sphinx.query(`OPTIMIZE INDEX files`)
@ -165,7 +165,7 @@ module.exports = async (callback, mainWindow, sphinxApp) => {
let i = 1 let i = 1
const torrents = (await sphinx.query("SELECT COUNT(*) AS c FROM torrents"))[0].c const torrents = (await sphinx.query("SELECT COUNT(*) AS c FROM torrents"))[0].c
await forBigTable(sphinx, 'torrents', async (torrent) => { await forBigTable(sphinx, 'torrents', async (torrent) => {
console.log('update index', torrent.id, torrent.name, '[', i, 'of', torrents, '] - delete:', bad) logT('patcher', 'update index', torrent.id, torrent.name, '[', i, 'of', torrents, '] - delete:', bad)
if(patchWindow) if(patchWindow)
patchWindow.webContents.send('reindex', {field: torrent.name, index: i++, all: torrents, torrent: true}) patchWindow.webContents.send('reindex', {field: torrent.name, index: i++, all: torrents, torrent: true})
@ -177,7 +177,7 @@ module.exports = async (callback, mainWindow, sphinxApp) => {
torrentTypeDetect(torrent, torrent.filesList) torrentTypeDetect(torrent, torrent.filesList)
if(torrent.contentType == 'bad') if(torrent.contentType == 'bad')
{ {
console.log('remove bad torrent', torrent.name) logT('patcher', 'remove bad torrent', torrent.name)
bad++ bad++
await sphinx.query(`DELETE FROM torrents WHERE hash = '${torrent.hash}'`) await sphinx.query(`DELETE FROM torrents WHERE hash = '${torrent.hash}'`)
await sphinx.query(`DELETE FROM files WHERE hash = '${torrent.hash}'`) await sphinx.query(`DELETE FROM files WHERE hash = '${torrent.hash}'`)
@ -185,7 +185,7 @@ module.exports = async (callback, mainWindow, sphinxApp) => {
} }
}) })
console.log('removed', bad, 'torrents') logT('patcher', 'removed', bad, 'torrents')
await setVersion(4) await setVersion(4)
} }
@ -200,7 +200,7 @@ module.exports = async (callback, mainWindow, sphinxApp) => {
let patch = 1 let patch = 1
await forBigTable(sphinx, 'torrents', async (torrent) => { await forBigTable(sphinx, 'torrents', async (torrent) => {
console.log('remember index', torrent.id, torrent.name, '[', i, 'of', torrents, ']') logT('patcher', 'remember index', torrent.id, torrent.name, '[', i, 'of', torrents, ']')
if(patchWindow) if(patchWindow)
patchWindow.webContents.send('reindex', {field: torrent.name, index: i++, all: torrents, torrent: true}) patchWindow.webContents.send('reindex', {field: torrent.name, index: i++, all: torrents, torrent: true})
@ -209,7 +209,7 @@ module.exports = async (callback, mainWindow, sphinxApp) => {
if(torrentsArray.length >= 20000) if(torrentsArray.length >= 20000)
{ {
fs.writeFileSync(`${sphinxApp.directoryPath}/torrents.patch.${patch++}`, JSON.stringify(torrentsArray, null, 4), 'utf8'); fs.writeFileSync(`${sphinxApp.directoryPath}/torrents.patch.${patch++}`, JSON.stringify(torrentsArray, null, 4), 'utf8');
console.log('write torrents dump', `${sphinxApp.directoryPath}/torrents.patch.${patch-1}`) logT('patcher', 'write torrents dump', `${sphinxApp.directoryPath}/torrents.patch.${patch-1}`)
torrentsArray = [] torrentsArray = []
} }
}) })
@ -217,7 +217,7 @@ module.exports = async (callback, mainWindow, sphinxApp) => {
if(torrentsArray.length > 0) if(torrentsArray.length > 0)
{ {
fs.writeFileSync(`${sphinxApp.directoryPath}/torrents.patch.${patch}`, JSON.stringify(torrentsArray, null, 4), 'utf8'); fs.writeFileSync(`${sphinxApp.directoryPath}/torrents.patch.${patch}`, JSON.stringify(torrentsArray, null, 4), 'utf8');
console.log('write torrents dump', `${sphinxApp.directoryPath}/torrents.patch.${patch}`) logT('patcher', 'write torrents dump', `${sphinxApp.directoryPath}/torrents.patch.${patch}`)
torrentsArray = [] torrentsArray = []
} }
else else
@ -232,19 +232,19 @@ module.exports = async (callback, mainWindow, sphinxApp) => {
sphinxApp.stop(resolve, true) sphinxApp.stop(resolve, true)
}) })
console.log('sphinx stoped for patching') logT('patcher', 'sphinx stoped for patching')
await new Promise((resolve) => { await new Promise((resolve) => {
glob(`${sphinxApp.directoryPathDb}/torrents.*`, function (er, files) { glob(`${sphinxApp.directoryPathDb}/torrents.*`, function (er, files) {
files.forEach(file => { files.forEach(file => {
console.log('clear torrents file', file) logT('patcher', 'clear torrents file', file)
fs.unlinkSync(path.resolve(file)) fs.unlinkSync(path.resolve(file))
}) })
resolve() resolve()
}) })
}) })
console.log('cleaned torrents db structure, rectreating again') logT('patcher', 'cleaned torrents db structure, rectreating again')
i = 1 i = 1
await new Promise((resolve) => { await new Promise((resolve) => {
// reopen sphinx // reopen sphinx
@ -254,14 +254,14 @@ module.exports = async (callback, mainWindow, sphinxApp) => {
}) // same args }) // same args
}) })
console.log('sphinx restarted, patch db now') logT('patcher', 'sphinx restarted, patch db now')
for(let k = 1; k <= patch; k++) for(let k = 1; k <= patch; k++)
{ {
torrentsArray = JSON.parse(fs.readFileSync(`${sphinxApp.directoryPath}/torrents.patch.${k}`, 'utf8')) torrentsArray = JSON.parse(fs.readFileSync(`${sphinxApp.directoryPath}/torrents.patch.${k}`, 'utf8'))
console.log('read torrents dump', `${sphinxApp.directoryPath}/torrents.patch.${k}`) logT('patcher', 'read torrents dump', `${sphinxApp.directoryPath}/torrents.patch.${k}`)
await asyncForEach(torrentsArray, async (torrent) => { await asyncForEach(torrentsArray, async (torrent) => {
console.log('update index', torrent.id, torrent.name, '[', i, 'of', torrents, ']') logT('patcher', 'update index', torrent.id, torrent.name, '[', i, 'of', torrents, ']')
if(patchWindow) if(patchWindow)
patchWindow.webContents.send('reindex', {field: torrent.name, index: i++, all: torrents, torrent: true}) patchWindow.webContents.send('reindex', {field: torrent.name, index: i++, all: torrents, torrent: true})
@ -274,7 +274,7 @@ module.exports = async (callback, mainWindow, sphinxApp) => {
await new Promise((resolve) => { await new Promise((resolve) => {
glob(`${sphinxApp.directoryPath}/torrents.patch.*`, function (er, files) { glob(`${sphinxApp.directoryPath}/torrents.patch.*`, function (er, files) {
files.forEach(file => { files.forEach(file => {
console.log('clear dump file', file) logT('patcher', 'clear dump file', file)
fs.unlinkSync(path.resolve(file)) fs.unlinkSync(path.resolve(file))
}) })
resolve() resolve()
@ -283,7 +283,7 @@ module.exports = async (callback, mainWindow, sphinxApp) => {
torrentsArray = null torrentsArray = null
console.log('optimizing torrents') logT('patcher', 'optimizing torrents')
if(patchWindow) if(patchWindow)
patchWindow.webContents.send('optimize', {field: 'torrents'}) patchWindow.webContents.send('optimize', {field: 'torrents'})
sphinx.query(`OPTIMIZE INDEX torrents`) sphinx.query(`OPTIMIZE INDEX torrents`)
@ -292,7 +292,7 @@ module.exports = async (callback, mainWindow, sphinxApp) => {
await setVersion(5) await setVersion(5)
} }
} }
console.log('db patch done') logT('patcher', 'db patch done')
sphinx.destroy() sphinx.destroy()
if(patchWindow) if(patchWindow)
{ {
@ -306,14 +306,14 @@ module.exports = async (callback, mainWindow, sphinxApp) => {
// init of db, we can set version to last // init of db, we can set version to last
if(sphinxApp && sphinxApp.isInitDb) if(sphinxApp && sphinxApp.isInitDb)
{ {
console.log('new db, set version to last version', currentVersion) logT('patcher', 'new db, set version to last version', currentVersion)
await setVersion(currentVersion) await setVersion(currentVersion)
} }
sphinx.query('select * from version', async (err, version) => { sphinx.query('select * from version', async (err, version) => {
if(err) if(err)
{ {
console.log('error on version get on db patch') logT('patcher', 'error on version get on db patch')
return return
} }
@ -324,17 +324,17 @@ module.exports = async (callback, mainWindow, sphinxApp) => {
const ver = parseInt(fs.readFileSync(`${sphinxApp.directoryPath}/version.vrs`)) const ver = parseInt(fs.readFileSync(`${sphinxApp.directoryPath}/version.vrs`))
if(ver > 0) if(ver > 0)
{ {
console.log('readed version from version.vrs', ver) logT('patcher', 'readed version from version.vrs', ver)
patch(ver) patch(ver)
} }
else else
{ {
console.log('error: bad version in version.vrs') logT('patcher', 'error: bad version in version.vrs')
} }
} }
else else
{ {
console.log('version not founded, set db version to 1') logT('patcher', 'version not founded, set db version to 1')
await setVersion(1) await setVersion(1)
patch(1) patch(1)
} }

View File

@ -156,7 +156,7 @@ const single = (callback) => {
mysqlSingle._mysql.connect((mysqlError) => { mysqlSingle._mysql.connect((mysqlError) => {
if (mysqlError) { if (mysqlError) {
console.error('error connecting: ' + mysqlError.stack); logT('sql', 'error connecting: ' + mysqlError.stack);
return; return;
} }

View File

@ -17,7 +17,7 @@ torrentClient.saveSession = (sessionFile) => {
torrentClient.loadSession = (sessionFile) => { torrentClient.loadSession = (sessionFile) => {
if(!fs.existsSync(sessionFile)) if(!fs.existsSync(sessionFile))
{ {
console.log('no download sessions - ignore') logT('downloader', 'no download sessions - ignore')
return return
} }
@ -25,26 +25,26 @@ torrentClient.loadSession = (sessionFile) => {
const obj = JSON.parse(data); const obj = JSON.parse(data);
if(!obj.torrents) if(!obj.torrents)
{ {
console.log('no torrents list for loading session') logT('downloader', 'no torrents list for loading session')
return return
} }
if(!torrentClient._add) if(!torrentClient._add)
{ {
console.log('no overriden _add() method') logT('downloader', 'no overriden _add() method')
return return
} }
const {torrents} = obj const {torrents} = obj
torrents.forEach(({torrent, infoHash, path, removeOnDone, paused}) => { torrents.forEach(({torrent, infoHash, path, removeOnDone, paused}) => {
if(!torrent || !infoHash || !path) if(!torrent || !infoHash || !path)
{ {
console.log('no info for starting download this torrent') logT('downloader', 'no info for starting download this torrent')
return return
} }
console.log('restore download session:', torrent.name) logT('downloader', 'restore download session:', torrent.name)
const download = torrentClient._add(torrent, path) const download = torrentClient._add(torrent, path)
if(download) if(download)
{ {
console.log('restore options') logT('downloader', 'restore options')
// restore options // restore options
download.removeOnDone = removeOnDone download.removeOnDone = removeOnDone
if(paused) if(paused)