1 /**
  2  * @fileOverview Allows each gadget to communicate with the server to send logs.
  3  * @name finesse.cslogger.FinesseLogger
  4  */
  5 
  6 var finesse = finesse || {};
  7 finesse.cslogger = finesse.cslogger || {};
  8 
  9 /**
 10  * @class
 11  * Allows each product to initialize its method of storage
 12  */
 13 finesse.cslogger.FinesseLogger = finesse.cslogger.FinesseLogger || (function () {
 14 
 15     var
 16 
 17     /**
 18      * Array use to collect ongoing logs in memory
 19      * @private
 20      */
 21     _logArray = [],
 22 
 23     /**
 24      * The final data string sent to the server, =_logArray.join
 25      * @private
 26      */
 27     _logStr = "",
 28 
 29     /**
 30      * Keep track of size of log
 31      * @private
 32      */
 33     _logSize = 0,
 34 
 35     /**
 36      * Flag to keep track show/hide of send log link
 37      */
 38     _sendLogShown = false,
 39 
 40     /**
 41      * Collect logs when onCollect called
 42      * @private
 43      */
 44     _collectMethod = function(data) {
 45       //Size of log should not exceed 1.5MB
 46       var info, maxLength = 1572864;
 47       
 48       //add size buffer equal to the size of info to be added when publish
 49 	  info = navigator.userAgent + "
";
 50 	  info = escape(info);
 51 
 52         //If log was empty previously, fade in buttons
 53         if (!_sendLogShown) {
 54             //call the fadeInSendLog() in Footer
 55             finesse.modules.Footer.sendLogAppear();
 56             _sendLogShown = true;
 57             _logSize = info.length;
 58         }
 59 		
 60 		
 61 		//escape all data to get accurate size (shindig will escape when it builds request)
 62 		//escape 6 special chars for XML: &<>"'\n
 63 		data = data.replace(/&/g, "&").replace(/"/g, """).replace(/'/g, "'").replace(/>/g, ">").replace(/</g, "<").replace(/\n/g, "
");
 64 		data = escape(data+"\n");
 65 
 66         if (data.length < maxLength){
 67 			//make room for new data if log is exceeding max length
 68             while (_logSize + data.length > maxLength) {
 69                 _logSize -= (_logArray.shift()).length;
 70             }
 71         }
 72 
 73         //Else push the log into memory, increment the log size
 74         _logArray.push(data);
 75 
 76         //inc the size accordingly
 77         _logSize+=data.length;
 78 
 79     };
 80 
 81     return {
 82 
 83         /**
 84          * Initiate FinesseLogger.
 85          *
 86          * @param {String} id
 87          *  id of the agent
 88          * @param {Object} userObj
 89          *  the parent object, used for constructing the restRequest URL
 90          */
 91 
 92         init: function () {
 93             finesse.clientservices.ClientServices.subscribe("finesse.clientLogging.*", _collectMethod);
 94         },
 95 
 96         /**
 97         * Publish logs to server and clear the memory
 98         */
 99         publish: function(userObj, options, callBack) {
100             // Avoid null references.
101             options = options || {};
102             callBack = callBack || {};
103 
104             if (callBack.sending === "function") {
105                 callBack.sending();
106             }
107 
108             //logs the basic version and machine info and escaped new line
109             _logStr = navigator.userAgent + "
";
110             
111             //join the logs to correct string format
112             _logStr += unescape(_logArray.join(""));
113 
114             //turning log string to JSON obj
115             var logObj = {
116                     ClientLog: {
117                     logData : _logStr //_logStr
118                 }
119             },
120             //
121             tmpOnAdd = (options.onAdd && typeof options.onAdd === "function")? options.onAdd : function(){};
122             options.onAdd = function(){
123                 tmpOnAdd();
124                 _logArray.length = 0; _logSize =0;
125                 _sendLogShown = false;
126                 };
127             //adding onLoad to the callbacks, this is the subscribe success case for the first time user subscribe to the client log node
128             options.onLoad = function (clientLogObj) {
129                 clientLogObj.sendLogs(logObj,{
130                         error: callBack.error
131                     });
132                 };
133 
134             userObj.getClientLog(options);
135         }
136     };
137 
138 }());
139