Log and break on:
The easiest way to install is to grab the chrome extension which will add the utility functions (described below) to your JavaScript console.
Grab du.js
and add it in script tag to your page, which will make all the
the functions (described below) available globally.
I haven't put much consideration into how will this work in node but I use it for running the tests so it should probably work.
$ npm install debug_utils
var du = require('debug_utils');
du.$duv(object, 'foo');
// Make the functions available globally.
du.global();
$duv(object, 'bar');
I tried to name the functions so that they're memorable and easy to type. Here are the rules that I followed for naming:
$du
to avoid conflicts.$du
there comes a single letter to hint at the functionality we are
debugging. e.g. $duv
, v for events.l
at the end of the function means 'log'. e.g. $duvl
, log events.r
at the end of the function name means 'remove'.As the complexity of a system grows, evented programming can make it very hard to debug. The following utilities should help you:
Attach an event handler that starts debugger when triggered.
Usesful for:
Attach an event handler that logs its arguments when fired.
Usesful for:
Remove previously set debug event handler.
Often times you find that some object is changing from under your feet. And you need to find out what is changing that object. These are utilities for you:
Debug when something tries to get at a property
of an object
.
Useful for:
Like $dug
but adds logging instead of debugger
.
Removes getters set by $dugl
and $dug
.
Debug when something tries to set a property
on an object
.
Useful for:
Like $dus
but adds logging instead of debugger
.
Removes setters set by $dus
or $dusl
.
Debug both getter and setter. It's like calling $dug
and $dus
on an object.
Like $dugs
but adds logging instead.
Removes getters and setters set by $dugs
and $dugsl
.
The JavaScript command line API provides really nice utilities for debugging functions:
monitor
|unmonitor
: logs function calls.debug
|undebug
: adds a breakpoint on the first line of the function.However, they don't work for native methods. The following should help:
Wraps an object's method with a wrapper function with a debugger
statement.
Useful for:
$dum(Event.prototype, 'preventDefault')
Like $dum
but logs arguments instead.
Removes debug or log wrappers added by $dum
or $duml
.
For APIs taking callbacks, it's really useful to be able to drop in a logger or a debugger statement. The following functions are shorter to type out:
A function with a debugger statement.
xhr.onreadystatechange = $dudebug;
Useful for:
p.s. no pun intended.
Similar to $dudebug
but logs it's arguments instead of breaking.
When called returns a function that logs it's arguments prefixed with message
.
xhr.onreadystatechange = $dulogm('xhr readystate change');
MIT
Copyright (c) 2014 Amjad Masad amjad.masad@gmail.com