| 1 | /* |
|---|
| 2 | Development Library - Copyright 2005 Six Apart |
|---|
| 3 | $Id$ |
|---|
| 4 | |
|---|
| 5 | Copyright (c) 2007, Six Apart, Ltd. |
|---|
| 6 | All rights reserved. |
|---|
| 7 | |
|---|
| 8 | Redistribution and use in source and binary forms, with or without |
|---|
| 9 | modification, are permitted provided that the following conditions are |
|---|
| 10 | met: |
|---|
| 11 | |
|---|
| 12 | * Redistributions of source code must retain the above copyright |
|---|
| 13 | notice, this list of conditions and the following disclaimer. |
|---|
| 14 | |
|---|
| 15 | * Redistributions in binary form must reproduce the above |
|---|
| 16 | copyright notice, this list of conditions and the following disclaimer |
|---|
| 17 | in the documentation and/or other materials provided with the |
|---|
| 18 | distribution. |
|---|
| 19 | |
|---|
| 20 | * Neither the name of "Six Apart" nor the names of its |
|---|
| 21 | contributors may be used to endorse or promote products derived from |
|---|
| 22 | this software without specific prior written permission. |
|---|
| 23 | |
|---|
| 24 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|---|
| 25 | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|---|
| 26 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|---|
| 27 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|---|
| 28 | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|---|
| 29 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|---|
| 30 | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|---|
| 31 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|---|
| 32 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|---|
| 33 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|---|
| 34 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|---|
| 35 | */ |
|---|
| 36 | |
|---|
| 37 | |
|---|
| 38 | /* benchmarking */ |
|---|
| 39 | |
|---|
| 40 | benchmark = function( callback, iterations ) { |
|---|
| 41 | var start = new Date(); |
|---|
| 42 | for( var i = 0; i < iterations; i++ ) |
|---|
| 43 | callback(); |
|---|
| 44 | var end = new Date(); |
|---|
| 45 | return (end.getSeconds() - start.getSeconds()) + |
|---|
| 46 | (end.getMilliseconds() - start.getMilliseconds()) / 1000; |
|---|
| 47 | } |
|---|
| 48 | |
|---|
| 49 | |
|---|
| 50 | inspect = function( object, allProperties, noBreaks ) { |
|---|
| 51 | var out = ""; |
|---|
| 52 | for( var property in object ) { |
|---|
| 53 | try { |
|---|
| 54 | if( !allProperties && !object.hasOwnProperty( property ) ) |
|---|
| 55 | continue; |
|---|
| 56 | out += property + ": " + object[ property ] + |
|---|
| 57 | (noBreaks ? "\n" : "<br />"); |
|---|
| 58 | } catch( e ) {} |
|---|
| 59 | } |
|---|
| 60 | return out; |
|---|
| 61 | } |
|---|
| 62 | |
|---|
| 63 | |
|---|
| 64 | /* logging, alert override */ |
|---|
| 65 | |
|---|
| 66 | Logger = new Class( Object, { |
|---|
| 67 | width: 320, |
|---|
| 68 | height: 240, |
|---|
| 69 | windowName: "log", |
|---|
| 70 | colors: { |
|---|
| 71 | INFO: "#000", |
|---|
| 72 | ERROR: "#f00", |
|---|
| 73 | DEBUG: "#005f16", |
|---|
| 74 | WARN: "#2634cf" |
|---|
| 75 | }, |
|---|
| 76 | |
|---|
| 77 | |
|---|
| 78 | log: function() { |
|---|
| 79 | this.logMessages( "INFO", arguments ); |
|---|
| 80 | }, |
|---|
| 81 | |
|---|
| 82 | |
|---|
| 83 | logError: function() { |
|---|
| 84 | this.logMessages( "ERROR", arguments ); |
|---|
| 85 | }, |
|---|
| 86 | |
|---|
| 87 | |
|---|
| 88 | logWarn: function() { |
|---|
| 89 | this.logMessages( "WARN", arguments ); |
|---|
| 90 | }, |
|---|
| 91 | |
|---|
| 92 | |
|---|
| 93 | logDebug: function() { |
|---|
| 94 | this.logMessages( "DEBUG", arguments ); |
|---|
| 95 | }, |
|---|
| 96 | |
|---|
| 97 | |
|---|
| 98 | logMessages: function( type, messages ) { |
|---|
| 99 | try { |
|---|
| 100 | var message = ""; |
|---|
| 101 | for( var i = 0; i < messages.length; i++ ) |
|---|
| 102 | message += messages[ i ]; |
|---|
| 103 | |
|---|
| 104 | // native console |
|---|
| 105 | if( window.console && window.console.log ) { |
|---|
| 106 | if( window.console[ type.toLowerCase() ] ) |
|---|
| 107 | window.console[ type.toLowerCase() ]( message ); |
|---|
| 108 | else |
|---|
| 109 | window.console.log( message ); |
|---|
| 110 | return; |
|---|
| 111 | } |
|---|
| 112 | |
|---|
| 113 | // create window |
|---|
| 114 | this.createWindow(); |
|---|
| 115 | |
|---|
| 116 | // check for no window |
|---|
| 117 | if( !this.window ) { |
|---|
| 118 | confirm( "Logger popup window blocked. Using confirm() instead.\n\n" + msg ); |
|---|
| 119 | return true; |
|---|
| 120 | } |
|---|
| 121 | |
|---|
| 122 | // create div |
|---|
| 123 | var div = this.window.document.createElement( "div" ); |
|---|
| 124 | div.style.color = this.colors[ type ] || "#000"; |
|---|
| 125 | div.style.backgroundColor = (this.count % 2) ? "#eee" : "#fff"; |
|---|
| 126 | div.style.width = "auto"; |
|---|
| 127 | div.style.padding = "3px"; |
|---|
| 128 | div.innerHTML = message; |
|---|
| 129 | |
|---|
| 130 | // append to window |
|---|
| 131 | this.window.document.body.appendChild( div ); |
|---|
| 132 | this.window.scroll( 0, this.window.document.body.scrollHeight ); |
|---|
| 133 | this.count++; |
|---|
| 134 | return true; |
|---|
| 135 | } catch( e ) {} |
|---|
| 136 | }, |
|---|
| 137 | |
|---|
| 138 | |
|---|
| 139 | createWindow: function() { |
|---|
| 140 | if( this.window && this.window.document ) |
|---|
| 141 | return; |
|---|
| 142 | |
|---|
| 143 | // create window |
|---|
| 144 | var x = "auto"; |
|---|
| 145 | var y = "auto"; |
|---|
| 146 | var attr = "resizable=yes, menubar=no, location=no, directories=no, scrollbars=yes, status=no, " + |
|---|
| 147 | "width=" + this.width + ", height=" + this.height + |
|---|
| 148 | "screenX=" + x + ", screenY=" + y + ", " + |
|---|
| 149 | "left=" + x + ", top=" + y + ", "; |
|---|
| 150 | // 2006-01-19 cmb For WebKit debugging, change "" below to "/logger". |
|---|
| 151 | this.window = window.open( "", this.windowName, attr ); |
|---|
| 152 | |
|---|
| 153 | // check for blocked popup |
|---|
| 154 | if( !this.window ) |
|---|
| 155 | return; |
|---|
| 156 | |
|---|
| 157 | // for safari |
|---|
| 158 | window.top.focus(); |
|---|
| 159 | |
|---|
| 160 | var instance; |
|---|
| 161 | try { |
|---|
| 162 | instance = this.window.__Logger; |
|---|
| 163 | } catch( e ) { |
|---|
| 164 | this.window.location.replace( "about:blank" ); |
|---|
| 165 | } |
|---|
| 166 | |
|---|
| 167 | // check for pre-existing instance |
|---|
| 168 | if( instance ) { |
|---|
| 169 | // create divider div |
|---|
| 170 | var div = this.window.document.createElement( "div" ); |
|---|
| 171 | div.style.backgroundColor = "#f00"; |
|---|
| 172 | div.style.width = "auto"; |
|---|
| 173 | div.style.height = "2px"; |
|---|
| 174 | div.style.fontSize = "0.1px"; |
|---|
| 175 | div.style.lineHeight = "0.1px"; |
|---|
| 176 | this.window.document.body.appendChild( div ); |
|---|
| 177 | } else { |
|---|
| 178 | // write body |
|---|
| 179 | this.window.document.open( "text/html", "replace" ); |
|---|
| 180 | this.window.document.write( "<html><head><title>JavaScript Log</title></head><body ondblclick=\"document.body.innerHTML='';\"></body></html>" ); |
|---|
| 181 | this.window.document.close(); |
|---|
| 182 | |
|---|
| 183 | // setup style |
|---|
| 184 | this.window.title = "JavaScript Loggers"; |
|---|
| 185 | this.window.document.body.style.margin = "0"; |
|---|
| 186 | this.window.document.body.style.padding = "0"; |
|---|
| 187 | this.window.document.body.style.fontFamily = "verdana, 'lucida grande', geneva, arial, helvetica, sans-serif"; |
|---|
| 188 | this.window.document.body.style.fontSize = "10px"; |
|---|
| 189 | } |
|---|
| 190 | |
|---|
| 191 | // get previous instance and attach new instance |
|---|
| 192 | this.prev = instance; |
|---|
| 193 | this.window.__Logger = this; |
|---|
| 194 | |
|---|
| 195 | // dereference previous previous |
|---|
| 196 | if( this.prev ) |
|---|
| 197 | this.prev.prev = null; |
|---|
| 198 | |
|---|
| 199 | // copy message count |
|---|
| 200 | this.count = this.prev ? this.prev.count : 0; |
|---|
| 201 | } |
|---|
| 202 | } ); |
|---|
| 203 | |
|---|
| 204 | |
|---|
| 205 | override( Logger, { |
|---|
| 206 | log: function() { |
|---|
| 207 | var a = Logger.logMessages( "INFO", arguments ); |
|---|
| 208 | return a; |
|---|
| 209 | }, |
|---|
| 210 | |
|---|
| 211 | |
|---|
| 212 | logError: function() { |
|---|
| 213 | return Logger.logMessages( "ERROR", arguments ); |
|---|
| 214 | }, |
|---|
| 215 | |
|---|
| 216 | |
|---|
| 217 | logWarn: function() { |
|---|
| 218 | return Logger.logMessages( "WARN", arguments ); |
|---|
| 219 | }, |
|---|
| 220 | |
|---|
| 221 | |
|---|
| 222 | logDebug: function() { |
|---|
| 223 | return Logger.logMessages( "DEBUG", arguments ); |
|---|
| 224 | }, |
|---|
| 225 | |
|---|
| 226 | |
|---|
| 227 | logMessages: function( type, messages ) { |
|---|
| 228 | Logger.initSingleton(); |
|---|
| 229 | return Logger.singleton.logMessages( type, messages ); |
|---|
| 230 | } |
|---|
| 231 | } ); |
|---|
| 232 | |
|---|
| 233 | |
|---|
| 234 | log = Logger.log; |
|---|
| 235 | log.error = Logger.logError; |
|---|
| 236 | log.warn = Logger.logWarn; |
|---|
| 237 | log.debug = Logger.logDebug; |
|---|