From a7a75bd9d43010919aa4dc834d70db156d46e756 Mon Sep 17 00:00:00 2001 From: MrFry Date: Thu, 12 Mar 2020 22:25:26 +0100 Subject: [PATCH] Added debug logging --- modules/qmining/qmining.js | 8 ++++++++ server.js | 12 +++++++++++- utils/actions.js | 16 +++++++++++++++- utils/logger.js | 32 ++++++++++++++++++++++++++++---- 4 files changed, 62 insertions(+), 6 deletions(-) diff --git a/modules/qmining/qmining.js b/modules/qmining/qmining.js index 4eb7acd..8038918 100644 --- a/modules/qmining/qmining.js +++ b/modules/qmining/qmining.js @@ -273,6 +273,8 @@ app.post('/isAdding', function (req, res) { logger.LogReq(req) // automatically saves to dataFile every n write + logger.DebugLog('request.body:', 4) + logger.DebugLog(req.body, 4) let result = actions.ProcessIncomingRequest( req.body.datatoadd, data, @@ -299,6 +301,7 @@ app.get('/ask', function (req, res) { let subj = req.query.subj || '' let question try { + logger.DebugLog('Decoding URI component', 4) question = decodeURIComponent(req.query.q) } catch (e) { console.err(req.query) @@ -306,11 +309,16 @@ app.get('/ask', function (req, res) { } let recData = {} try { + logger.DebugLog('Parsing req.query.data', 4) recData = JSON.parse(req.query.data) } catch (e) { logger.Log(`Unable to parse recieved question data! '${req.query.data}'`, logger.GetColor('redbg')) } + logger.DebugLog('Searching in DB', 4) let r = data.Search(question, subj, recData) + logger.DebugLog('Result length: ' + r.length, 1) + logger.DebugLog('Result:', 3) + logger.DebugLog(r, 3) res.json({ result: r, diff --git a/server.js b/server.js index 9b50137..7dd86a2 100755 --- a/server.js +++ b/server.js @@ -25,7 +25,6 @@ const httpsport = 8443 const express = require('express') const vhost = require('vhost') const logger = require('./utils/logger.js') -logger.Load() const utils = require('./utils/utils.js') const http = require('http') const https = require('https') @@ -38,6 +37,17 @@ const loggableKeywords = [ ] let modules = JSON.parse(utils.ReadFile(modulesFile)) +let logLevel = parseInt(GetParams()[0]) +if (isNaN(logLevel)) { + logLevel = 0 +} +logger.Log('Loglevel is: ' + logLevel) +logger.Load(logLevel) // TODO: debug level in enviromet variable / param + +function GetParams () { + return process.argv.splice(2) +} + try { if (utils.FileExists(extraModulesFile)) { const extraModules = JSON.parse(utils.ReadFile(extraModulesFile)) diff --git a/utils/actions.js b/utils/actions.js index ff14935..a957b56 100755 --- a/utils/actions.js +++ b/utils/actions.js @@ -37,6 +37,7 @@ const writeAfter = 1 // write after # of adds FIXME: set reasonable save rate var currWrites = 0 function ProcessIncomingRequest (recievedData, qdb, infos) { + logger.DebugLog('Processing incoming request', 1) if (recievedData === undefined) { logger.Log('\tRecieved data is undefined!', logger.GetColor('redbg')) return @@ -48,6 +49,7 @@ function ProcessIncomingRequest (recievedData, qdb, infos) { towrite += recievedData towrite += '\n------------------------------------------------------------------------------\n' utils.AppendToFile(towrite, recDataFile) + logger.DebugLog('recDataFile written', 1) } catch (e) { logger.log('Error writing recieved data.') } @@ -55,20 +57,29 @@ function ProcessIncomingRequest (recievedData, qdb, infos) { try { // recievedData: { version: "", id: "", subj: "" quiz: {} } let d = JSON.parse(recievedData) + logger.DebugLog('recievedData JSON parsed', 1) let allQLength = d.quiz.length let allQuestions = [] + logger.DebugLog(logger.hr, 2) d.quiz.forEach((question) => { + logger.DebugLog('Question:', 2) + logger.DebugLog(question, 2) let q = new classes.Question(question.Q, question.A, question.data) let sames = qdb.Search(q, d.subj) + logger.DebugLog('Same questions:', 2) + logger.DebugLog('Length: ' + sames.length, 2) + logger.DebugLog(sames, 3) // if it didnt find any question, or every found questions match is lower thatn 80 let isNew = sames.length === 0 || sames.every(searchResItem => { return searchResItem.match < classes.minMatchAmmount }) + logger.DebugLog('isNew: ' + isNew, 2) if (isNew) { allQuestions.push(q) } }) + logger.DebugLog(logger.hr, 2) let color = logger.GetColor('green') let msg = '' @@ -81,15 +92,17 @@ function ProcessIncomingRequest (recievedData, qdb, infos) { }) currWrites++ - console.log(currWrites) + logger.DebugLog('currWrites for data.json: ' + currWrites, 1) if (currWrites >= writeAfter) { currWrites = 0 try { qdb.version = infos.version qdb.motd = infos.motd + logger.DebugLog('version and motd set for data.json', 3) } catch (e) { logger.Log('MOTD/Version writing/reading error!') } + logger.DebugLog('Writing data.json', 1) utils.WriteFile(JSON.stringify(qdb), dataFile) logger.Log('\tData file written', color) } @@ -106,6 +119,7 @@ function ProcessIncomingRequest (recievedData, qdb, infos) { if (d.version !== undefined) { msg += '. Version: ' + d.version } logger.Log('\t' + msg, color) + logger.DebugLog('ProcessIncomingRequest done', 1) return allQuestions.length } catch (e) { console.log(e) diff --git a/utils/logger.js b/utils/logger.js index 5ce78d0..8993bc3 100755 --- a/utils/logger.js +++ b/utils/logger.js @@ -18,14 +18,18 @@ ------------------------------------------------------------------------- */ +const hr = '---------------------------------------------------------------------------------' + module.exports = { GetDateString: GetDateString, Log: Log, + DebugLog: DebugLog, GetColor: GetColor, LogReq: LogReq, LogStat: LogStat, Load: Load, - logHashed: logHashed + logHashed: logHashed, + hr: hr } const DELIM = C('green') + '|' + C() @@ -43,6 +47,7 @@ const writeInterval = 10 let data = {} let vData = {} let writes = 0 +let debugLevel = 0 const colors = [ 'green', @@ -66,9 +71,25 @@ function GetDateString () { return GetRandomColor(m.getHours().toString()) + d + C() } +function DebugLog (msg, lvl) { + if (lvl <= debugLevel) { + let s = msg + let header = C('red') + '#DEBUG ' + lvl + '#' + C() + DELIM + C() + if (typeof msg !== 'object') { + s = header + msg + } else { + Log(header + 'OBJECT:', 'yellow') + } + Log(s, 'yellow') + } +} + function Log (s, c) { - let dl = DELIM + C(c) - let log = C(c) + GetDateString() + dl + s + let log = s + if (typeof s !== 'object') { + let dl = DELIM + C(c) + log = C(c) + GetDateString() + dl + s + } console.log(log) utils.AppendToFile(log, logFile) @@ -132,7 +153,10 @@ function setNoLogReadInterval () { parseNoLogFile(utils.ReadFile(nologFile)) } -function Load () { +function Load (debugLvl) { + if (debugLvl) { + debugLevel = debugLvl + } Log('Loading logger...') try { var prevData = utils.ReadFile(statFile)