comparison client/digitallibrary/greyskin/debug.js @ 405:4bcbec6fed60

added my rudimentary debug library
author hertzhaft
date Tue, 13 Dec 2005 13:31:12 +0100
parents
children 893bebd68a3f
comparison
equal deleted inserted replaced
404:e9bfc85f2df5 405:4bcbec6fed60
1 var Debug;
2 var _now = {};
3
4 function newElement(tagname, content) {
5 // creates a new element, adding node or text content if given
6 var elem = document.createElement(tagname);
7 if (content) elem.appendChild(content.nodeType
8 ? content // content is a node
9 : document.createTextNode(content) // content is a string
10 );
11 return elem;
12 }
13
14 function appendNewElement(node, tagname, content) {
15 // appends a new element to "node", adding content if given
16 if (!node.appendChild) {
17 alert("Could not append: " + typeof(node));
18 return null;
19 }
20 return node.appendChild(newElement(tagname, content));
21 }
22
23 function getDebug() {
24 if (Debug == null) {
25 Debug = document.getElementById('debug');
26 if (Debug == null) {
27 if (!document.body) return null;
28 // its still too early!
29 Debug = appendNewElement(document.body, 'div');
30 Debug.setAttribute('id', 'debug');
31 Debug.className = 'debug';
32 Debug.innerhtml = '<p class="debug">Debug</p>';
33 };
34 };
35 return Debug;
36 }
37
38 function debug() {
39 var msg = "";
40 for (var i = 0; i<arguments.length; i++)
41 msg += arguments[i] + " ";
42 var D = getDebug();
43 if (!D) {
44 alert("Debug div not present!\n" + msg);
45 return null;
46 }
47 return appendNewElement(D, "p", msg);
48 }
49
50 function debugObject(obj) {
51 var D = getDebug();
52 var A = new Array();
53 for (var i in obj) A[i] = typeof(obj[i]);
54 var T = appendNewElement(D, "table");
55 for (var item in A) {
56 var TR = appendNewElement(T, "tr");
57 appendNewElement(TR, "td", element("b", item));
58 appendNewElement(TR, "td", A[item]);
59 if (A[item] == "function")
60 appendNewElement(TR, "td", A[item].toSource());
61 };
62 }
63
64 function strObject(obj) {
65 var res = "";
66 var A = new Array();
67 for (var i in obj) A[i] = typeof(obj[i]);
68 for (var item in A) {
69 typ = A[item];
70 res += item + " (" + typ + "): ";
71 if (typ != "function") res += obj[item];
72 res += "\n";
73 }
74 return res;
75 }
76
77 function alertObject(obj) {
78 return alert(strObject(obj));
79 }
80
81 function startTime(s) {
82 _now[s] = new Date();
83 }
84
85 function elapsedTime(s) {
86 var diff = new Date(new Date - _now[s]);
87 debug(s + ": " + diff.getSeconds() + "." + diff.getMilliseconds() + " sec.");
88 }