CoolFace
Apppublic

Umama-at-Bluchip/Quick-UI

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
README.md482 linesDownload Raw Back to debug
1# debug2[![Build Status](https://travis-ci.org/debug-js/debug.svg?branch=master)](https://travis-ci.org/debug-js/debug)  [![Coverage Status](https://coveralls.io/repos/github/debug-js/debug/badge.svg?branch=master)](https://coveralls.io/github/debug-js/debug?branch=master)  [![Slack](https://visionmedia-community-slackin.now.sh/badge.svg)](https://visionmedia-community-slackin.now.sh/) [![OpenCollective](https://opencollective.com/debug/backers/badge.svg)](#backers)3[![OpenCollective](https://opencollective.com/debug/sponsors/badge.svg)](#sponsors)4 5<img width="647" src="https://user-images.githubusercontent.com/71256/29091486-fa38524c-7c37-11e7-895f-e7ec8e1039b6.png">6 7A tiny JavaScript debugging utility modelled after Node.js core's debugging8technique. Works in Node.js and web browsers.9 10## Installation11 12```bash13$ npm install debug14```15 16## Usage17 18`debug` exposes a function; simply pass this function the name of your module, and it will return a decorated version of `console.error` for you to pass debug statements to. This will allow you to toggle the debug output for different parts of your module as well as the module as a whole.19 20Example [_app.js_](./examples/node/app.js):21 22```js23var debug = require('debug')('http')24  , http = require('http')25  , name = 'My App';26 27// fake app28 29debug('booting %o', name);30 31http.createServer(function(req, res){32  debug(req.method + ' ' + req.url);33  res.end('hello\n');34}).listen(3000, function(){35  debug('listening');36});37 38// fake worker of some kind39 40require('./worker');41```42 43Example [_worker.js_](./examples/node/worker.js):44 45```js46var a = require('debug')('worker:a')47  , b = require('debug')('worker:b');48 49function work() {50  a('doing lots of uninteresting work');51  setTimeout(work, Math.random() * 1000);52}53 54work();55 56function workb() {57  b('doing some work');58  setTimeout(workb, Math.random() * 2000);59}60 61workb();62```63 64The `DEBUG` environment variable is then used to enable these based on space or65comma-delimited names.66 67Here are some examples:68 69<img width="647" alt="screen shot 2017-08-08 at 12 53 04 pm" src="https://user-images.githubusercontent.com/71256/29091703-a6302cdc-7c38-11e7-8304-7c0b3bc600cd.png">70<img width="647" alt="screen shot 2017-08-08 at 12 53 38 pm" src="https://user-images.githubusercontent.com/71256/29091700-a62a6888-7c38-11e7-800b-db911291ca2b.png">71<img width="647" alt="screen shot 2017-08-08 at 12 53 25 pm" src="https://user-images.githubusercontent.com/71256/29091701-a62ea114-7c38-11e7-826a-2692bedca740.png">72 73#### Windows command prompt notes74 75##### CMD76 77On Windows the environment variable is set using the `set` command.78 79```cmd80set DEBUG=*,-not_this81```82 83Example:84 85```cmd86set DEBUG=* & node app.js87```88 89##### PowerShell (VS Code default)90 91PowerShell uses different syntax to set environment variables.92 93```cmd94$env:DEBUG = "*,-not_this"95```96 97Example:98 99```cmd100$env:DEBUG='app';node app.js101```102 103Then, run the program to be debugged as usual.104 105npm script example:106```js107  "windowsDebug": "@powershell -Command $env:DEBUG='*';node app.js",108```109 110## Namespace Colors111 112Every debug instance has a color generated for it based on its namespace name.113This helps when visually parsing the debug output to identify which debug instance114a debug line belongs to.115 116#### Node.js117 118In Node.js, colors are enabled when stderr is a TTY. You also _should_ install119the [`supports-color`](https://npmjs.org/supports-color) module alongside debug,120otherwise debug will only use a small handful of basic colors.121 122<img width="521" src="https://user-images.githubusercontent.com/71256/29092181-47f6a9e6-7c3a-11e7-9a14-1928d8a711cd.png">123 124#### Web Browser125 126Colors are also enabled on "Web Inspectors" that understand the `%c` formatting127option. These are WebKit web inspectors, Firefox ([since version12831](https://hacks.mozilla.org/2014/05/editable-box-model-multiple-selection-sublime-text-keys-much-more-firefox-developer-tools-episode-31/))129and the Firebug plugin for Firefox (any version).130 131<img width="524" src="https://user-images.githubusercontent.com/71256/29092033-b65f9f2e-7c39-11e7-8e32-f6f0d8e865c1.png">132 133 134## Millisecond diff135 136When actively developing an application it can be useful to see when the time spent between one `debug()` call and the next. Suppose for example you invoke `debug()` before requesting a resource, and after as well, the "+NNNms" will show you how much time was spent between calls.137 138<img width="647" src="https://user-images.githubusercontent.com/71256/29091486-fa38524c-7c37-11e7-895f-e7ec8e1039b6.png">139 140When stdout is not a TTY, `Date#toISOString()` is used, making it more useful for logging the debug information as shown below:141 142<img width="647" src="https://user-images.githubusercontent.com/71256/29091956-6bd78372-7c39-11e7-8c55-c948396d6edd.png">143 144 145## Conventions146 147If you're using this in one or more of your libraries, you _should_ use the name of your library so that developers may toggle debugging as desired without guessing names. If you have more than one debuggers you _should_ prefix them with your library name and use ":" to separate features. For example "bodyParser" from Connect would then be "connect:bodyParser".  If you append a "*" to the end of your name, it will always be enabled regardless of the setting of the DEBUG environment variable.  You can then use it for normal output as well as debug output.148 149## Wildcards150 151The `*` character may be used as a wildcard. Suppose for example your library has152debuggers named "connect:bodyParser", "connect:compress", "connect:session",153instead of listing all three with154`DEBUG=connect:bodyParser,connect:compress,connect:session`, you may simply do155`DEBUG=connect:*`, or to run everything using this module simply use `DEBUG=*`.156 157You can also exclude specific debuggers by prefixing them with a "-" character.158For example, `DEBUG=*,-connect:*` would include all debuggers except those159starting with "connect:".160 161## Environment Variables162 163When running through Node.js, you can set a few environment variables that will164change the behavior of the debug logging:165 166| Name      | Purpose                                         |167|-----------|-------------------------------------------------|168| `DEBUG`   | Enables/disables specific debugging namespaces. |169| `DEBUG_HIDE_DATE` | Hide date from debug output (non-TTY).  |170| `DEBUG_COLORS`| Whether or not to use colors in the debug output. |171| `DEBUG_DEPTH` | Object inspection depth.                    |172| `DEBUG_SHOW_HIDDEN` | Shows hidden properties on inspected objects. |173 174 175__Note:__ The environment variables beginning with `DEBUG_` end up being176converted into an Options object that gets used with `%o`/`%O` formatters.177See the Node.js documentation for178[`util.inspect()`](https://nodejs.org/api/util.html#util_util_inspect_object_options)179for the complete list.180 181## Formatters182 183Debug uses [printf-style](https://wikipedia.org/wiki/Printf_format_string) formatting.184Below are the officially supported formatters:185 186| Formatter | Representation |187|-----------|----------------|188| `%O`      | Pretty-print an Object on multiple lines. |189| `%o`      | Pretty-print an Object all on a single line. |190| `%s`      | String. |191| `%d`      | Number (both integer and float). |192| `%j`      | JSON. Replaced with the string '[Circular]' if the argument contains circular references. |193| `%%`      | Single percent sign ('%'). This does not consume an argument. |194 195 196### Custom formatters197 198You can add custom formatters by extending the `debug.formatters` object.199For example, if you wanted to add support for rendering a Buffer as hex with200`%h`, you could do something like:201 202```js203const createDebug = require('debug')204createDebug.formatters.h = (v) => {205  return v.toString('hex')206}207 208// …elsewhere209const debug = createDebug('foo')210debug('this is hex: %h', new Buffer('hello world'))211//   foo this is hex: 68656c6c6f20776f726c6421 +0ms212```213 214 215## Browser Support216 217You can build a browser-ready script using [browserify](https://github.com/substack/node-browserify),218or just use the [browserify-as-a-service](https://wzrd.in/) [build](https://wzrd.in/standalone/debug@latest),219if you don't want to build it yourself.220 221Debug's enable state is currently persisted by `localStorage`.222Consider the situation shown below where you have `worker:a` and `worker:b`,223and wish to debug both. You can enable this using `localStorage.debug`:224 225```js226localStorage.debug = 'worker:*'227```228 229And then refresh the page.230 231```js232a = debug('worker:a');233b = debug('worker:b');234 235setInterval(function(){236  a('doing some work');237}, 1000);238 239setInterval(function(){240  b('doing some work');241}, 1200);242```243 244In Chromium-based web browsers (e.g. Brave, Chrome, and Electron), the JavaScript console will—by default—only show messages logged by `debug` if the "Verbose" log level is _enabled_.245 246<img width="647" src="https://user-images.githubusercontent.com/7143133/152083257-29034707-c42c-4959-8add-3cee850e6fcf.png">247 248## Output streams249 250  By default `debug` will log to stderr, however this can be configured per-namespace by overriding the `log` method:251 252Example [_stdout.js_](./examples/node/stdout.js):253 254```js255var debug = require('debug');256var error = debug('app:error');257 258// by default stderr is used259error('goes to stderr!');260 261var log = debug('app:log');262// set this namespace to log via console.log263log.log = console.log.bind(console); // don't forget to bind to console!264log('goes to stdout');265error('still goes to stderr!');266 267// set all output to go via console.info268// overrides all per-namespace log settings269debug.log = console.info.bind(console);270error('now goes to stdout via console.info');271log('still goes to stdout, but via console.info now');272```273 274## Extend275You can simply extend debugger 276```js277const log = require('debug')('auth');278 279//creates new debug instance with extended namespace280const logSign = log.extend('sign');281const logLogin = log.extend('login');282 283log('hello'); // auth hello284logSign('hello'); //auth:sign hello285logLogin('hello'); //auth:login hello286```287 288## Set dynamically289 290You can also enable debug dynamically by calling the `enable()` method :291 292```js293let debug = require('debug');294 295console.log(1, debug.enabled('test'));296 297debug.enable('test');298console.log(2, debug.enabled('test'));299 300debug.disable();301console.log(3, debug.enabled('test'));302 303```304 305print :   306```3071 false3082 true3093 false310```311 312Usage :  313`enable(namespaces)`  314`namespaces` can include modes separated by a colon and wildcards.315   316Note that calling `enable()` completely overrides previously set DEBUG variable : 317 318```319$ DEBUG=foo node -e 'var dbg = require("debug"); dbg.enable("bar"); console.log(dbg.enabled("foo"))'320=> false321```322 323`disable()`324 325Will disable all namespaces. The functions returns the namespaces currently326enabled (and skipped). This can be useful if you want to disable debugging327temporarily without knowing what was enabled to begin with.328 329For example:330 331```js332let debug = require('debug');333debug.enable('foo:*,-foo:bar');334let namespaces = debug.disable();335debug.enable(namespaces);336```337 338Note: There is no guarantee that the string will be identical to the initial339enable string, but semantically they will be identical.340 341## Checking whether a debug target is enabled342 343After you've created a debug instance, you can determine whether or not it is344enabled by checking the `enabled` property:345 346```javascript347const debug = require('debug')('http');348 349if (debug.enabled) {350  // do stuff...351}352```353 354You can also manually toggle this property to force the debug instance to be355enabled or disabled.356 357## Usage in child processes358 359Due to the way `debug` detects if the output is a TTY or not, colors are not shown in child processes when `stderr` is piped. A solution is to pass the `DEBUG_COLORS=1` environment variable to the child process.  360For example:361 362```javascript363worker = fork(WORKER_WRAP_PATH, [workerPath], {364  stdio: [365    /* stdin: */ 0,366    /* stdout: */ 'pipe',367    /* stderr: */ 'pipe',368    'ipc',369  ],370  env: Object.assign({}, process.env, {371    DEBUG_COLORS: 1 // without this settings, colors won't be shown372  }),373});374 375worker.stderr.pipe(process.stderr, { end: false });376```377 378 379## Authors380 381 - TJ Holowaychuk382 - Nathan Rajlich383 - Andrew Rhyne384 - Josh Junon385 386## Backers387 388Support us with a monthly donation and help us continue our activities. [[Become a backer](https://opencollective.com/debug#backer)]389 390<a href="https://opencollective.com/debug/backer/0/website" target="_blank"><img src="https://opencollective.com/debug/backer/0/avatar.svg"></a>391<a href="https://opencollective.com/debug/backer/1/website" target="_blank"><img src="https://opencollective.com/debug/backer/1/avatar.svg"></a>392<a href="https://opencollective.com/debug/backer/2/website" target="_blank"><img src="https://opencollective.com/debug/backer/2/avatar.svg"></a>393<a href="https://opencollective.com/debug/backer/3/website" target="_blank"><img src="https://opencollective.com/debug/backer/3/avatar.svg"></a>394<a href="https://opencollective.com/debug/backer/4/website" target="_blank"><img src="https://opencollective.com/debug/backer/4/avatar.svg"></a>395<a href="https://opencollective.com/debug/backer/5/website" target="_blank"><img src="https://opencollective.com/debug/backer/5/avatar.svg"></a>396<a href="https://opencollective.com/debug/backer/6/website" target="_blank"><img src="https://opencollective.com/debug/backer/6/avatar.svg"></a>397<a href="https://opencollective.com/debug/backer/7/website" target="_blank"><img src="https://opencollective.com/debug/backer/7/avatar.svg"></a>398<a href="https://opencollective.com/debug/backer/8/website" target="_blank"><img src="https://opencollective.com/debug/backer/8/avatar.svg"></a>399<a href="https://opencollective.com/debug/backer/9/website" target="_blank"><img src="https://opencollective.com/debug/backer/9/avatar.svg"></a>400<a href="https://opencollective.com/debug/backer/10/website" target="_blank"><img src="https://opencollective.com/debug/backer/10/avatar.svg"></a>401<a href="https://opencollective.com/debug/backer/11/website" target="_blank"><img src="https://opencollective.com/debug/backer/11/avatar.svg"></a>402<a href="https://opencollective.com/debug/backer/12/website" target="_blank"><img src="https://opencollective.com/debug/backer/12/avatar.svg"></a>403<a href="https://opencollective.com/debug/backer/13/website" target="_blank"><img src="https://opencollective.com/debug/backer/13/avatar.svg"></a>404<a href="https://opencollective.com/debug/backer/14/website" target="_blank"><img src="https://opencollective.com/debug/backer/14/avatar.svg"></a>405<a href="https://opencollective.com/debug/backer/15/website" target="_blank"><img src="https://opencollective.com/debug/backer/15/avatar.svg"></a>406<a href="https://opencollective.com/debug/backer/16/website" target="_blank"><img src="https://opencollective.com/debug/backer/16/avatar.svg"></a>407<a href="https://opencollective.com/debug/backer/17/website" target="_blank"><img src="https://opencollective.com/debug/backer/17/avatar.svg"></a>408<a href="https://opencollective.com/debug/backer/18/website" target="_blank"><img src="https://opencollective.com/debug/backer/18/avatar.svg"></a>409<a href="https://opencollective.com/debug/backer/19/website" target="_blank"><img src="https://opencollective.com/debug/backer/19/avatar.svg"></a>410<a href="https://opencollective.com/debug/backer/20/website" target="_blank"><img src="https://opencollective.com/debug/backer/20/avatar.svg"></a>411<a href="https://opencollective.com/debug/backer/21/website" target="_blank"><img src="https://opencollective.com/debug/backer/21/avatar.svg"></a>412<a href="https://opencollective.com/debug/backer/22/website" target="_blank"><img src="https://opencollective.com/debug/backer/22/avatar.svg"></a>413<a href="https://opencollective.com/debug/backer/23/website" target="_blank"><img src="https://opencollective.com/debug/backer/23/avatar.svg"></a>414<a href="https://opencollective.com/debug/backer/24/website" target="_blank"><img src="https://opencollective.com/debug/backer/24/avatar.svg"></a>415<a href="https://opencollective.com/debug/backer/25/website" target="_blank"><img src="https://opencollective.com/debug/backer/25/avatar.svg"></a>416<a href="https://opencollective.com/debug/backer/26/website" target="_blank"><img src="https://opencollective.com/debug/backer/26/avatar.svg"></a>417<a href="https://opencollective.com/debug/backer/27/website" target="_blank"><img src="https://opencollective.com/debug/backer/27/avatar.svg"></a>418<a href="https://opencollective.com/debug/backer/28/website" target="_blank"><img src="https://opencollective.com/debug/backer/28/avatar.svg"></a>419<a href="https://opencollective.com/debug/backer/29/website" target="_blank"><img src="https://opencollective.com/debug/backer/29/avatar.svg"></a>420 421 422## Sponsors423 424Become a sponsor and get your logo on our README on Github with a link to your site. [[Become a sponsor](https://opencollective.com/debug#sponsor)]425 426<a href="https://opencollective.com/debug/sponsor/0/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/0/avatar.svg"></a>427<a href="https://opencollective.com/debug/sponsor/1/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/1/avatar.svg"></a>428<a href="https://opencollective.com/debug/sponsor/2/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/2/avatar.svg"></a>429<a href="https://opencollective.com/debug/sponsor/3/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/3/avatar.svg"></a>430<a href="https://opencollective.com/debug/sponsor/4/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/4/avatar.svg"></a>431<a href="https://opencollective.com/debug/sponsor/5/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/5/avatar.svg"></a>432<a href="https://opencollective.com/debug/sponsor/6/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/6/avatar.svg"></a>433<a href="https://opencollective.com/debug/sponsor/7/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/7/avatar.svg"></a>434<a href="https://opencollective.com/debug/sponsor/8/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/8/avatar.svg"></a>435<a href="https://opencollective.com/debug/sponsor/9/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/9/avatar.svg"></a>436<a href="https://opencollective.com/debug/sponsor/10/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/10/avatar.svg"></a>437<a href="https://opencollective.com/debug/sponsor/11/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/11/avatar.svg"></a>438<a href="https://opencollective.com/debug/sponsor/12/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/12/avatar.svg"></a>439<a href="https://opencollective.com/debug/sponsor/13/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/13/avatar.svg"></a>440<a href="https://opencollective.com/debug/sponsor/14/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/14/avatar.svg"></a>441<a href="https://opencollective.com/debug/sponsor/15/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/15/avatar.svg"></a>442<a href="https://opencollective.com/debug/sponsor/16/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/16/avatar.svg"></a>443<a href="https://opencollective.com/debug/sponsor/17/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/17/avatar.svg"></a>444<a href="https://opencollective.com/debug/sponsor/18/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/18/avatar.svg"></a>445<a href="https://opencollective.com/debug/sponsor/19/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/19/avatar.svg"></a>446<a href="https://opencollective.com/debug/sponsor/20/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/20/avatar.svg"></a>447<a href="https://opencollective.com/debug/sponsor/21/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/21/avatar.svg"></a>448<a href="https://opencollective.com/debug/sponsor/22/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/22/avatar.svg"></a>449<a href="https://opencollective.com/debug/sponsor/23/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/23/avatar.svg"></a>450<a href="https://opencollective.com/debug/sponsor/24/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/24/avatar.svg"></a>451<a href="https://opencollective.com/debug/sponsor/25/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/25/avatar.svg"></a>452<a href="https://opencollective.com/debug/sponsor/26/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/26/avatar.svg"></a>453<a href="https://opencollective.com/debug/sponsor/27/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/27/avatar.svg"></a>454<a href="https://opencollective.com/debug/sponsor/28/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/28/avatar.svg"></a>455<a href="https://opencollective.com/debug/sponsor/29/website" target="_blank"><img src="https://opencollective.com/debug/sponsor/29/avatar.svg"></a>456 457## License458 459(The MIT License)460 461Copyright (c) 2014-2017 TJ Holowaychuk &lt;tj@vision-media.ca&gt;462Copyright (c) 2018-2021 Josh Junon463 464Permission is hereby granted, free of charge, to any person obtaining465a copy of this software and associated documentation files (the466'Software'), to deal in the Software without restriction, including467without limitation the rights to use, copy, modify, merge, publish,468distribute, sublicense, and/or sell copies of the Software, and to469permit persons to whom the Software is furnished to do so, subject to470the following conditions:471 472The above copyright notice and this permission notice shall be473included in all copies or substantial portions of the Software.474 475THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,476EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF477MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.478IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY479CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,480TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE481SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.482