PageRenderTime 45ms CodeModel.GetById 30ms app.highlight 12ms RepoModel.GetById 1ms app.codeStats 0ms

/BMTmobile-master/assets/Log.js

https://gitlab.com/webservers/codeigniter_mobile_dev_server
JavaScript | 165 lines | 101 code | 7 blank | 57 comment | 42 complexity | aa19eb51fc9dd3b1b1194b7ac52a9d0e MD5 | raw file
  1/* Log.js
  2 * 
  3 * Author: Brad Steffy
  4 * 
  5 * 
  6 * LogJs came from the need to toggle the display of certain log's
  7 *  I often use log statements to debug apps, and they tend to strangle each other.
  8 *  So I thought that why not have more log types even custom types
  9 *  and support multiple output formats
 10 * 
 11 *  I'd like to extend LogJS by creating
 12 *  LogJsAjax: upload logs to a server and flush cache
 13 * 
 14 *  and
 15 * 
 16 *  LogJsLocalStorage: save logs localy and flush cache
 17 * 
 18 *  these should be accomplished by adding callBack checkers in _echo
 19 */
 20var Log = null;
 21//Logs messages and optionally stores data to them
 22//displays in FIFO format and deletes displayed messages
 23var Log = {
 24	catagories: new Array(),  //array of objects
 25	outputMode: "console",
 26	LogCmd: Ti.API.log,
 27	disp: true, //Global Disp Override
 28	LogCallback: null,
 29	findInCatagory: function(k, v){
 30		if(!isArray(Log.catagories) || !isDefined(k) || !isDefined(v)) return null;
 31        var r = null;		
 32		for(var i=0;i<Log.catagories.length;i++){
 33		    //Log.LogCmd(i+a[i][k]);
 34		    if(Log.catagories[i]["args"][k] == v){
 35                if(r == null)  r = i;  //just return the number for one                           
 36                else if(! isArray(r)){ //convert to array for more than one
 37                    var t = new Array();
 38                    t.push(r); //push the one int for 1st element
 39                    t.push(i); //also push this element 2nd
 40                    r = t;
 41                }
 42				else if(isArray(r)){ //add to array
 43                    r.push(i);
 44                }                 
 45            }
 46		}
 47		return r;
 48	},
 49	addCatagory: function(o){
 50	    //array search catagories for name, add it if it's not there
 51	    if(!isDefined(o.name)){ Log.LogCmd("[LOG] Error: You Must Specify Name{name: 'theName'}"); return false;}
 52	    if(Log.findInCatagory("name", o.name) == null){
 53	        var c = new Object(); //create catagory object
 54	        c.data = new Array();
 55	        c.args = o;
 56	        //c.args.disp = false;
 57	        //if(isTrue(o.disp))  	c.args.disp = true;
 58	        Log.catagories.push(c);
 59	        Log[o.name] = function(msg){ //add the function to the object literal key
 60		       //Log function is added dynamically
 61		       //name is the name of c.name && Log function name
 62		       //equivelant to log statement for each catagory
 63		       var ci = Log.findInCatagory("name", o.name); //try not to search every time
 64		       if(ci != null){ 
 65			    //alert("name: "+o.name+" id: "+ci);
 66		           var c = new Object();
 67		           c.time = new Date().getTime();
 68		           c.args = arguments;
 69		           Log.catagories[ci].data.push(c);
 70		       }
 71	           Log.clear = function(){
 72	           		//Log.LogCmd("clearing cache"+ci);
 73					Log.catagories[ci].data = new Array();
 74	           }
 75		       Log._echo(ci); //echo determines if disp == true
 76		       return Log;
 77	   		};
 78	    } else {
 79	        Log.LogCmd("[Log] Error: catagory already exists");
 80	    }
 81	    return this;
 82	},
 83	getMessages: function(ci){
 84		if(typeof ci != "number")
 85			ci = Log.findInCatagory("name", ci);
 86		if(ci < Log.catagories.length ){
 87			return Log.catagories[ci].data;
 88		}
 89		return false;
 90	},
 91	outputString: function(name, msg){
 92			if(Log.outputMode == "console"){
 93				return "["+name.toUpperCase()+"] "+msg;
 94			}
 95			else if(Log.outputMode == "html"){
 96				return "<br />["+name.toUpperCase()+"] "+msg;
 97			}
 98	},
 99	outputMsgs: function(name, Msgs){
100        for(var i=0;i<Msgs.length;i++){
101			Log.LogCmd(Log.outputString(name, Msgs[i].args[0]));
102		}
103	},
104	outputLastMsg: function(name, Msgs){
105		Log.LogCmd(Log.outputString(name, Msgs[(Msgs.length-1)].args[0]));			
106	},	
107	_echo: function(ci){ //echo all logs for anyCategory that has arg.disp set to true
108		if(typeof(ci) != 'number' && Log.disp){ //looksup all displayable logs and displays them
109			//don't worry about a specific item display them all
110	    	var dl = Log.findInCatagory("disp", true); //returns an array of array offsets for args.disp = true
111        	if(isArray(dl)){
112	       	    for(var i=0;i<dl.length;i++){
113	       			if(Log.catagories[dl[i]].args.disp) Log.outputMsgs(Log.catagories[dl[i]].args.name, Log.catagories[dl[i]].data);
114	        	}
115	        } else if(typeof(dl) == 'number'){
116	        	if(Log.catagories[dl].args.disp)    Log.outputMsgs(Log.catagories[dl].args.name, Log.catagories[dl].data); //it's just a number not an array so don't loop
117	        }
118        }else if(typeof(ci) == 'number' && Log.disp){ //calling echo by Log.XYZ("log statement");
119        	if(Log.catagories[ci].args.disp)
120        		Log.outputLastMsg(Log.catagories[ci].args.name, Log.catagories[ci].data);
121        	//Log.catagories[ci].data = new Array();
122        }
123        if(isFunction(Log.LogCallback))   	Log.LogCallback(ci);
124	    return this;
125	}
126};
127
128/* Create a catagory
129 * display is automatically true, so no need for disp:true
130 */
131//Log.addCatagory({name: "info"});
132//Log.addCatagory({name: "debug", disp: false});
133
134
135/* Creating a catagory creates a function to log for that catagory
136 * you can add data to the log statement
137 * 
138 * you can clear the cache also
139 */
140//Log.info("info1", {data: 34, school: "berkley"});
141//Log.info().clear();
142
143
144/* creating the callback ------
145 * 
146 * var i = Log.catagories offset
147 * 
148 * var f = function(i){
149 *    alert(i);
150 * }
151 * Log.LogCallback = f;
152 * 
153 */
154
155//OverRide Logs to buffer locally and upload when bufferSize Matches
156function OverRide_Log(){
157	Log.LogCmd = Ti.API.info;
158	Log.addCatagory({name: "info", disp: false});
159	Log.addCatagory({name: "error", disp: true});	
160	eInfo = Log.info;
161	eError = Log.Error;
162}
163//LogCallback
164//Log.getMessages('info');
165