-
Notifications
You must be signed in to change notification settings - Fork 7
/
driftwood.min.js
11 lines (11 loc) · 11.8 KB
/
driftwood.min.js
1
2
3
4
5
6
7
8
9
10
11
// **Driftwood.js** is a super simple logging and exception tracking library for client side javascript. Works in all major browsers.
//
//[Driftwood on Github](https://github.com/errplane/driftwood.js)
//
//Matthew Kanwisher [Errplane Inc](http://errplane.com)
//MIT License
//Copyright 2012 Errplane
// Driftwood namespace is a static namespace for logging, if you want instances of loggers do:
//
// var logger = new Driftwood.logger();
function ISODateString(a){function b(a){return a<10?"0"+a:a}return a.getUTCFullYear()+"-"+b(a.getUTCMonth()+1)+"-"+b(a.getUTCDate())+"T"+b(a.getUTCHours())+":"+b(a.getUTCMinutes())+":"+b(a.getUTCSeconds())+"Z"}function printStackTrace(a){a=a||{guess:!0};var b=a.e||null,c=!!a.guess,d=new printStackTrace.implementation,e=d.run(b);return c?d.guessAnonymousFunctions(e):e}var Driftwood=new function(){this.logger=function(){function d(a){var b=document.createElement("script");b.src=a,document.body.appendChild(b)}function e(a){var c=b.serverPath+encodeURIComponent(JSON.stringify(a));return c}function f(a){var c={application_name:b.applicationName,message:a||"",url:window.location.toString(),language:"javascript",custom_data:{hostname:window.location.hostname,user_agent:navigator.userAgent||"",referrer:document.referrer||"",cookies:document.cookie||""},backtrace:g(a),exception_class:"Javscript#Unknown"};return c}function g(a,b,c){var d=["no backtrace"];return typeof a=="object"?d=printStackTrace({e:a}):d=printStackTrace(),d}function h(a,b){var c=null;typeof a=="object"&&(c=a,a=c.message);var g=f(a,b),h=e(g);d(h)}function i(a,d,e){var f=c(d),g=new Date;f>=b.consoleLevelId&&(a[0]=d+":"+"["+ISODateString(g)+"] "+a[0]),f>=b.exceptionLevelId&&h(a[0]),e.apply(console,Array.prototype.slice.call(a))}var a=["DEBUG","INFO","ERROR","EXCEPTION","NONE"],b={consoleLevel:"DEBUG",consoleLevelId:0,exceptionLevel:"NONE",exceptionLevelId:99,mode:"development",serverPath:"/exceptions/notify?payload=",applicationName:"js_client"},c=function(b){return a.indexOf(b.toUpperCase())};return{setServerPath:function(a){b.serverPath=a},env:function(a){a.toLowerCase()=="development"?(b.consoleLevel="DEBUG",b.exceptionLevel="none",b.consoleLevelId=0,b.exceptionLevelId=4):a.toLowerCase()=="production"?(b.consoleLevel="ERROR",b.exceptionLevel="none",b.consoleLevelId=2,b.exceptionLevelId=3):console.log("Unknown environment level")},applicationName:function(a){b.applicationName=a},logLevel:function(a){var d=c(a);d>-1?(b.consoleLevel=a.toUpperCase(),b.consoleLevelId=d):console.log("Setting an invalid log level: "+a)},exceptionLevel:function(a){var d=c(a);d>-1?(b.exceptionLevel=a.toUpperCase(),b.exceptionLevelId=d):console.log("Setting an invalid log level: "+a)},debug:function(){i(arguments,"DEBUG",console.debug)},info:function(){i(arguments,"INFO",console.info)},error:function(){i(arguments,"ERROR",console.error)},exception:function(a){i(a,"EXCEPTION",console.error)}}},defaultLogger=new this.logger,this.exception=function(){defaultLogger.exception(arguments)},this.applicationName=function(a){defaultLogger.applicationName(a)},this.debug=defaultLogger.debug,this.info=defaultLogger.info,this.error=defaultLogger.error,this.env=function(a){defaultLogger.env(a)},this.setServerPath=function(a){defaultLogger.setServerPath(a)},this.exceptionLevel=function(a){defaultLogger.exceptionLevel(a)},this.logLevel=function(a){defaultLogger.logLevel(a)}};window.onerror=function(a,b,c){Driftwood.exception(a,{url:b,line:c})},Array.prototype.indexOf||(Array.prototype.indexOf=function(a){"use strict";if(this===void 0||this===null)throw new TypeError;var b=Object(this),c=b.length>>>0;if(c===0)return-1;var d=0;arguments.length>0&&(d=Number(arguments[1]),d!==d?d=0:d!==0&&d!==1/0&&d!==-Infinity&&(d=(d>0||-1)*Math.floor(Math.abs(d))));if(d>=c)return-1;var e=d>=0?d:Math.max(c-Math.abs(d),0);for(;e<c;e++)if(e in b&&b[e]===a)return e;return-1});var JSON;JSON||(JSON={}),function(){function f(a){return a<10?"0"+a:a}function quote(a){return escapable.lastIndex=0,escapable.test(a)?'"'+a.replace(escapable,function(a){var b=meta[a];return typeof b=="string"?b:"\\u"+("0000"+a.charCodeAt(0).toString(16)).slice(-4)})+'"':'"'+a+'"'}function str(a,b){var c,d,e,f,g=gap,h,i=b[a];i&&typeof i=="object"&&typeof i.toJSON=="function"&&(i=i.toJSON(a)),typeof rep=="function"&&(i=rep.call(b,a,i));switch(typeof i){case"string":return quote(i);case"number":return isFinite(i)?String(i):"null";case"boolean":case"null":return String(i);case"object":if(!i)return"null";gap+=indent,h=[];if(Object.prototype.toString.apply(i)==="[object Array]"){f=i.length;for(c=0;c<f;c+=1)h[c]=str(c,i)||"null";return e=h.length===0?"[]":gap?"[\n"+gap+h.join(",\n"+gap)+"\n"+g+"]":"["+h.join(",")+"]",gap=g,e}if(rep&&typeof rep=="object"){f=rep.length;for(c=0;c<f;c+=1)typeof rep[c]=="string"&&(d=rep[c],e=str(d,i),e&&h.push(quote(d)+(gap?": ":":")+e))}else for(d in i)Object.prototype.hasOwnProperty.call(i,d)&&(e=str(d,i),e&&h.push(quote(d)+(gap?": ":":")+e));return e=h.length===0?"{}":gap?"{\n"+gap+h.join(",\n"+gap)+"\n"+g+"}":"{"+h.join(",")+"}",gap=g,e}}"use strict",typeof Date.prototype.toJSON!="function"&&(Date.prototype.toJSON=function(a){return isFinite(this.valueOf())?this.getUTCFullYear()+"-"+f(this.getUTCMonth()+1)+"-"+f(this.getUTCDate())+"T"+f(this.getUTCHours())+":"+f(this.getUTCMinutes())+":"+f(this.getUTCSeconds())+"Z":null},String.prototype.toJSON=Number.prototype.toJSON=Boolean.prototype.toJSON=function(a){return this.valueOf()});var cx=/[\u0000\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,escapable=/[\\\"\x00-\x1f\x7f-\x9f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,gap,indent,meta={"\b":"\\b"," ":"\\t","\n":"\\n","\f":"\\f","\r":"\\r",'"':'\\"',"\\":"\\\\"},rep;typeof JSON.stringify!="function"&&(JSON.stringify=function(a,b,c){var d;gap="",indent="";if(typeof c=="number")for(d=0;d<c;d+=1)indent+=" ";else typeof c=="string"&&(indent=c);rep=b;if(!b||typeof b=="function"||typeof b=="object"&&typeof b.length=="number")return str("",{"":a});throw new Error("JSON.stringify")}),typeof JSON.parse!="function"&&(JSON.parse=function(text,reviver){function walk(a,b){var c,d,e=a[b];if(e&&typeof e=="object")for(c in e)Object.prototype.hasOwnProperty.call(e,c)&&(d=walk(e,c),d!==undefined?e[c]=d:delete e[c]);return reviver.call(a,b,e)}var j;text=String(text),cx.lastIndex=0,cx.test(text)&&(text=text.replace(cx,function(a){return"\\u"+("0000"+a.charCodeAt(0).toString(16)).slice(-4)}));if(/^[\],:{}\s]*$/.test(text.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g,"@").replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g,"]").replace(/(?:^|:|,)(?:\s*\[)+/g,"")))return j=eval("("+text+")"),typeof reviver=="function"?walk({"":j},""):j;throw new SyntaxError("JSON.parse")})}(),printStackTrace.implementation=function(){},printStackTrace.implementation.prototype={run:function(a,b){return a=a||this.createException(),b=b||this.mode(a),b==="other"?this.other(arguments.callee):this[b](a)},createException:function(){try{this.undef()}catch(a){return a}},mode:function(a){return a.arguments&&a.stack?"chrome":typeof a.message=="string"&&typeof window!="undefined"&&window.opera?a.stacktrace?a.message.indexOf("\n")>-1&&a.message.split("\n").length>a.stacktrace.split("\n").length?"opera9":a.stack?a.stacktrace.indexOf("called from line")<0?"opera10b":"opera11":"opera10a":"opera9":a.stack?"firefox":"other"},instrumentFunction:function(a,b,c){a=a||window;var d=a[b];a[b]=function(){return c.call(this,printStackTrace().slice(4)),a[b]._instrumented.apply(this,arguments)},a[b]._instrumented=d},deinstrumentFunction:function(a,b){a[b].constructor===Function&&a[b]._instrumented&&a[b]._instrumented.constructor===Function&&(a[b]=a[b]._instrumented)},chrome:function(a){var b=(a.stack+"\n").replace(/^\S[^\(]+?[\n$]/gm,"").replace(/^\s+(at eval )?at\s+/gm,"").replace(/^([^\(]+?)([\n$])/gm,"{anonymous}()@$1$2").replace(/^Object.<anonymous>\s*\(([^\)]+)\)/gm,"{anonymous}()@$1").split("\n");return b.pop(),b},firefox:function(a){return a.stack.replace(/(?:\n@:0)?\s+$/m,"").replace(/^\(/gm,"{anonymous}(").split("\n")},opera11:function(a){var b="{anonymous}",c=/^.*line (\d+), column (\d+)(?: in (.+))? in (\S+):$/,d=a.stacktrace.split("\n"),e=[];for(var f=0,g=d.length;f<g;f+=2){var h=c.exec(d[f]);if(h){var i=h[4]+":"+h[1]+":"+h[2],j=h[3]||"global code";j=j.replace(/<anonymous function: (\S+)>/,"$1").replace(/<anonymous function>/,b),e.push(j+"@"+i+" -- "+d[f+1].replace(/^\s+/,""))}}return e},opera10b:function(a){var b="{anonymous}",c=/^(.*)@(.+):(\d+)$/,d=a.stacktrace.split("\n"),e=[];for(var f=0,g=d.length;f<g;f++){var h=c.exec(d[f]);if(h){var i=h[1]?h[1]+"()":"global code";e.push(i+"@"+h[2]+":"+h[3])}}return e},opera10a:function(a){var b="{anonymous}",c=/Line (\d+).*script (?:in )?(\S+)(?:: In function (\S+))?$/i,d=a.stacktrace.split("\n"),e=[];for(var f=0,g=d.length;f<g;f+=2){var h=c.exec(d[f]);if(h){var i=h[3]||b;e.push(i+"()@"+h[2]+":"+h[1]+" -- "+d[f+1].replace(/^\s+/,""))}}return e},opera9:function(a){var b="{anonymous}",c=/Line (\d+).*script (?:in )?(\S+)/i,d=a.message.split("\n"),e=[];for(var f=2,g=d.length;f<g;f+=2){var h=c.exec(d[f]);h&&e.push(b+"()@"+h[2]+":"+h[1]+" -- "+d[f+1].replace(/^\s+/,""))}return e},other:function(a){var b="{anonymous}",c=/function\s*([\w\-$]+)?\s*\(/i,d=[],e,f,g=10;while(a&&a.arguments&&d.length<g)e=c.test(a.toString())?RegExp.$1||b:b,f=Array.prototype.slice.call(a.arguments||[]),d[d.length]=e+"("+this.stringifyArguments(f)+")",a=a.caller;return d},stringifyArguments:function(a){var b=[],c=Array.prototype.slice;for(var d=0;d<a.length;++d){var e=a[d];e===undefined?b[d]="undefined":e===null?b[d]="null":e.constructor&&(e.constructor===Array?e.length<3?b[d]="["+this.stringifyArguments(e)+"]":b[d]="["+this.stringifyArguments(c.call(e,0,1))+"..."+this.stringifyArguments(c.call(e,-1))+"]":e.constructor===Object?b[d]="#object":e.constructor===Function?b[d]="#function":e.constructor===String?b[d]='"'+e+'"':e.constructor===Number&&(b[d]=e))}return b.join(",")},sourceCache:{},ajax:function(a){var b=this.createXMLHTTPObject();if(b)try{return b.open("GET",a,!1),b.notify(null),b.responseText}catch(c){}return""},createXMLHTTPObject:function(){var a,b=[function(){return new XMLHttpRequest},function(){return new ActiveXObject("Msxml2.XMLHTTP")},function(){return new ActiveXObject("Msxml3.XMLHTTP")},function(){return new ActiveXObject("Microsoft.XMLHTTP")}];for(var c=0;c<b.length;c++)try{return a=b[c](),this.createXMLHTTPObject=b[c],a}catch(d){}},isSameDomain:function(a){return a.indexOf(location.hostname)!==-1},getSource:function(a){return a in this.sourceCache||(this.sourceCache[a]=this.ajax(a).split("\n")),this.sourceCache[a]},guessAnonymousFunctions:function(a){for(var b=0;b<a.length;++b){var c=/\{anonymous\}\(.*\)@(.*)/,d=/^(.*?)(?::(\d+))(?::(\d+))?(?: -- .+)?$/,e=a[b],f=c.exec(e);if(f){var g=d.exec(f[1]);if(g){var h=g[1],i=g[2],j=g[3]||0;if(h&&this.isSameDomain(h)&&i){var k=this.guessAnonymousFunction(h,i,j);a[b]=e.replace("{anonymous}",k)}}}}return a},guessAnonymousFunction:function(a,b,c){var d;try{d=this.findFunctionName(this.getSource(a),b)}catch(e){d="getSource failed with url: "+a+", exception: "+e.toString()}return d},findFunctionName:function(a,b){var c=/function\s+([^(]*?)\s*\(([^)]*)\)/,d=/['"]?([0-9A-Za-z_]+)['"]?\s*[:=]\s*function\b/,e=/['"]?([0-9A-Za-z_]+)['"]?\s*[:=]\s*(?:eval|new Function)\b/,f="",g,h=Math.min(b,20),i,j;for(var k=0;k<h;++k){g=a[b-k-1],j=g.indexOf("//"),j>=0&&(g=g.substr(0,j));if(g){f=g+f,i=d.exec(f);if(i&&i[1])return i[1];i=c.exec(f);if(i&&i[1])return i[1];i=e.exec(f);if(i&&i[1])return i[1]}}return"(?)"}};if(typeof console=="undefined")var console={log:function(){}};Function.prototype.bind&&console&&typeof console.log=="object"&&["log","info","warn","error","assert","dir","clear","profile","profileEnd"].forEach(function(a){console[a]=this.bind(console[a],console)},Function.prototype.call),typeof console.debug=="undefined"&&(console.debug=console.log);