// Logger Version Number var LOGGER_VERSION = '1.0'; // Turn logging on or off; var LOG_ON = true; // All logging statements whose level is greater than or equal to // DEFAULT_LOG_LEVEL will be processed all others will be ignored var DEFAULT_LOG_LEVEL = DEBUG; // The ID of the node that will have the logging statements appended // to it var LOG_ID = 'Logger'; // The element that should be wrapped around the log messages var LOG_ELEMENT = 'p'; // Constants for logging levels var DEBUG = 0; var INFO = 1; var WARN = 2; var ERROR = 3; var FATAL = 4; // The css classes that will be applied to the logging elements var LOG_CLASSES = new Array("debug","info","warn","error","fatal"); var LOG_ERRORS = 0; var logger = { // Retrieves the element whose id is equal to LOG_ID GetLogger: function (id) { if (arguments.length == 0) { id = LOG_ID; } return document.getElementById(id); }, ShowDebug: function () { logger.ShowMessages(DEBUG); }, ShowInfo: function () { logger.ShowMessages(INFO); }, ShowWarn: function () { logger.ShowMessages(WARN); }, ShowError: function () { logger.ShowMessages(ERROR); }, ShowFatal: function () { logger.ShowMessages(FATAL); }, ShowAll: function () { logger.ShowMessages(); }, // Removes all logging information from the logging element EraseLog:function (logID,ask) { LOG_ERRORS = 0; var debug = logger.GetLogger(logID); if (!debug) { return false; } if (ask && !confirm('Are you sure you wish to erase the log?')) { return false; } var ps = debug.getElementsByTagName(LOG_ELEMENT); var length = ps.length; for (var i = 0; i < length; i++) { debug.removeChild(ps[length - i - 1]); } return true; }, Debug: function (logID, message, descriptionUrl) { logger.ClientEnvironmentTesterLog(logID, "" + message, descriptionUrl, DEBUG); }, Warn: function (logID, message, descriptionUrl) { logger.ClientEnvironmentTesterLog(logID, "" + message, descriptionUrl, WARN); }, Info: function (logID, message, descriptionUrl) { logger.ClientEnvironmentTesterLog(logID, "" + message, descriptionUrl, INFO); }, Error: function (logID, message, descriptionUrl) { LOG_ERRORS++; logger.ClientEnvironmentTesterLog(logID, "" + message, descriptionUrl, ERROR); }, Fatal: function (logID, message, descriptionUrl) { LOG_ERRORS++; logger.ClientEnvironmentTesterLog(logID, "" + message, descriptionUrl, FATAL); }, ShowMessages: function (level, hideOthers) { var showAll = false; // if no level has been specified, use the default if (arguments.length == 0) { level = DEFAULT_LOG_LEVEL; showAll = true; } if (arguments.length < 2) { hideOthers = true; } // retrieve the element and current statements var debug = logger.GetLogger(); if (!debug) { return false; } var ps = debug.getElementsByTagName("p"); if (ps.length == 0) { return true; } // get the number of nodes in the list var l = ps.length; // get the class name for the specified level var lookup = LOG_CLASSES[level]; // loop through all logging statements/

elements... for (var i = l - 1; i >= 0; i--) { // hide all elements by default, if specified if (hideOthers) { logger.HideNode(ps[i]); } // get the class name for this

var c = logger.GetNodeClass(ps[i]); if (c && c.indexOf(lookup) > -1 || showAll) { logger.ShowNode(ps[i]); } } }, // appends a statement to the logging element if the threshold level is exceeded ClientEnvironmentTesterLog: function (logID, message, descriptionUrl, level) { // check to make sure logging is turned on if (!LOG_ON) { return false; } // retrieve the infrastructure if (arguments.length == 1) { level = INFO;} if (level < DEFAULT_LOG_LEVEL) { return false; } var div = logger.GetLogger(logID); if (!div) { return false; } // append the statement var p = document.createElement(LOG_ELEMENT); // this is a hack work around a bug in ie if (p.getAttributeNode("class")) { for (var i = 0; i < p.attributes.length; i++) { if (p.attributes[i].name.toUpperCase() == 'CLASS') { p.attributes[i].value = LOG_CLASSES[level]; } } } else { p.setAttribute("class", LOG_CLASSES[level]); } var a = document.createElement('a'); var hrefUrl = applicationPath + "/Modules/ClientEnvironmentTester/Resources/TestDescriptions.aspx" + descriptionUrl; a.setAttribute("href", hrefUrl); a.setAttribute("title", 'Click to learn more about the test'); a.setAttribute("target","_blank"); var text = document.createTextNode(message); a.appendChild(text); p.appendChild(a); div.appendChild(p); return true; }, // show a node ShowNode: function (target) { target.style.display = ""; return true; }, // hide a node HideNode: function (target) { target.style.display = "none"; return true; }, // returns the class attribute of a node GetNodeClass: function (obj) { var result = false; if (obj.getAttributeNode("class")) { result = obj.attributes.getNamedItem("class").value; } return result; }, HasErrors: function() { return LOG_ERRORS > 0; } } // Only override the window's error handler if logging is turned on if (LOG_ON) { window.onerror = WindowError; } function WindowError(message, url, line) { logger.ClientEnvironmentTesterLog('Error on line ' + line + ' of document ' + url + ': ' + message, FATAL); return true; }