From 3a6cd01a6c69ae6206715714b7a4c628bb4d0a6a Mon Sep 17 00:00:00 2001 From: Michael FIG Date: Mon, 5 Feb 2024 10:30:09 -0600 Subject: [PATCH] fixup! fix: DEBUG harmony --- docs/env.md | 4 +- packages/agoric-cli/src/anylogger-agoric.js | 26 +++++++----- .../cosmic-swingset/src/anylogger-agoric.js | 40 ++++++++++--------- 3 files changed, 41 insertions(+), 29 deletions(-) diff --git a/docs/env.md b/docs/env.md index a34a55189901..da851d07369d 100644 --- a/docs/env.md +++ b/docs/env.md @@ -58,7 +58,9 @@ for a given context, in order of increasing severity: 4. `console.warn` 5. `console.error` -If not set, then default (`console.info` and above) logging is enabled. +If `$DEBUG` is unset or non-empty, then default (`console.log` and above) logging is enabled. (`console.debug` logging is disabled.) + +If `$DEBUG` is set to an empty string, then quiet (`console.info` and above) logging is enabled. (`console.log` and `console.debug` logging is disabled.) Otherwise, set to a comma-separated list of strings. diff --git a/packages/agoric-cli/src/anylogger-agoric.js b/packages/agoric-cli/src/anylogger-agoric.js index 5124c0f305e7..2f7d6b5c303e 100644 --- a/packages/agoric-cli/src/anylogger-agoric.js +++ b/packages/agoric-cli/src/anylogger-agoric.js @@ -1,12 +1,17 @@ -import { getEnvironmentOptionsList } from '@endo/env-options'; +import { + getEnvironmentOption, + getEnvironmentOptionsList, +} from '@endo/env-options'; import anylogger from 'anylogger'; import chalk from 'chalk'; -// Turn on debugging output with DEBUG=agoric or DEBUG=agoric:${level} - const DEBUG_LIST = getEnvironmentOptionsList('DEBUG'); -let selectedLevel = 'info'; +// Turn on debugging output with DEBUG=agoric or DEBUG=agoric:${level} +let selectedLevel = + DEBUG_LIST.length || getEnvironmentOption('DEBUG', 'unset') === 'unset' + ? 'log' + : 'info'; for (const level of DEBUG_LIST) { const parts = level.split(':'); if (parts[0] !== 'agoric') { @@ -18,22 +23,23 @@ for (const level of DEBUG_LIST) { selectedLevel = 'debug'; } } -const defaultLevel = anylogger.levels[selectedLevel]; +const selectedCode = anylogger.levels[selectedLevel]; +const globalCode = selectedCode === undefined ? -Infinity : selectedCode; const oldExt = anylogger.ext; anylogger.ext = (l, o) => { l = oldExt(l, o); - l.enabledFor = lvl => defaultLevel >= anylogger.levels[lvl]; + l.enabledFor = lvl => globalCode >= anylogger.levels[lvl]; const prefix = l.name.replace(/:/g, ': '); for (const [level, code] of Object.entries(anylogger.levels)) { - if (code > defaultLevel) { - // Disable printing. - l[level] = () => {}; - } else { + if (globalCode >= code) { // Enable the printing with a prefix. const doLog = l[level] || (() => {}); l[level] = (...args) => doLog(chalk.bold.blue(`${prefix}:`), ...args); + } else { + // Disable printing. + l[level] = () => {}; } } return l; diff --git a/packages/cosmic-swingset/src/anylogger-agoric.js b/packages/cosmic-swingset/src/anylogger-agoric.js index 8e6e40412987..8199ddcea771 100644 --- a/packages/cosmic-swingset/src/anylogger-agoric.js +++ b/packages/cosmic-swingset/src/anylogger-agoric.js @@ -1,4 +1,7 @@ -import { getEnvironmentOptionsList } from '@endo/env-options'; +import { + getEnvironmentOptionsList, + getEnvironmentOption, +} from '@endo/env-options'; import anylogger from 'anylogger'; // Turn on debugging output with DEBUG=agoric @@ -9,8 +12,10 @@ const isVatLogNameColon = nameColon => ['SwingSet:ls:', 'SwingSet:vat:'].some(sel => nameColon.startsWith(sel)); // Turn on debugging output with DEBUG=agoric or DEBUG=agoric:${level} - -let selectedLevel = 'info'; +let selectedLevel = + DEBUG_LIST.length || getEnvironmentOption('DEBUG', 'unset') === 'unset' + ? 'log' + : 'info'; for (const selector of DEBUG_LIST) { const parts = selector.split(':'); if (parts[0] !== 'agoric') { @@ -22,33 +27,29 @@ for (const selector of DEBUG_LIST) { selectedLevel = 'debug'; } } -const defaultLevel = anylogger.levels[selectedLevel]; +const selectedCode = anylogger.levels[selectedLevel]; +const globalCode = selectedCode === undefined ? -Infinity : selectedCode; const oldExt = anylogger.ext; anylogger.ext = (l, o) => { l = oldExt(l, o); - l.enabledFor = lvl => defaultLevel >= anylogger.levels[lvl]; + l.enabledFor = lvl => globalCode >= anylogger.levels[lvl]; const prefix = l.name.replace(/:/g, ': '); const nameColon = `${l.name}:`; const logBelongsToVat = isVatLogNameColon(nameColon); - const logMatchesSelector = DEBUG_LIST.some(selector => { - const selectorColon = `${selector}:`; - if (!logBelongsToVat) { - return true; - } - - // If this is a vat log, then it is enabled if it matches the selector. - return nameColon.startsWith(selectorColon); - }); + // If this is a vat log, then it is enabled by a selector in DEBUG_LIST. + const logMatchesSelector = + !logBelongsToVat || + DEBUG_LIST.some(selector => { + const selectorColon = `${selector}:`; + return nameColon.startsWith(selectorColon); + }); for (const [level, code] of Object.entries(anylogger.levels)) { - if (!logMatchesSelector || code > defaultLevel) { - // Disable printing. - l[level] = () => {}; - } else { + if (logMatchesSelector && globalCode >= code) { // Enable the printing with a prefix. const doLog = l[level]; if (doLog) { @@ -60,6 +61,9 @@ anylogger.ext = (l, o) => { } else { l[level] = () => {}; } + } else { + // Disable printing. + l[level] = () => {}; } } return l;