diff options
| author | PliablePixels <pliablepixels@gmail.com> | 2015-06-27 09:52:06 -0400 |
|---|---|---|
| committer | PliablePixels <pliablepixels@gmail.com> | 2015-06-27 09:52:06 -0400 |
| commit | 319d4cb6670729708c19ad50b0146d1bcb7b4719 (patch) | |
| tree | c61e9723a1fd217b1816c987bba66e470e73bf02 /www/lib/filelogger/dist | |
| parent | fdc42fae48db0fef5fbdc9ef51a27d219aea3a72 (diff) | |
Added ability to log key events to file and email (useful for release debugging)
Diffstat (limited to 'www/lib/filelogger/dist')
| -rw-r--r-- | www/lib/filelogger/dist/filelogger.js | 274 | ||||
| -rw-r--r-- | www/lib/filelogger/dist/filelogger.min.js | 6 |
2 files changed, 280 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..c266f22a --- /dev/null +++ b/www/lib/filelogger/dist/filelogger.js @@ -0,0 +1,274 @@ +/*! + * fileLogger + * Copyright 2015 Peter Bakondy https://github.com/pbakondy + * See LICENSE in this repository for license information + */ +(function(){ +/* global angular, console, cordova */ + +// install : cordova plugin add org.apache.cordova.file + +angular.module('fileLogger', ['ngCordova.plugins.file']) + + .factory('$fileLogger', ['$q', '$window', '$cordovaFile', '$timeout', function ($q, $window, $cordovaFile, $timeout) { + 'use strict'; + + + var queue = []; + var ongoing = false; + var levels = ['DEBUG', 'INFO', 'WARN', 'ERROR']; + + var storageFilename = 'messages.log'; + + + 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 timestamp = (new Date()).toJSON(); + + var messages = Array.prototype.slice.call(arguments, 1); + var message = [ timestamp, level ]; + + for (var i = 0; i < messages.length; i++ ) { + if (angular.isArray(messages[i])) { + message.push(JSON.stringify(messages[i])); + } + else if (angular.isObject(messages[i])) { + message.push(JSON.stringify(messages[i])); + } + 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 { + + $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 { + $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 { + $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 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, + debug: debug, + info: info, + warn: warn, + error: error + }; + + }]); + +})(); diff --git a/www/lib/filelogger/dist/filelogger.min.js b/www/lib/filelogger/dist/filelogger.min.js new file mode 100644 index 00000000..366521a2 --- /dev/null +++ b/www/lib/filelogger/dist/filelogger.min.js @@ -0,0 +1,6 @@ +/*! + * fileLogger + * Copyright 2015 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",function(e,o,n,r){"use strict";function l(){return!o.cordova&&!o.PhoneGap&&!o.phonegap}function t(e){angular.isString(e)?(e=e.toUpperCase(),-1===h.indexOf(e)&&(e="INFO")):e="INFO";for(var o=(new Date).toJSON(),n=Array.prototype.slice.call(arguments,1),r=[o,e],t=0;t<n.length;t++)r.push(angular.isArray(n[t])?JSON.stringify(n[t]):angular.isObject(n[t])?JSON.stringify(n[t]):n[t]);if(l()){if(n.unshift(o),angular.isObject(console)&&angular.isFunction(console.log))switch(e){case"DEBUG":angular.isFunction(console.debug)?console.debug.apply(console,n):console.log.apply(console,n);break;case"INFO":angular.isFunction(console.debug)?console.info.apply(console,n):console.log.apply(console,n);break;case"WARN":angular.isFunction(console.debug)?console.warn.apply(console,n):console.log.apply(console,n);break;case"ERROR":angular.isFunction(console.debug)?console.error.apply(console,n):console.log.apply(console,n);break;default:console.log.apply(console,n)}}else console.log(r.join(" "));v.push({message:r.join(" ")+"\n"}),y||a()}function a(){if(!v.length)return void(y=!1);y=!0;var e=v.shift();i(e.message).then(function(){r(function(){a()})},function(){r(function(){a()})})}function i(r){var t=e.defer();return l()?(o.localStorage[m]||(o.localStorage[m]=""),o.localStorage[m]+=r,t.resolve()):n.checkFile(cordova.file.dataDirectory,m).then(function(){n.writeExistingFile(cordova.file.dataDirectory,m,r).then(function(){t.resolve()},function(e){t.reject(e)})},function(){n.writeFile(cordova.file.dataDirectory,m,r,!0).then(function(){t.resolve()},function(e){t.reject(e)})}),t.promise}function c(){var r=e.defer();return l()?r.resolve(o.localStorage[m]):n.readAsText(cordova.file.dataDirectory,m).then(function(e){r.resolve(e)},function(e){r.reject(e)}),r.promise}function s(){var r=e.defer();return l()?(o.localStorage.removeItem(m),r.resolve()):n.removeFile(cordova.file.dataDirectory,m).then(function(e){r.resolve(e)},function(e){r.reject(e)}),r.promise}function u(e){return angular.isString(e)&&e.length>0?(m=e,!0):!1}function g(){var e=Array.prototype.slice.call(arguments,0);e.unshift("DEBUG"),t.apply(void 0,e)}function f(){var e=Array.prototype.slice.call(arguments,0);e.unshift("INFO"),t.apply(void 0,e)}function p(){var e=Array.prototype.slice.call(arguments,0);e.unshift("WARN"),t.apply(void 0,e)}function d(){var e=Array.prototype.slice.call(arguments,0);e.unshift("ERROR"),t.apply(void 0,e)}var v=[],y=!1,h=["DEBUG","INFO","WARN","ERROR"],m="messages.log";return{log:t,getLogfile:c,deleteLogfile:s,setStorageFilename:u,debug:g,info:f,warn:p,error:d}}])}(); |
