Image

stuff I use for debugging

Hi,

This is my first post here; hope not last.
Here are three funny functions; the first one dumps the output to a filed if it exists (please define forId() yourself; on my cellphone document.getElementById does not even exist).

show() converts a string of unknown nature into a readable sequence of bytes;
describe() describes the structure of an object. It was made almost obsolete by firebug, of course.

var patience_ = 100;

function debug(msg, opt_data) {
  try {
    if (patience_ --> 0 && forId("debug")) {
        forId("debug").value += (new Date()).getTime() + "] " + msg + " " +
                                show(opt_data) + "\n";
    }
  } catch(e) {
    alert("debug: " + e);
  }
}

function show(s) {
  var buf = [];
  try {
    if (s) {
      for (var i = 0; i < s.length; i++) {
        var c= s.charCodeAt(i);
        var hex = c.toString(16);
        while (hex.length < 4) hex = '0' + hex;
        buf.push((c < 128 && c > 31) ? s.charAt(i) : ("\\u" + hex));
      }
    }
  } catch(e) {
    buf.push("\n");
    buf.push(e);
  }
  return buf.join("");
}

function describe(item) {
  var s = [''];
  try {
    for (var x in item) {
      if (!item[x]) {
        s.push(x);
      } else {
        var value = item[x].toString();
        s.push(x);
        if (typeof item[x] == 'function') {
          s.push('()');
        } else {
          s.push(':');
          s.push(value.length < 100 ? value : (value.substring(0,100) + "..."));
        }
      }
      s.push(', ');
    }
  } catch(e) {
    s.push("\nError:");
    s.push(e);
  }
  return s.join('');
}