diff options
| author | Pliable Pixels <pliablepixels@gmail.com> | 2017-09-21 12:49:18 -0400 |
|---|---|---|
| committer | Pliable Pixels <pliablepixels@gmail.com> | 2017-09-21 12:49:18 -0400 |
| commit | b28028ac4082842143b0f528d6bc539da6ccb419 (patch) | |
| tree | 1e26ea969a781ed8e323fca4e3c76345113fc694 /www/lib/filelogger/dist | |
| parent | 676270d21beed31d767a06c89522198c77d5d865 (diff) | |
mega changes, including updates and X
Diffstat (limited to 'www/lib/filelogger/dist')
| -rw-r--r-- | www/lib/filelogger/dist/filelogger.js | 358 | ||||
| -rw-r--r-- | www/lib/filelogger/dist/filelogger.min.js | 6 |
2 files changed, 364 insertions, 0 deletions
diff --git a/www/lib/filelogger/dist/filelogger.js b/www/lib/filelogger/dist/filelogger.js new file mode 100644 index 00000000..8a90ab5c --- /dev/null +++ b/www/lib/filelogger/dist/filelogger.js @@ -0,0 +1,358 @@ +/*! + * fileLogger + * Copyright 2016 Peter Bakondy https://github.com/pbakondy + * See LICENSE in this repository for license information + */ +(function(){ +/* global angular, console, cordova */ +/* eslint no-console:0 */ + +// install : cordova plugin add cordova-plugin-file +// date format: https://docs.angularjs.org/api/ng/filter/date + +angular.module('fileLogger', ['ngCordova.plugins.file']) + + .factory('$fileLogger', ['$q', '$window', '$cordovaFile', '$timeout', '$filter', + function ($q, $window, $cordovaFile, $timeout, $filter) { + + 'use strict'; + + + var queue = []; + var ongoing = false; + var levels = ['DEBUG', 'INFO', 'WARN', 'ERROR']; + + var storageFilename = 'messages.log'; + + var dateFormat; + var dateTimezone; + + + function isBrowser() { + return (!$window.cordova && !$window.PhoneGap && !$window.phonegap); + } + + + function log(level) { + if (angular.isString(level)) { + level = level.toUpperCase(); + + if (levels.indexOf(level) === -1) { + level = 'INFO'; + } + } else { + level = 'INFO'; + } + + var now = new Date(); + var timestamp = dateFormat ? + $filter('date')(now, dateFormat, dateTimezone) : now.toJSON(); + + var messages = Array.prototype.slice.call(arguments, 1); + var message = [ timestamp, level ]; + var text; + + for (var i = 0; i < messages.length; i++ ) { + if (angular.isArray(messages[i])) { + text = '[Array]'; + try { + // avoid "TypeError: Converting circular structure to JSON" + text = JSON.stringify(messages[i]); + } catch(e) { + // do nothing + } + message.push(text); + } + else if (angular.isObject(messages[i])) { + text = '[Object]'; + try { + // avoid "TypeError: Converting circular structure to JSON" + text = JSON.stringify(messages[i]); + } catch(e) { + // do nothing + } + message.push(text); + } + else { + message.push(messages[i]); + } + } + + if (isBrowser()) { + // log to browser console + + messages.unshift(timestamp); + + if (angular.isObject(console) && angular.isFunction(console.log)) { + switch (level) { + case 'DEBUG': + if (angular.isFunction(console.debug)) { + console.debug.apply(console, messages); + } else { + console.log.apply(console, messages); + } + break; + case 'INFO': + if (angular.isFunction(console.debug)) { + console.info.apply(console, messages); + } else { + console.log.apply(console, messages); + } + break; + case 'WARN': + if (angular.isFunction(console.debug)) { + console.warn.apply(console, messages); + } else { + console.log.apply(console, messages); + } + break; + case 'ERROR': + if (angular.isFunction(console.debug)) { + console.error.apply(console, messages); + } else { + console.log.apply(console, messages); + } + break; + default: + console.log.apply(console, messages); + } + } + + } else { + // log to logcat + console.log(message.join(' ')); + } + + queue.push({ message: message.join(' ') + '\n' }); + + if (!ongoing) { + process(); + } + } + + + function process() { + + if (!queue.length) { + ongoing = false; + return; + } + + ongoing = true; + var m = queue.shift(); + + writeLog(m.message).then( + function() { + $timeout(function() { + process(); + }); + }, + function() { + $timeout(function() { + process(); + }); + } + ); + + } + + + function writeLog(message) { + var q = $q.defer(); + + if (isBrowser()) { + // running in browser with 'ionic serve' + + if (!$window.localStorage[storageFilename]) { + $window.localStorage[storageFilename] = ''; + } + + $window.localStorage[storageFilename] += message; + q.resolve(); + + } else { + + if (!$window.cordova || !$window.cordova.file || !$window.cordova.file.dataDirectory) { + q.reject('cordova.file.dataDirectory is not available'); + return q.promise; + } + + $cordovaFile.checkFile(cordova.file.dataDirectory, storageFilename).then( + function() { + // writeExistingFile(path, fileName, text) + $cordovaFile.writeExistingFile(cordova.file.dataDirectory, storageFilename, message).then( + function() { + q.resolve(); + }, + function(error) { + q.reject(error); + } + ); + }, + function() { + // writeFile(path, fileName, text, replaceBool) + $cordovaFile.writeFile(cordova.file.dataDirectory, storageFilename, message, true).then( + function() { + q.resolve(); + }, + function(error) { + q.reject(error); + } + ); + } + ); + + } + + return q.promise; + } + + + function getLogfile() { + var q = $q.defer(); + + if (isBrowser()) { + q.resolve($window.localStorage[storageFilename]); + } else { + + if (!$window.cordova || !$window.cordova.file || !$window.cordova.file.dataDirectory) { + q.reject('cordova.file.dataDirectory is not available'); + return q.promise; + } + + $cordovaFile.readAsText(cordova.file.dataDirectory, storageFilename).then( + function(result) { + q.resolve(result); + }, + function(error) { + q.reject(error); + } + ); + } + + return q.promise; + } + + + function deleteLogfile() { + var q = $q.defer(); + + if (isBrowser()) { + $window.localStorage.removeItem(storageFilename); + q.resolve(); + } else { + + if (!$window.cordova || !$window.cordova.file || !$window.cordova.file.dataDirectory) { + q.reject('cordova.file.dataDirectory is not available'); + return q.promise; + } + + $cordovaFile.removeFile(cordova.file.dataDirectory, storageFilename).then( + function(result) { + q.resolve(result); + }, + function(error) { + q.reject(error); + } + ); + } + + return q.promise; + } + + + function setStorageFilename(filename) { + if (angular.isString(filename) && filename.length > 0) { + storageFilename = filename; + return true; + } else { + return false; + } + } + + + function setTimestampFormat(format, timezone) { + if (!(angular.isUndefined(format) || angular.isString(format))) { + throw new TypeError('format parameter must be a string or undefined'); + } + if (!(angular.isUndefined(timezone) || angular.isString(timezone))) { + throw new TypeError('timezone parameter must be a string or undefined'); + } + + dateFormat = format; + dateTimezone = timezone; + } + + + function checkFile() { + var q = $q.defer(); + + if (isBrowser()) { + + q.resolve({ + 'name': storageFilename, + 'localURL': 'localStorage://localhost/' + storageFilename, + 'type': 'text/plain', + 'size': ($window.localStorage[storageFilename] ? $window.localStorage[storageFilename].length : 0) + }); + + } else { + + if (!$window.cordova || !$window.cordova.file || !$window.cordova.file.dataDirectory) { + q.reject('cordova.file.dataDirectory is not available'); + return q.promise; + } + + $cordovaFile.checkFile(cordova.file.dataDirectory, storageFilename).then(function(fileEntry) { + fileEntry.file(q.resolve, q.reject); + }, q.reject); + + } + + return q.promise; + } + + function debug() { + var args = Array.prototype.slice.call(arguments, 0); + args.unshift('DEBUG'); + log.apply(undefined, args); + } + + + function info() { + var args = Array.prototype.slice.call(arguments, 0); + args.unshift('INFO'); + log.apply(undefined, args); + } + + + function warn() { + var args = Array.prototype.slice.call(arguments, 0); + args.unshift('WARN'); + log.apply(undefined, args); + } + + + function error() { + var args = Array.prototype.slice.call(arguments, 0); + args.unshift('ERROR'); + log.apply(undefined, args); + } + + + return { + log: log, + getLogfile: getLogfile, + deleteLogfile: deleteLogfile, + setStorageFilename: setStorageFilename, + setTimestampFormat: setTimestampFormat, + checkFile: checkFile, + debug: debug, + info: info, + warn: warn, + error: error + }; + + }]); + +})();
\ No newline at end of file diff --git a/www/lib/filelogger/dist/filelogger.min.js b/www/lib/filelogger/dist/filelogger.min.js new file mode 100644 index 00000000..de02b863 --- /dev/null +++ b/www/lib/filelogger/dist/filelogger.min.js @@ -0,0 +1,6 @@ +/*! + * fileLogger + * Copyright 2016 Peter Bakondy https://github.com/pbakondy + * See LICENSE in this repository for license information + */ +!function(){angular.module("fileLogger",["ngCordova.plugins.file"]).factory("$fileLogger",["$q","$window","$cordovaFile","$timeout","$filter",function(e,o,r,a,n){"use strict";function t(){return!o.cordova&&!o.PhoneGap&&!o.phonegap}function i(e){angular.isString(e)?(e=e.toUpperCase(),-1===S.indexOf(e)&&(e="INFO")):e="INFO";for(var o,r=new Date,a=m?n("date")(r,m,b):r.toJSON(),i=Array.prototype.slice.call(arguments,1),c=[a,e],s=0;s<i.length;s++)if(angular.isArray(i[s])){o="[Array]";try{o=JSON.stringify(i[s])}catch(f){}c.push(o)}else if(angular.isObject(i[s])){o="[Object]";try{o=JSON.stringify(i[s])}catch(f){}c.push(o)}else c.push(i[s]);if(t()){if(i.unshift(a),angular.isObject(console)&&angular.isFunction(console.log))switch(e){case"DEBUG":angular.isFunction(console.debug)?console.debug.apply(console,i):console.log.apply(console,i);break;case"INFO":angular.isFunction(console.debug)?console.info.apply(console,i):console.log.apply(console,i);break;case"WARN":angular.isFunction(console.debug)?console.warn.apply(console,i):console.log.apply(console,i);break;case"ERROR":angular.isFunction(console.debug)?console.error.apply(console,i):console.log.apply(console,i);break;default:console.log.apply(console,i)}}else console.log(c.join(" "));F.push({message:c.join(" ")+"\n"}),D||l()}function l(){if(!F.length)return void(D=!1);D=!0;var e=F.shift();c(e.message).then(function(){a(function(){l()})},function(){a(function(){l()})})}function c(a){var n=e.defer();if(t())o.localStorage[j]||(o.localStorage[j]=""),o.localStorage[j]+=a,n.resolve();else{if(!o.cordova||!o.cordova.file||!o.cordova.file.dataDirectory)return n.reject("cordova.file.dataDirectory is not available"),n.promise;r.checkFile(cordova.file.dataDirectory,j).then(function(){r.writeExistingFile(cordova.file.dataDirectory,j,a).then(function(){n.resolve()},function(e){n.reject(e)})},function(){r.writeFile(cordova.file.dataDirectory,j,a,!0).then(function(){n.resolve()},function(e){n.reject(e)})})}return n.promise}function s(){var a=e.defer();if(t())a.resolve(o.localStorage[j]);else{if(!o.cordova||!o.cordova.file||!o.cordova.file.dataDirectory)return a.reject("cordova.file.dataDirectory is not available"),a.promise;r.readAsText(cordova.file.dataDirectory,j).then(function(e){a.resolve(e)},function(e){a.reject(e)})}return a.promise}function f(){var a=e.defer();if(t())o.localStorage.removeItem(j),a.resolve();else{if(!o.cordova||!o.cordova.file||!o.cordova.file.dataDirectory)return a.reject("cordova.file.dataDirectory is not available"),a.promise;r.removeFile(cordova.file.dataDirectory,j).then(function(e){a.resolve(e)},function(e){a.reject(e)})}return a.promise}function u(e){return angular.isString(e)&&e.length>0?(j=e,!0):!1}function g(e,o){if(!angular.isUndefined(e)&&!angular.isString(e))throw new TypeError("format parameter must be a string or undefined");if(!angular.isUndefined(o)&&!angular.isString(o))throw new TypeError("timezone parameter must be a string or undefined");m=e,b=o}function d(){var a=e.defer();if(t())a.resolve({name:j,localURL:"localStorage://localhost/"+j,type:"text/plain",size:o.localStorage[j]?o.localStorage[j].length:0});else{if(!o.cordova||!o.cordova.file||!o.cordova.file.dataDirectory)return a.reject("cordova.file.dataDirectory is not available"),a.promise;r.checkFile(cordova.file.dataDirectory,j).then(function(e){e.file(a.resolve,a.reject)},a.reject)}return a.promise}function p(){var e=Array.prototype.slice.call(arguments,0);e.unshift("DEBUG"),i.apply(void 0,e)}function v(){var e=Array.prototype.slice.call(arguments,0);e.unshift("INFO"),i.apply(void 0,e)}function y(){var e=Array.prototype.slice.call(arguments,0);e.unshift("WARN"),i.apply(void 0,e)}function h(){var e=Array.prototype.slice.call(arguments,0);e.unshift("ERROR"),i.apply(void 0,e)}var m,b,F=[],D=!1,S=["DEBUG","INFO","WARN","ERROR"],j="messages.log";return{log:i,getLogfile:s,deleteLogfile:f,setStorageFilename:u,setTimestampFormat:g,checkFile:d,debug:p,info:v,warn:y,error:h}}])}();
\ No newline at end of file |
