Skip to content

Commit

Permalink
fixup! DEBUG harmony
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelfig authored and erights committed Feb 5, 2024
1 parent 34e105f commit 4497cd9
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 29 deletions.
4 changes: 3 additions & 1 deletion docs/env.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
26 changes: 16 additions & 10 deletions packages/agoric-cli/src/anylogger-agoric.js
Original file line number Diff line number Diff line change
@@ -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') {
Expand All @@ -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;
Expand Down
40 changes: 22 additions & 18 deletions packages/cosmic-swingset/src/anylogger-agoric.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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') {
Expand All @@ -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) {
Expand All @@ -60,6 +61,9 @@ anylogger.ext = (l, o) => {
} else {
l[level] = () => {};
}
} else {
// Disable printing.
l[level] = () => {};
}
}
return l;
Expand Down

0 comments on commit 4497cd9

Please sign in to comment.