1 /** 2 * @fileOverview Allows gadgets to call the log function to publish client logging messages over the hub. 3 * 4 * @name finesse.cslogger.ClientLogger 5 * @requires OpenAjax 6 */ 7 8 var finesse = finesse || {}; 9 finesse.cslogger = finesse.cslogger || {}; 10 11 /** 12 * @class 13 * Allows gadgets to call the log function to publish client logging messages over the hub. 14 */ 15 finesse.cslogger.ClientLogger = (function () { 16 var _hub, _logTopic, _originId, 17 18 /** 19 * Pads a single digit number for display purposes (e.g. '4' shows as '04') 20 * @param num is the number to pad to 2 digits 21 * @returns a two digit padded string 22 */ 23 padTwoDigits = function (num) 24 { 25 return (num < 10) ? '0' + num : num; 26 }, 27 28 /** 29 * Gets a timestamp. 30 * @returns {String} is a timestamp in the following format: HH:MM:SS 31 */ 32 getTimestamp = function () 33 { 34 var date = new Date(), timeStr; 35 timeStr = padTwoDigits(date.getHours()) + ":" + padTwoDigits(date.getMinutes()) + ":" + padTwoDigits(date.getSeconds()); 36 37 return timeStr; 38 }, 39 40 /** 41 * Checks to see if we have a console. 42 * @returns Whether the console object exists. 43 */ 44 hasConsole = function () { 45 try { 46 if (window.console !== undefined) { 47 return true; 48 } 49 } catch (err) { 50 // ignore and return false 51 } 52 53 return false; 54 }, 55 56 /** 57 * Logs a message to a hidden textarea element on the page 58 * 59 * @param str is the string to log. 60 * @param logType is the optional type which is prefixed (default is "LOG") 61 * @private 62 */ 63 writeToLogOutput = function (msg) { 64 var logOutput = document.getElementById("finesseLogOutput"); 65 66 if (logOutput === null) 67 { 68 logOutput = document.createElement("textarea"); 69 logOutput.id = "finesseLogOutput"; 70 logOutput.style.display = "none"; 71 document.body.appendChild(logOutput); 72 } 73 74 if (logOutput.value === "") 75 { 76 logOutput.value = msg; 77 } 78 else 79 { 80 logOutput.value = logOutput.value + "\n" + msg; 81 } 82 }, 83 84 logToConsole = function (str) 85 { 86 var msg, timeStr = getTimestamp(); 87 msg = timeStr + ": " + str; 88 89 // Log to console 90 if (hasConsole()) { 91 console.log(msg); 92 } 93 94 //Uncomment to print logs to hidden textarea. 95 //writeToLogOutput(msg); 96 97 return msg; 98 }; 99 100 return { 101 102 /** 103 * Publishes a Log Message over the hub. 104 * 105 * @param {String} message 106 * The string to log. 107 */ 108 log : function (message) { 109 if(_hub) { 110 _hub.publish(_logTopic, logToConsole(_originId + message)); 111 } 112 }, 113 114 /** 115 * Initiates the client logger with a hub and a gadgetId 116 * @param {Object} hub 117 * the hub to communicate with 118 * @param {Object} gadgetId 119 * a unique string to identify which gadget is doing the logging 120 */ 121 init: function (hub, gadgetId) { 122 _hub = hub; 123 _logTopic = "finesse.clientLogging." + gadgetId; 124 _originId = gadgetId + " : "; 125 } 126 }; 127 }()); 128