Created
March 10, 2011 09:52
-
-
Save micmath/863840 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * @name Logging Utility | |
| * @description Adds logging functions to window namespace. | |
| * Adds console logging to browsers that don't have a console (E.G.: IE 6,7 & 8, Safari 2) | |
| * @author Rob Taylor <robert.taylor1@bbc.co.uk> | |
| */ | |
| define('iplayer/utils/logging', | |
| ['iplayer/models/cookie'], | |
| function (cookie) { | |
| var logging = { | |
| _consoleWindow: null, // Placeholder for console popup, if it's created. | |
| _sendMessage: function (method, args) { | |
| // If a method isn't selected, set it to 'log'. | |
| method = method || 'log'; | |
| var forceDebug = cookie.get('BBCiPlayer', 'debug'), | |
| split_args = Array.prototype.join.call(args, ''), | |
| dh = 0, ch = 0, p = '', message = '', | |
| enable_console_window_input = true; | |
| if (iplayer.config.debug || forceDebug) { | |
| if (typeof console !== 'undefined' && typeof console[method] !== 'undefined' && typeof window.consoleEnabled !== 'undefined' && window.consoleEnabled) { | |
| if (typeof console[method] === 'function') { | |
| console[method].apply(console, args); | |
| } else { | |
| // since console.log is an object & not a function in IE, | |
| // you can't call it with apply. Casting the arguments object | |
| // into an Array gives you at least the serialised output | |
| console[method](split_args); | |
| } | |
| } else if (iplayer.config.logging.enablePopupWindow || forceDebug) { | |
| if (!logging._consoleWindow) { | |
| try { | |
| logging._consoleWindow = window.open('', 'loggingConsoleWindow', 'width=750,height=250,menubar=0,toolbar=0,location=no,status=0,scrollbars=1,resizable=1'); | |
| logging._consoleWindow.document.write( | |
| '<html>' + | |
| '<head>' + | |
| '<title>Console<\/title>' + | |
| '<style>' + | |
| 'body{background-color:#FFF;padding:0;margin:0;font:11px monaco;} ' + | |
| (enable_console_window_input ? 'form{display:inline;} ' : 'form{display:none;} ') + | |
| 'form input{width:100%;border:0 none;font:11px monaco;height:15px;line-height:11px;padding:4px;margin:0px;} ' + | |
| '.message{background-color:#FFF;padding:4px;margin:0px;border-bottom:1px solid #ccc;} .message span{height:15px;line-height:11px;padding:0 2px;margin:1px;border:1px solid #FFF;} ' + | |
| '.warn{background-color:#FFFFC8;} .info{background-color:#EBF5FF;} .run{color:#00F;} .error{background-color:#FFEBEB;color:#F00;}' + | |
| '.typeof_function span{color:#006400;} .typeof_string span{color:#F00;} .typeof_undefined span{background-color:#888;color:#FFF;border-color:#666;}' + | |
| '<\/style>' + | |
| '<\/head>' + | |
| '<body>' + | |
| '<code id="messages"><\/code>' + | |
| '<form onsubmit="return window.opener._exec(this.consoleInput);">' + | |
| '<input type="text" name="consoleInput"\/>' + | |
| '<\/form>' + | |
| '<\/body>' + | |
| '<\/html>' | |
| ); | |
| if (enable_console_window_input) { | |
| window._exec = window._exec || function (consoleInput) { | |
| var result; | |
| logging._sendMessage('run', ['>>> ' + consoleInput.value]); | |
| try { | |
| // Eval is Evil, but necessary for this task. | |
| result = eval(consoleInput.value); | |
| switch (typeof result) { | |
| case 'undefined': | |
| if (!result) { | |
| logging._sendMessage('typeof_undefined', ['undefined']); | |
| } else { | |
| logging._sendMessage('log', [result]); | |
| } | |
| break; | |
| case 'string': | |
| logging._sendMessage('typeof_string', ['"' + result + '"']); | |
| break; | |
| case 'function': | |
| logging._sendMessage('typeof_function', ['function()']); | |
| break; | |
| default: | |
| logging._sendMessage('typeof_unknown', [typeof result]); | |
| } | |
| } catch (err) { | |
| logging._sendMessage('error', ['Error: ' + consoleInput.value]); | |
| } | |
| consoleInput.value = ''; | |
| return false; | |
| }; | |
| } | |
| logging._consoleWindow.document.close(); | |
| } catch (ignored) { | |
| logging._consoleWindow = null; | |
| } | |
| } | |
| if (logging._consoleWindow) { | |
| p = logging._consoleWindow.document.createElement('P'); | |
| p.className = 'message ' + method; | |
| message = logging._consoleWindow.document.createElement('SPAN'); | |
| message.innerHTML = split_args; | |
| p.appendChild(message); | |
| logging._consoleWindow.document.getElementById('messages').appendChild(p); | |
| dh = logging._consoleWindow.document.body.scrollHeight; | |
| ch = logging._consoleWindow.document.body.clientHeight; | |
| if (dh > ch) { | |
| logging._consoleWindow.scrollTo(0, dh - ch); | |
| } | |
| } | |
| } | |
| } | |
| }, | |
| log: function () { | |
| logging._sendMessage("log", arguments); | |
| }, | |
| info: function () { | |
| logging._sendMessage("info", arguments); | |
| }, | |
| warn: function () { | |
| logging._sendMessage("warn", arguments); | |
| }, | |
| error: function () { | |
| logging._sendMessage("error", arguments); | |
| }, | |
| count: function () { | |
| logging._sendMessage("count", arguments); | |
| }, | |
| trace: function () { | |
| logging._sendMessage("trace", arguments); | |
| }, | |
| time: function () { | |
| logging._sendMessage("time", arguments); | |
| }, | |
| timeEnd: function () { | |
| logging._sendMessage("timeEnd", arguments); | |
| } | |
| }; | |
| // attach the log function to the global namespace if it is not taken | |
| window.log = window.log || logging.log; | |
| window.info = window.info || logging.info; | |
| window.warn = window.warn || logging.warn; | |
| window.error = window.error || logging.error; | |
| window.count = window.count || logging.count; | |
| window.trace = window.trace || logging.trace; | |
| window.time = window.time || logging.time; | |
| window.timeEnd = window.timeEnd || logging.timeEnd; | |
| if (typeof window.consoleEnabled !== 'undefined' && !window.consoleEnabled) { | |
| // Catching window errors and passing them to the error log. | |
| window.onerror = function () { | |
| logging.error('Error: ' + arguments[0], '<br />File: ' + arguments[1], '<br />Line: ' + arguments[2]); | |
| //return false; | |
| }; | |
| } | |
| iplayer.utils = glow.lang.apply(iplayer.utils, logging); | |
| return logging; | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * @name Logging Utility | |
| * @description Adds logging functions to window namespace. | |
| * Adds console logging to browsers that don't have a console (E.G.: IE 6,7 & 8, Safari 2) | |
| * @author Rob Taylor <robert.taylor1@bbc.co.uk> | |
| */ | |
| // VARIABLES THAT SHOULD BE SET SOMEWHERE | |
| window.debug = { | |
| 'enable_logging': true, | |
| 'enable_popup': true, | |
| 'enable_popup_eval': true | |
| }; | |
| define('iplayer/utils/logging', | |
| function () { | |
| var logging = { | |
| _consoleWindow: null, // Placeholder for console popup, if it's created. | |
| _sendMessage: function (method, args) { | |
| // If a method isn't selected, set it to 'log'. | |
| method = method || 'log'; | |
| var split_args = Array.prototype.join.call(args, ''), | |
| dh = 0, ch = 0, p = '', | |
| message = '', | |
| debug = window.debug || {}, | |
| enable_debug = debug.enable_logging || false, | |
| enable_debug_window = debug.enable_popup || false, | |
| enable_debug_window_input = debug.enable_popup_eval || false; | |
| if (enable_debug) { | |
| // If console and console method exist, use those to send console message. | |
| if (typeof console !== 'undefined' && typeof console[method] !== 'undefined') { | |
| if (typeof console[method] === 'function') { | |
| console[method].apply(console, args); | |
| } else { | |
| // since console.log is an object & not a function in IE, | |
| // you can't call it with apply. Casting the arguments object | |
| // into an Array gives you at least the serialised output | |
| console[method](split_args); | |
| } | |
| // If console doesn't exist, and debug window is enabled, create a console window. | |
| } else if (enable_debug_window) { | |
| // If their isn't a console window yet, make it. | |
| if (!logging._consoleWindow) { | |
| try { | |
| logging._consoleWindow = window.open('', 'loggingConsoleWindow', 'width=750,height=250,menubar=0,toolbar=0,location=no,status=0,scrollbars=1,resizable=1'); | |
| logging._consoleWindow.document.write( | |
| '<html>' + | |
| '<head>' + | |
| '<title>Console<\/title>' + | |
| '<style>' + | |
| 'body{background-color:#FFF;padding:0;margin:0;font:11px monaco;} ' + | |
| (enable_debug_window_input ? 'form{display:inline;} ' : 'form{display:none;} ') + | |
| 'form input{width:100%;border:0 none;font:11px monaco;height:15px;line-height:11px;padding:4px;margin:0px;} ' + | |
| '.message{background-color:#FFF;padding:4px;margin:0px;border-bottom:1px solid #ccc;} .message span{height:15px;line-height:11px;padding:0 2px;margin:1px;border:1px solid #FFF;} ' + | |
| '.warn{background-color:#FFFFC8;} .info{background-color:#EBF5FF;} .run{color:#00F;} .error{background-color:#FFEBEB;color:#F00;}' + | |
| '.typeof_function span{color:#006400;} .typeof_string span{color:#F00;} .typeof_undefined span{background-color:#888;color:#FFF;border-color:#666;}' + | |
| '<\/style>' + | |
| '<\/head>' + | |
| '<body>' + | |
| '<code id="messages"><\/code>' + | |
| '<form onsubmit="return window.opener._exec(this.consoleInput);">' + | |
| '<input type="text" name="consoleInput"\/>' + | |
| '<\/form>' + | |
| '<\/body>' + | |
| '<\/html>' | |
| ); | |
| if (enable_debug_window_input) { | |
| window._exec = window._exec || function (consoleInput) { | |
| var result; | |
| // Repeat the command back to the user. | |
| logging._sendMessage('run', ['>>> ' + consoleInput.value]); | |
| try { | |
| // Eval is Evil, but necessary for this task to run code in the parent window. | |
| result = eval(consoleInput.value); | |
| // Show the eval'd result, or typeof result, to the user. | |
| // Most of this is to replicate FireBug functionality. | |
| switch (typeof result) { | |
| case 'undefined': | |
| if (!result) { | |
| // If their is no result, don't return it. | |
| logging._sendMessage('typeof_undefined', ['undefined']); | |
| } else { | |
| logging._sendMessage('log', [result]); | |
| } | |
| break; | |
| case 'string': | |
| // Show the user the string. | |
| logging._sendMessage('typeof_string', ['"' + result + '"']); | |
| break; | |
| case 'function': | |
| // Identify the that the string is function that hasn't been activated. | |
| logging._sendMessage('typeof_function', ['function()']); | |
| break; | |
| default: | |
| // Display the typeof that hasn't been handled. | |
| logging._sendMessage('typeof_unknown', [typeof result]); | |
| } | |
| } catch (err) { | |
| // Something went wrong, show the error to the user. | |
| logging._sendMessage('error', ['Error: ' + consoleInput.value]); | |
| } | |
| // Reset the console input box to blank. | |
| consoleInput.value = ''; | |
| return false; | |
| }; | |
| } | |
| logging._consoleWindow.document.close(); | |
| } catch (ignored) { | |
| logging._consoleWindow = null; | |
| } | |
| } | |
| // Now that the console window has been created, start adding log messages. | |
| if (logging._consoleWindow) { | |
| p = logging._consoleWindow.document.createElement('P'); | |
| p.className = 'message ' + method; | |
| message = logging._consoleWindow.document.createElement('SPAN'); | |
| message.innerHTML = split_args; | |
| p.appendChild(message); | |
| logging._consoleWindow.document.getElementById('messages').appendChild(p); | |
| dh = logging._consoleWindow.document.body.scrollHeight; | |
| ch = logging._consoleWindow.document.body.clientHeight; | |
| if (dh > ch) { | |
| logging._consoleWindow.scrollTo(0, dh - ch); | |
| } | |
| } | |
| } | |
| } | |
| }, | |
| log: function () { | |
| logging._sendMessage("log", arguments); | |
| }, | |
| info: function () { | |
| logging._sendMessage("info", arguments); | |
| }, | |
| warn: function () { | |
| logging._sendMessage("warn", arguments); | |
| }, | |
| error: function () { | |
| logging._sendMessage("error", arguments); | |
| }, | |
| count: function () { | |
| logging._sendMessage("count", arguments); | |
| }, | |
| trace: function () { | |
| logging._sendMessage("trace", arguments); | |
| }, | |
| time: function () { | |
| logging._sendMessage("time", arguments); | |
| }, | |
| timeEnd: function () { | |
| logging._sendMessage("timeEnd", arguments); | |
| } | |
| }; | |
| // attach the log function to the global namespace if it is not taken | |
| window.log = window.log || logging.log; | |
| window.info = window.info || logging.info; | |
| window.warn = window.warn || logging.warn; | |
| window.error = window.error || logging.error; | |
| window.count = window.count || logging.count; | |
| window.trace = window.trace || logging.trace; | |
| window.time = window.time || logging.time; | |
| window.timeEnd = window.timeEnd || logging.timeEnd; | |
| if (logging._consoleWindow !== null) { | |
| // Catching window errors and passing them to the error log (console window only). | |
| window.onerror = function () { | |
| logging.error('Error: ' + arguments[0], '<br />File: ' + arguments[1], '<br />Line: ' + arguments[2]); | |
| return false; // Prevent the browser from stopping on error. It has been logged. | |
| }; | |
| } | |
| return logging; | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment