improve debug logs

This commit is contained in:
Andre Staltz 2024-01-19 15:48:25 +02:00
parent 198b4be8e9
commit eca1e5428d
No known key found for this signature in database
GPG Key ID: 9EDE23EA7E8A4890
1 changed files with 21 additions and 3 deletions

View File

@ -32,6 +32,18 @@ function initGC(peer, config) {
let stopMonitoringLogSize = /** @type {CallableFunction | null} */ (null)
let hasCleanupScheduled = false
/**
* @param {Error} err
*/
function flattenCauseChain(err) {
let str = ''
while (err?.message ?? err) {
str += (err.message ?? err) + ': '
err = /**@type {Error}*/ (err.cause)
}
return str
}
/**
* Deletes messages that don't correspond to any goal.
* @private
@ -40,12 +52,15 @@ function initGC(peer, config) {
function cleanup(cb) {
debug('Cleanup started')
const startTime = Date.now()
const done = multicb({ pluck: 1 })
function makeRecCB(/**@type {string}*/ errorMessage) {
/**
* @param {string} explanation
*/
function makeRecCB(explanation) {
const cb = done()
return (/**@type {Error=}*/ err) => {
if (err) debug('%s: %s', errorMessage, err.message ?? err)
if (err) debug('%s: %s', explanation, flattenCauseChain(err))
cb()
}
}
@ -58,12 +73,14 @@ function initGC(peer, config) {
if (purpose === 'goal') continue // don't cleanup
if (purpose === 'none') {
const recCB = makeRecCB('Failed to delete msg when cleaning up')
debug('Deleting msg %s with purpose=none', msgID)
peer.db.del(msgID, recCB)
waiting = true
} else if (purpose === 'ghost') {
const { tangleID, span } = details
const recCB = makeRecCB('Failed to delete ghost msg when cleaning up')
// TODO: Could one msg be a ghostable in MANY tangles? Or just one?
debug('Deleting and ghosting msg %s with purpose=ghost', msgID)
peer.db.ghosts.add({ tangleID, msgID, span }, (err) => {
if (err) return recCB(err)
peer.db.del(msgID, recCB)
@ -71,6 +88,7 @@ function initGC(peer, config) {
waiting = true
} else if (purpose === 'trail') {
const recCB = makeRecCB('Failed to erase trail msg when cleaning up')
debug('Erasing msg %s with purpose=trail', msgID)
peer.db.erase(msgID, recCB)
waiting = true
} else {