Skip to content

Commit ef10d7f

Browse files
michaelfigerights
authored andcommitted
fixup! fix: DEBUG harmony
1 parent 28f6df6 commit ef10d7f

File tree

3 files changed

+41
-29
lines changed

3 files changed

+41
-29
lines changed

docs/env.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,9 @@ for a given context, in order of increasing severity:
5858
4. `console.warn`
5959
5. `console.error`
6060

61-
If not set, then default (`console.info` and above) logging is enabled.
61+
If `$DEBUG` is unset or non-empty, then default (`console.log` and above) logging is enabled. (`console.debug` logging is disabled.)
62+
63+
If `$DEBUG` is set to an empty string, then quiet (`console.info` and above) logging is enabled.
6264
(`console.log` and `console.debug` logging is disabled.)
6365

6466
Otherwise, set to a comma-separated list of strings.

packages/agoric-cli/src/anylogger-agoric.js

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
1-
import { getEnvironmentOptionsList } from '@endo/env-options';
1+
import {
2+
getEnvironmentOption,
3+
getEnvironmentOptionsList,
4+
} from '@endo/env-options';
25
import anylogger from 'anylogger';
36
import chalk from 'chalk';
47

5-
// Turn on debugging output with DEBUG=agoric or DEBUG=agoric:${level}
6-
78
const DEBUG_LIST = getEnvironmentOptionsList('DEBUG');
89

9-
let selectedLevel = 'info';
10+
// Turn on debugging output with DEBUG=agoric or DEBUG=agoric:${level}
11+
let selectedLevel =
12+
DEBUG_LIST.length || getEnvironmentOption('DEBUG', 'unset') === 'unset'
13+
? 'log'
14+
: 'info';
1015
for (const level of DEBUG_LIST) {
1116
const parts = level.split(':');
1217
if (parts[0] !== 'agoric') {
@@ -18,22 +23,23 @@ for (const level of DEBUG_LIST) {
1823
selectedLevel = 'debug';
1924
}
2025
}
21-
const defaultLevel = anylogger.levels[selectedLevel];
26+
const selectedCode = anylogger.levels[selectedLevel];
27+
const globalCode = selectedCode === undefined ? -Infinity : selectedCode;
2228

2329
const oldExt = anylogger.ext;
2430
anylogger.ext = (l, o) => {
2531
l = oldExt(l, o);
26-
l.enabledFor = lvl => defaultLevel >= anylogger.levels[lvl];
32+
l.enabledFor = lvl => globalCode >= anylogger.levels[lvl];
2733

2834
const prefix = l.name.replace(/:/g, ': ');
2935
for (const [level, code] of Object.entries(anylogger.levels)) {
30-
if (code > defaultLevel) {
31-
// Disable printing.
32-
l[level] = () => {};
33-
} else {
36+
if (globalCode >= code) {
3437
// Enable the printing with a prefix.
3538
const doLog = l[level] || (() => {});
3639
l[level] = (...args) => doLog(chalk.bold.blue(`${prefix}:`), ...args);
40+
} else {
41+
// Disable printing.
42+
l[level] = () => {};
3743
}
3844
}
3945
return l;

packages/cosmic-swingset/src/anylogger-agoric.js

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
import { getEnvironmentOptionsList } from '@endo/env-options';
1+
import {
2+
getEnvironmentOptionsList,
3+
getEnvironmentOption,
4+
} from '@endo/env-options';
25
import anylogger from 'anylogger';
36

47
// Turn on debugging output with DEBUG=agoric
@@ -9,8 +12,10 @@ const isVatLogNameColon = nameColon =>
912
['SwingSet:ls:', 'SwingSet:vat:'].some(sel => nameColon.startsWith(sel));
1013

1114
// Turn on debugging output with DEBUG=agoric or DEBUG=agoric:${level}
12-
13-
let selectedLevel = 'info';
15+
let selectedLevel =
16+
DEBUG_LIST.length || getEnvironmentOption('DEBUG', 'unset') === 'unset'
17+
? 'log'
18+
: 'info';
1419
for (const selector of DEBUG_LIST) {
1520
const parts = selector.split(':');
1621
if (parts[0] !== 'agoric') {
@@ -22,33 +27,29 @@ for (const selector of DEBUG_LIST) {
2227
selectedLevel = 'debug';
2328
}
2429
}
25-
const defaultLevel = anylogger.levels[selectedLevel];
30+
const selectedCode = anylogger.levels[selectedLevel];
31+
const globalCode = selectedCode === undefined ? -Infinity : selectedCode;
2632

2733
const oldExt = anylogger.ext;
2834
anylogger.ext = (l, o) => {
2935
l = oldExt(l, o);
30-
l.enabledFor = lvl => defaultLevel >= anylogger.levels[lvl];
36+
l.enabledFor = lvl => globalCode >= anylogger.levels[lvl];
3137

3238
const prefix = l.name.replace(/:/g, ': ');
3339

3440
const nameColon = `${l.name}:`;
3541
const logBelongsToVat = isVatLogNameColon(nameColon);
3642

37-
const logMatchesSelector = DEBUG_LIST.some(selector => {
38-
const selectorColon = `${selector}:`;
39-
if (!logBelongsToVat) {
40-
return true;
41-
}
42-
43-
// If this is a vat log, then it is enabled if it matches the selector.
44-
return nameColon.startsWith(selectorColon);
45-
});
43+
// If this is a vat log, then it is enabled by a selector in DEBUG_LIST.
44+
const logMatchesSelector =
45+
!logBelongsToVat ||
46+
DEBUG_LIST.some(selector => {
47+
const selectorColon = `${selector}:`;
48+
return nameColon.startsWith(selectorColon);
49+
});
4650

4751
for (const [level, code] of Object.entries(anylogger.levels)) {
48-
if (!logMatchesSelector || code > defaultLevel) {
49-
// Disable printing.
50-
l[level] = () => {};
51-
} else {
52+
if (logMatchesSelector && globalCode >= code) {
5253
// Enable the printing with a prefix.
5354
const doLog = l[level];
5455
if (doLog) {
@@ -60,6 +61,9 @@ anylogger.ext = (l, o) => {
6061
} else {
6162
l[level] = () => {};
6263
}
64+
} else {
65+
// Disable printing.
66+
l[level] = () => {};
6367
}
6468
}
6569
return l;

0 commit comments

Comments
 (0)