-
Notifications
You must be signed in to change notification settings - Fork 30.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
util: add debugtest utility #33424
util: add debugtest utility #33424
Conversation
I feel like we need better namespacing here. Maybe split into |
Is V8 able to completely eliminate the branch when the value is |
@devsnek that would be adding an alias to the existing function and i'm not sure what we get except moving a @targos per my testing, yes. I was using: node --allow-natives-syntax --code-comments --print-code --print-code-verbose -e 'const test=require("util").debugtest("foo"); function easy_foo() {if (test) return; return [1,'2',null];};%OptimizeFunctionOnNextCall(easy_foo);easy_foo()' |
There are a couple of possible alternative approaches here, since const debug = util.debuglog('test');
// What debug log currently does...
debug('how we do it currently');
// Add an additional function off `debug` that invokes the given function
// only if the category is enabled.
debug.do(() => {
// Do something here
}) And / Or
Another modification we could make here would be similar to what we did on the C++ level const { debuglog } = require('util');
const debug = debuglog('foo');
const obj {
[debuglog.debug]() { return 'abc'; }
};
debug('something happened: %s', obj); This would obviously take a bit of work on the internals to account for the symbol but in the default case (debug off) it would not incur any additional cost. |
@jasnell per the alternatives:
|
@jasnell @bmeck it is already possible to use const debug = require('util').debuglog('test')
debug('Lazy property inspection: %o', { works: true })
// TEST 123456: Lazy property inspection: { works: true } If we want to add more powerful lazy evaluations, I'd favor to use |
The point about having to use Thinking through it more, @devsnek's comment here #33424 (comment) makes a lot of sense ... that is, replacing the const debug = util.debug('test');
debug('some output: %s', 'a');
debug.do((a) => {/* ... */}, 'a');
if (debug.enabled) {
/* ... */
} |
I'm not sure on the need for a util.debug: (section: string) => {log(...): typeof debuglog, enabled: boolean}; Is fine I can move the PR to that, I'm not sure I understand the desire for the namespace itself to be a function. |
The namespace being a function just makes the transition from the current model a bit easier in my opinion but it's not critical |
@jasnell If the goal is transition ease; would it make it easier if this was just an alias and we added |
Yes that works |
Keeping the ENV parsing lazy means |
I've pushed up changes so that
In both a getter form and a method form V8 does not optimize |
8ae28ff
to
2935f72
Compare
#32260 landed an undocumented API change that causes a callback to be fired with the logger or |
reopening, i misunderstood how that callback works. made a PR to doc it, but this PR still has utility by avoiding the call to the logger in order to set the boolean. |
rebased due to changes with util.debuglog implementation. |
Landed in 7f5da53c7972 |
@bmeck I think you've pushed the wrong commits up to master (the ones that were pushed don't appear to have metadata added): |
Looks like @himself65 force pushed them out: https://github.com/orgs/nodejs/teams/collaborators/discussions/105. I'll reopen this one as it will need to be relanded. |
@richardlau i went through git node land so i'm unclear how this occurred, checking my master branch (from which i ran I'm unclear what to do here to reland since my master matches what i expected and i don't want to repush the wrong commits |
PR-URL: nodejs#33424 Reviewed-By: James M Snell <[email protected]>
PR-URL: #33424 Reviewed-By: James M Snell <[email protected]>
Landed in bcfb176 |
PR-URL: #33424 Reviewed-By: James M Snell <[email protected]>
PR-URL: #33424 Reviewed-By: James M Snell <[email protected]>
Checklist
make -j4 test
(UNIX), orvcbuild test
(Windows) passesCurrently we have a
debuglog
utility to allow logging when debugging. This PR introduces a corollarydebugtest
utility to see if something is being debugged rather than logging. This would allow people usingdebuglog
style conditional debugging to avoid doing unnecessary work such as creating strings etc. For example:Would always stringify the object, even if
foo
is not being debugged. Additionally, it would allow branching to add additional debugging logic that may be undesirable when not debugging. For example: