/**
* Debug logging for keet-daemon discovery system.
* Enable with --debug flag or DEBUG=discovery environment variable.
*
* Usage:
* import { dbg, setDebug } from './debug.js'
* setDebug(true)
* dbg('discovery', 'register', { roomId, title, registryKey })
*/
let _enabled = false
export function setDebug (on) {
_enabled = !!on
}
export function isDebug () {
return _enabled
}
/**
* Log a debug message with context.
* @param {string} scope - e.g. 'discovery', 'registry-mgr', 'router', 'apply'
* @param {string} action - e.g. 'register', 'getDiscovery', 'append'
* @param {object} data - key-value pairs to log
*/
export function dbg (scope, action, data) {
if (!_enabled) return
const ts = new Date().toISOString()
const parts = [`[DBG ${ts}] ${scope}.${action}`]
if (data) {
for (const [k, v] of Object.entries(data)) {
if (v === undefined) continue
const str = typeof v === 'string' ? v : JSON.stringify(v)
// Truncate long values (keys, hex strings)
parts.push(`${k}=${str.length > 48 ? str.slice(0, 48) + '...' : str}`)
}
}
console.log(parts.join(' '))
}