UNPKG

logline

Version:
3 lines (2 loc) 13.6 kB
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):e.Logline=t()}(this,function(){"use strict";function e(e){return e?d[e]:d}function t(e,t){var o={};"string"==typeof e?o[e]=t:"[object Object]"===Object.prototype.toString.call(e)&&(o=e),s(d,o)}function o(e){_&&console.error("Logline: "+e)}function r(e,t,o,r){_&&g.get().verbose&&window.console[y[t.toUpperCase()]||y.INFO]("["+e+"] "+t.toUpperCase()+" "+o,r||"")}function n(e){var t,o={};if("object"!==(void 0===e?"undefined":a(e)))return e;for(t in e)e.hasOwnProperty(t)&&"function"!=typeof e[t]&&(o[t]=n(e[t]));return o}var a="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},i=function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")},c=function(){function e(e,t){for(var o=0;o<t.length;o++){var r=t[o];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}return function(t,o,r){return o&&e(t.prototype,o),r&&e(t,r),t}}(),s=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var o=arguments[t];for(var r in o)Object.prototype.hasOwnProperty.call(o,r)&&(e[r]=o[r])}return e},u=function e(t,o,r){null===t&&(t=Function.prototype);var n=Object.getOwnPropertyDescriptor(t,o);if(void 0===n){var a=Object.getPrototypeOf(t);return null===a?void 0:e(a,o,r)}if("value"in n)return n.value;var i=n.get;if(void 0!==i)return i.call(r)},l=function(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Super expression must either be null or a function, not "+typeof t);e.prototype=Object.create(t&&t.prototype,{constructor:{value:e,enumerable:!1,writable:!0,configurable:!0}}),t&&(Object.setPrototypeOf?Object.setPrototypeOf(e,t):e.__proto__=t)},f=function(e,t){if(!e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return!t||"object"!=typeof t&&"function"!=typeof t?e:t},p={verbose:!0},d=s({},p),g=t;g.set=t,g.get=e;var _=window.console,y={INFO:"log",WARN:"warn",ERROR:"error",CRITICAL:"error"},v=function(){function e(t){i(this,e),this._namespace=t}return c(e,[{key:"_record",value:function(e,t,r){o("method _record is not implemented.")}},{key:"info",value:function(){for(var e=arguments.length,t=Array(e),o=0;o<e;o++)t[o]=arguments[o];this._record.apply(this,["info"].concat(t))}},{key:"warn",value:function(){for(var e=arguments.length,t=Array(e),o=0;o<e;o++)t[o]=arguments[o];this._record.apply(this,["warn"].concat(t))}},{key:"error",value:function(){for(var e=arguments.length,t=Array(e),o=0;o<e;o++)t[o]=arguments[o];this._record.apply(this,["error"].concat(t))}},{key:"critical",value:function(){for(var e=arguments.length,t=Array(e),o=0;o<e;o++)t[o]=arguments[o];this._record.apply(this,["critical"].concat(t))}}],[{key:"init",value:function(e){return!0}},{key:"transTimeFormat",value:function(e,t){if(!e||/^\d{13}$/.test(e))return+e;if(t&&!/^\d{13}$/.test(t))throw new TypeError("relative time should be standard unix timestamp");return(t||Date.now())-24*e.replace(/d$/,"")*3600*1e3}},{key:"get",value:function(e,t,r){o("method get is not implemented.")}},{key:"keep",value:function(e){o("method keep is not implemented.")}},{key:"clean",value:function(){o("method clean is not implemented.")}},{key:"STATUS",get:function(){return{INITING:1,INITED:2,FAILED:4}}}]),e}(),m=function(){function e(){i(this,e),this._pool=[]}return c(e,[{key:"push",value:function(e,t){e.context=t,this._pool.push(e)}},{key:"consume",value:function(){for(var e;e=this._pool.shift();)e.call(e.context)}}]),e}(),h=function(e){function t(){var e;i(this,t);for(var o=arguments.length,r=Array(o),n=0;n<o;n++)r[n]=arguments[n];return f(this,(e=t.__proto__||Object.getPrototypeOf(t)).call.apply(e,[this].concat(r)))}return l(t,e),c(t,[{key:"_record",value:function(e,a,i){var c=this;try{if(t.status!==v.STATUS.INITED)return t._pool.push(function(){return c._record(e,a,i)}),void(t.status!==v.STATUS.INITING&&t.init());r(this._namespace,e,a,i);var s=t.db.transaction(["logs"],"readwrite");s.onerror=function(e){return o(e.target.error)};s.objectStore("logs").add({time:Date.now(),level:e,namespace:this._namespace,descriptor:a,data:n(i)}).onerror=function(e){t.status=v.STATUS.FAILED,o(e.target.error)}}catch(e){o("failed to write, "+e.message)}}}],[{key:"init",value:function(e){var r=this;try{if(t.support||o("your platform does not support indexeddb protocol."),t.status)return!1;t._pool=t._pool||new m,t._database=e||"logline",t.status=u(t.__proto__||Object.getPrototypeOf(t),"STATUS",this).INITING,t.request=window.indexedDB.open(t._database),t.request.onerror=function(e){return o("protocol indexeddb is prevented.")},t.request.onsuccess=function(e){t.db=e.target.result,t.status=u(t.__proto__||Object.getPrototypeOf(t),"STATUS",r).INITED,t._pool.consume(),t.db.onerror=function(e){return o(e.target.error)}},t.request.onupgradeneeded=function(e){var t=e.target.result,o=t.createObjectStore("logs",{autoIncrement:!0});o.createIndex("namespace","namespace",{unique:!1}),o.createIndex("level","level",{unique:!1}),o.createIndex("descriptor","descriptor",{unique:!1}),o.createIndex("data","data",{unique:!1})}}catch(e){o("failed init, "+e.message)}}},{key:"get",value:function(e,r,n){try{if(t.status!==u(t.__proto__||Object.getPrototypeOf(t),"STATUS",this).INITED)return t._pool.push(function(){return t.get(e,r,n)});e=v.transTimeFormat(e),r=v.transTimeFormat(r);var a=t._getTransactionStore(IDBTransaction.READ_ONLY);if(!a)return n([]);if(a.getAll){var i=void 0,c=[];a.getAll().onsuccess=function(t){i=t.target.result;for(var o=0;o<i.length;o++)e&&i[o].time<e||r&&i[o].time>r||c.push(i[o]);n(c)}}else{var s=a.openCursor(),l=[];s.onsuccess=function(t){var o=t.target.result;if(o){if(e&&o.value.time<e||r&&o.value.time>r)return o.continue();l.push({time:o.value.time,level:o.value.level,namespace:o.value.namespace,descriptor:o.value.descriptor,data:o.value.data}),o.continue()}else n(l)}}}catch(e){o("failed to get logs, "+e.message)}}},{key:"keep",value:function(e){try{if(t.status!==u(t.__proto__||Object.getPrototypeOf(t),"STATUS",this).INITED)return t._pool.push(function(){return t.keep(e)});var r=t._getTransactionStore("readwrite");if(!r)return!1;if(e){var n=Date.now()-24*(e||2)*3600*1e3,a=r.openCursor();a.onsuccess=function(e){var t=e.target.result;t&&t.value.time<n&&(r.delete(t.primaryKey),t.continue())},a.onerror=function(t){return o("unable to locate logs earlier than "+e+"d.")}}else{r.clear().onerror=function(e){return o(e.target.error)}}}catch(e){o("failed to keep logs, "+e.message)}}},{key:"clean",value:function(){try{if(t.status!==u(t.__proto__||Object.getPrototypeOf(t),"STATUS",this).INITED)return t._pool.push(function(){return t.clean()});t.db.close();var e=window.indexedDB.deleteDatabase(t._database);e.onerror=function(e){return o(e.target.error)},e.onsuccess=function(e){delete t.status,delete t.db}}catch(e){o("failed to cleanup logs, "+e.message)}}},{key:"_getTransactionStore",value:function(e){try{if(t.db){var r=t.db.transaction(["logs"],e||"readwrite");return r.onerror=function(e){return o(e.target.error)},r.objectStore("logs")}o("log database is not created or connections are closed, considering init it.")}catch(e){return o("failed to generate new transaction, "+e.message),!1}}},{key:"support",get:function(){return!!(window.indexedDB&&window.IDBTransaction&&window.IDBKeyRange)}}]),t}(v),b=function(e){function t(){var e;i(this,t);for(var o=arguments.length,r=Array(o),n=0;n<o;n++)r[n]=arguments[n];return f(this,(e=t.__proto__||Object.getPrototypeOf(t)).call.apply(e,[this].concat(r)))}return l(t,e),c(t,[{key:"_record",value:function(e,n,a){var i;try{i=window.localStorage.getItem(t._database)?JSON.parse(window.localStorage.getItem(t._database)):[],i.push([Date.now(),this._namespace,e,n,a]),r(this._namespace,e,n,a),window.localStorage.setItem(t._database,JSON.stringify(i))}catch(e){window.localStorage.removeItem(t._database),window.localStorage.setItem(t._database,JSON.stringify([])),o("failed to write, may be localStorage is full, "+e.message)}}}],[{key:"init",value:function(e){try{t.support||o("your platform does not support localstorage protocol."),t._database=e||"logline",window.localStorage.getItem(t._database)||window.localStorage.setItem(t._database,JSON.stringify([])),t.status=u(t.__proto__||Object.getPrototypeOf(t),"STATUS",this).INITED}catch(e){o("failed to init, "+e.message)}}},{key:"get",value:function(e,r,n){var a,i;try{for(a=JSON.parse(window.localStorage.getItem(t._database)),e=v.transTimeFormat(e),r=v.transTimeFormat(r),i=0;i<a.length;i++)e&&a[i][0]<e||r&&a[i][0]>r||(a[i]={time:a[i][0],namespace:a[i][1],level:a[i][2],descriptor:a[i][3],data:a[i][4]});n(a)}catch(e){o("failed to get, "+e.message),n([])}}},{key:"keep",value:function(e){var r;try{r=e?(window.localStorage.getItem(t._database)?JSON.parse(window.localStorage.getItem(t._database)):[]).filter(function(t){return t.time>=Date.now()-24*(e||2)*3600*1e3}):[],window.localStorage.setItem(t._database,JSON.stringify(r))}catch(e){o("failed to keep, "+e.message)}}},{key:"clean",value:function(){try{delete t.status,window.localStorage.removeItem(t._database)}catch(e){o("failed to clean, "+e.message)}}},{key:"support",get:function(){return"localStorage"in window}}]),t}(v),S=function(e){function t(){var e;i(this,t);for(var o=arguments.length,r=Array(o),n=0;n<o;n++)r[n]=arguments[n];return f(this,(e=t.__proto__||Object.getPrototypeOf(t)).call.apply(e,[this].concat(r)))}return l(t,e),c(t,[{key:"_record",value:function(e,n,a){var i=this;if(t.status!==v.STATUS.INITED)return t._pool.push(function(){return i._record(e,n,a)}),void(t.status!==v.STATUS.INITING&&t.init());try{r(this._namespace,e,n,a),t._db.transaction(function(t){t.executeSql("INSERT INTO logs (time, namespace, level, descriptor, data) VALUES(?, ?, ?, ? ,?)",[Date.now(),i._namespace,e,n,void 0===a||""===a?"":JSON.stringify(a)||""],function(){},function(e,t){o("write error, "+t.message)})})}catch(e){o("error inserting record, "+e.message)}}}],[{key:"init",value:function(e){var r=this;if(t.support||o(new Error("your platform does not support websql protocol.")),t.status)return!1;t._pool=t._pool||new m,t._database=e||"logline",t.status=u(t.__proto__||Object.getPrototypeOf(t),"STATUS",this).INITING;try{t._db=window.openDatabase(t._database,"1.0","cats loves logs",5085593.6),t._db.transaction(function(e){e.executeSql("CREATE TABLE IF NOT EXISTS logs (time, namespace, level, descriptor, data)",[],function(){t.status=u(t.__proto__||Object.getPrototypeOf(t),"STATUS",r).INITED,t._pool.consume()},function(e,n){t.status=u(t.__proto__||Object.getPrototypeOf(t),"STATUS",r).FAILED,o("unable to create table, "+n.message)})})}catch(e){o("unable to init log database, "+e.message)}}},{key:"get",value:function(e,r,n){if(t.status!==u(t.__proto__||Object.getPrototypeOf(t),"STATUS",this).INITED)return t._pool.push(function(){return t.get(e,r,n)});e=v.transTimeFormat(e),r=v.transTimeFormat(r);try{t._db.transaction(function(t){t.executeSql("SELECT * FROM logs ORDER BY time DESC",[],function(t,o){for(var a,i,c=[],s=o.rows.length;--s>=0;)if(i=o.rows.item(s),!(e&&i.time<e||r&&i.time>r)){a=JSON.parse(JSON.stringify(i));try{a.data=JSON.parse(a.data)}catch(e){}c.push(a)}n(c)},function(e,t){o(t.message)})})}catch(e){o("unable to collect logs from database.")}}},{key:"keep",value:function(e){if(t.status!==u(t.__proto__||Object.getPrototypeOf(t),"STATUS",this).INITED)return t._pool.push(function(){return t.keep(e)});try{t._db.transaction(function(t){e?t.executeSql("DELETE FROM logs WHERE time < ?",[Date.now()-24*(e||2)*3600*1e3],function(){},function(e,t){o(t.message)}):t.executeSql("DELETE FROM logs",[],function(){},function(e,t){o(t.message)})})}catch(e){o("unable to clean logs from database.")}}},{key:"clean",value:function(){if(t.status!==u(t.__proto__||Object.getPrototypeOf(t),"STATUS",this).INITED)return void t._pool.push(function(){return t.clean()});try{t._db.transaction(function(e){e.executeSql("DROP TABLE logs",[],function(){delete t.status},function(e,t){o(t.message)})})}catch(e){o("unable to clean log database.")}}},{key:"support",get:function(){return"openDatabase"in window}}]),t}(v),w=function(){function e(t){if(i(this,e),!(this instanceof e))return new e(t);try{return e._checkProtocol(),new e._protocol(t)}catch(e){return new v(t)}}return c(e,null,[{key:"_initProtocol",value:function(t){e._protocol=t,e._protocol.init(e._database||"logline")}},{key:"_checkProtocol",value:function(){if(!e._protocol){for(var t=Object.keys(e.PROTOCOL),r=void 0;r=e.PROTOCOL[t.shift()];)if(r.support)return void e._initProtocol(r);o("protocols "+t.join(", ").toLowerCase()+" are not supported on this platform")}}},{key:"get",value:function(t,o,r){switch(e._checkProtocol(),arguments.length){case 1:r=t,t=void 0;break;case 2:r=o,o=void 0}e._protocol.get(t,o,r)}},{key:"all",value:function(t){e.get(t)}},{key:"keep",value:function(t){return e._checkProtocol(),e._protocol.keep(t),this}},{key:"clean",value:function(){return e._checkProtocol(),e._protocol.clean(),this}},{key:"using",value:function(t,r){return-1===[h,b,S].indexOf(t)&&o("specialfied protocol "+(t?t+" ":"")+"is not available"),e._protocol?this:(e.database(r||e._database),e._initProtocol(t),this)}},{key:"database",value:function(t){e._database=t}},{key:"config",get:function(){return g}}]),e}();return w.PROTOCOL={INDEXEDDB:h,LOCALSTORAGE:b,WEBSQL:S},w.INTERFACE=Object.freeze(v),w.env={verbose:!0},w}); //# sourceMappingURL=logline.min.js.map