@boost/debug
Version:
Lightweight debugging and crash reporting.
41 lines (38 loc) • 1.46 kB
JavaScript
import coreDebug from 'debug';
import { toArray } from '@boost/common';
import { env, color } from '@boost/internal';
import { debug } from './debug.mjs';
/**
* Create and return a `Debugger` instance with the defined namespace.
*/
function createDebugger(namespace) {
const globalNamespace = env('DEBUG_NAMESPACE');
const namespaces = toArray(namespace);
if (globalNamespace) {
namespaces.unshift(globalNamespace);
}
const mainNamespace = namespaces.join(':');
debug('New debugger created: %s %s', mainNamespace, env('DEBUG_VERBOSE') ? '(verbose enabled)' : '');
const logger = coreDebug(mainNamespace);
// `debug` doesn't support this on an individual namespace basis,
// so we have to manually support it using this hacky regex.
logger.disable = () => {
debug('Debugger %s disabled', mainNamespace);
process.env.DEBUG = (process.env.DEBUG ?? '').replace(new RegExp(`${logger.namespace}(:\\*)?`, 'u'), '').replace(/(^,)|(,$)/u, '').replace(',,', ',');
};
logger.enable = () => {
debug('Debugger %s enabled', mainNamespace);
coreDebug.enable(mainNamespace);
};
logger.invariant = (condition, message, pass, fail) => {
logger('%s: %s', message, condition ? color.pass(pass) : color.fail(fail));
};
logger.verbose = (message, ...args) => {
if (env('DEBUG_VERBOSE')) {
logger(message, ...args);
}
};
return logger;
}
export { createDebugger };
//# sourceMappingURL=createDebugger.mjs.map