source: gs3-extensions/seaweed-debug/trunk/src/Debug.js@ 25160

Last change on this file since 25160 was 25160, checked in by sjm84, 12 years ago

Initial cut at a version of seaweed for debugging purposes. Check it out live into the web/ext folder

File size: 4.1 KB
Line 
1/*
2 * file: Debug.js
3 *
4 * @BEGINLICENSE
5 * Copyright 2010 Brook Novak (email : [email protected])
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 * @ENDLICENSE
18 */
19/**
20 * @namespace The debug namespace will be removed in release builds.
21 */
22debug = {};
23
24/**
25 * Asserts a condition. If a condition fails a alert box shows and an exception is thrown.
26 *
27 * @param {Boolean} cond A condition
28 *
29 * @param {String} msg An optional message
30 *
31 * @throws {Error} if a condition fails
32 */
33debug.assert = function(cond, msg) {
34
35 try {
36 if (!cond) throw new Error("Assertion failed" + (msg ? ": " + msg : ""));
37 } catch (e) {
38
39 var fullMsg = e.message + (e.stack ? "\nstack:\n" + e.stack : "");
40
41 alert(fullMsg);
42
43 throw e;
44 }
45}
46
47debug.close = function(){
48
49 debug.stop = true;
50
51 if (typeof _debugConsole != "undefined" && _debugConsole) {
52
53 _debugConsole.parentNode.removeChild(_debugConsole);
54
55 _debugConsole = null;
56
57 }
58}
59
60debug.stop = false;
61
62/**
63 * Prints a message to a debug console.
64 *
65 * @param {String} msg A message to print.
66 */
67debug.print = function(msg){
68
69 if (debug.stop)
70 return;
71
72 if (window.console && console.log) {
73 console.log(msg);
74 } else if (window.opera && window.opera.postError){
75 window.opera.postError(msg);
76 } else {
77
78 // Setup debug console
79 if (typeof _debugConsole == "undefined" || !_debugConsole) {
80
81 _debugConsole = document.createElement("div");
82 _debugConsole.style.backgroundColor = "#444444";
83 _debugConsole.style.position = "fixed";
84 _debugConsole.style.border = "2px solid #000000";
85 _debugConsole.style.width = "320px";
86 _debugConsole.style.height = "174px";
87 _debugConsole.style.zIndex = "9999";
88 _debugConsole.style.top = "0";
89 _debugConsole.style.left = "0";
90
91 _debugConsole.innerHTML = '<div style="width:320px; color:white; font-weight:bold; font-family:arial,sans; text-align:center; font-size:14px;"><div style="float:right;border: 1px dashed black; color:black; background-color:#66D390; padding:4px; cursor:pointer;" onclick="debug.close();">close</div>~Seaweed~ Debug Console</div>';
92
93 _debugOutput = document.createElement("div");
94 _debugOutput.style.backgroundColor = "white";
95 _debugOutput.style.overflow = "scroll";
96 _debugOutput.style.width = _debugConsole.style.width;
97 _debugOutput.style.height = "140px";
98
99 _debugConsole.appendChild(_debugOutput);
100
101 }
102
103 if (document.body && _debugConsole && _debugConsole.parentNode != document.body)
104 document.body.appendChild(_debugConsole);
105
106 _debugOutput.innerHTML += msg.replace(/\n/g,"<br/>");
107 var st = _debugOutput.scrollHeight - _debugOutput.clientHeight;
108 if (st < 0) st = 0;
109 _debugOutput.scrollTop = st;
110
111 }
112}
113
114/**
115 * Prints a message to a debug console with a new line
116 *
117 * @param {String} msg A message to print.
118 */
119debug.println = function(msg){
120 debug.print(msg + "\n");
121};
122
123/**
124 * Prints out a TODO message and it's stack location
125 * @param {String} msg An optional todo message
126 */
127debug.todo = function(msg) {
128 try {
129 throw new Error();
130 } catch (e) {
131 var fullMsg = "TODO: " + (msg ? msg : "") + (e.stack ? "\nAt" + e.stack : + "");
132 debug.println(fullMsg);
133 }
134};
135
136
Note: See TracBrowser for help on using the repository browser.