-
Notifications
You must be signed in to change notification settings - Fork 944
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
Add optional dynamic debug instances (react on enable() and disable() once created) #209
Conversation
+1 on this PR, turning on / off on the fly would be great for browser applications |
@TooTallNate any comment about this? |
+1 please merge this! |
@TooTallNate any chance of merging this feature? |
👍 Yes, please! |
index--; | ||
// Return a dynamic debug instance. | ||
else { | ||
function dynamic() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
-1 for all this copypasta
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you mean??
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the dynamic
function is nearly identical to the enabled
function
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right. We can talk about code optimization if the PR is at least considered by the author, which said nothing yet about it.
Wouldn't it also be referred to #275 ? |
@TooTallNate thoughts here? Not sure if we want to go this direction with the API |
Referenced issue #326 |
A few comments:
|
Honestly that last point is the main reason a feature like this is not in debug right now. I don't want to have to maintain a list of all the debug instances in memory, which just grows and grows as more modules are required. |
@TooTallNate agreed. Closing this for now. |
I don't see how this would cause a leak - the number of modules which can be require()'d is bounded, is it not? |
If you want to dynamicall enable/disable a debug instance you must store it somewhere. If you create tons of debug instances and never destroy them, they will in fact leak. Easy. That's why in my PR I added a debug.release() method. But, the current implementation of this module is too not handle debug instances. Instead they just exist while they are in the scope, and they are garbage collected if not like any variable. |
If you're creating debug instances on the fly and not releasing them, then sure. But is the library being used in that way? I'd bet 99.99% of cases users just This would be sooo useful for debugging in production, it seems a real shame to pass on for such an esoteric use case. If I were to wrap the library in userland, i'd only get the functionality for my application (not submodules like express). |
Sure. However sometimes I create dynamic debug instances in class constructors so their label include the |
That doesn't seem too efficient, it would be running lots of regex checks on your class instantiation? Why not just |
The reason this leaks is because internal state within the debug lib is required. If you call debug over and over in it's current state it simply returns a function. This PR requires it to keep an ever growing amount of bookkeeping. A lot of people create debug instances in class constructors as mentioned before. This would be a breaking change that would manifest itself as a server crash once ram filled up. I get that it's useful, but that doesn't necessarily mean it's doable. There are always tradeoffs |
I agree, but that's up to the app developer.
Not true. This PR does NOT change the current/default behavior. If the user wants manageable debug instances it must create them by passing var dyndebug = require('debug')('myApp', true); I understand that such an API (passing just |
require()s are synchronous, right? So if one were to enable bookkeeping initially on startup, then disable bookkeeping in a process.nextTick() (so dynamic debug instances would not be tracked), this would achieve the desired effect with no leaks. Food for thought...? |
I would argue with the API for enabling that feature, but I would definitely love to see it in next major release, enabled by default. |
Check out the 3.0 conversation issue. It's something that will be added. However, I don't think we want to resort to hackery to make it work. We should probably just save enables/disabled state on a boolean within the factory closure as that won't leak |
I need this feature, so I created a module which enables it with no API changes: It patches the debug module, so any sub-dependencies of your project (e.g. express) will get the enable/disable functionality. It'd be great to see this feature natively in debug v3! |
There are some questions that I use debug on Browser.
steps
on chrome console:
|
First of all: this PR is 100% compliant with the current design and adds zero overhead to it. This is, no changes must be done in apps and libraries already using the debug module, and the performance is the same as before.
This PR adds "dynamic debug instances" which are created by passing
true
as the second parameter to the module exported function:Those instances do properly react if
debug.enable(xxxx)
ordebug.disable()
is called after they are created.An usage example is provided in the example/ folder.
The PR also resets
exports.names
andexports.skips
whenenable()
ordisable()
is called (rationale here).Related issues: