Mercurial > hg > digilib-old
changeset 409:84c20b64e1de
added my rudimentary debug library
author | hertzhaft |
---|---|
date | Tue, 13 Dec 2005 13:31:12 +0100 |
parents | bd95a59ba0f9 |
children | 395db075906d |
files | client/digitallibrary/greyskin/debug.js |
diffstat | 1 files changed, 88 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/client/digitallibrary/greyskin/debug.js Tue Dec 13 13:31:12 2005 +0100 @@ -0,0 +1,88 @@ +var Debug; +var _now = {}; + +function newElement(tagname, content) { + // creates a new element, adding node or text content if given + var elem = document.createElement(tagname); + if (content) elem.appendChild(content.nodeType + ? content // content is a node + : document.createTextNode(content) // content is a string + ); + return elem; + } + +function appendNewElement(node, tagname, content) { + // appends a new element to "node", adding content if given + if (!node.appendChild) { + alert("Could not append: " + typeof(node)); + return null; + } + return node.appendChild(newElement(tagname, content)); + } + +function getDebug() { + if (Debug == null) { + Debug = document.getElementById('debug'); + if (Debug == null) { + if (!document.body) return null; + // its still too early! + Debug = appendNewElement(document.body, 'div'); + Debug.setAttribute('id', 'debug'); + Debug.className = 'debug'; + Debug.innerhtml = '<p class="debug">Debug</p>'; + }; + }; + return Debug; + } + +function debug() { + var msg = ""; + for (var i = 0; i<arguments.length; i++) + msg += arguments[i] + " "; + var D = getDebug(); + if (!D) { + alert("Debug div not present!\n" + msg); + return null; + } + return appendNewElement(D, "p", msg); + } + +function debugObject(obj) { + var D = getDebug(); + var A = new Array(); + for (var i in obj) A[i] = typeof(obj[i]); + var T = appendNewElement(D, "table"); + for (var item in A) { + var TR = appendNewElement(T, "tr"); + appendNewElement(TR, "td", element("b", item)); + appendNewElement(TR, "td", A[item]); + if (A[item] == "function") + appendNewElement(TR, "td", A[item].toSource()); + }; + } + +function strObject(obj) { + var res = ""; + var A = new Array(); + for (var i in obj) A[i] = typeof(obj[i]); + for (var item in A) { + typ = A[item]; + res += item + " (" + typ + "): "; + if (typ != "function") res += obj[item]; + res += "\n"; + } + return res; + } + +function alertObject(obj) { + return alert(strObject(obj)); + } + +function startTime(s) { + _now[s] = new Date(); + } + +function elapsedTime(s) { + var diff = new Date(new Date - _now[s]); + debug(s + ": " + diff.getSeconds() + "." + diff.getMilliseconds() + " sec."); + }