From 319d4cb6670729708c19ad50b0146d1bcb7b4719 Mon Sep 17 00:00:00 2001 From: PliablePixels Date: Sat, 27 Jun 2015 09:52:06 -0400 Subject: Added ability to log key events to file and email (useful for release debugging) --- www/lib/filelogger/.bower.json | 40 +++++ www/lib/filelogger/LICENSE | 21 +++ www/lib/filelogger/README.md | 123 ++++++++++++++ www/lib/filelogger/bower.json | 30 ++++ www/lib/filelogger/dist/filelogger.js | 274 ++++++++++++++++++++++++++++++ www/lib/filelogger/dist/filelogger.min.js | 6 + www/lib/filelogger/package.json | 26 +++ 7 files changed, 520 insertions(+) create mode 100644 www/lib/filelogger/.bower.json create mode 100644 www/lib/filelogger/LICENSE create mode 100644 www/lib/filelogger/README.md create mode 100644 www/lib/filelogger/bower.json create mode 100644 www/lib/filelogger/dist/filelogger.js create mode 100644 www/lib/filelogger/dist/filelogger.min.js create mode 100644 www/lib/filelogger/package.json (limited to 'www/lib/filelogger') diff --git a/www/lib/filelogger/.bower.json b/www/lib/filelogger/.bower.json new file mode 100644 index 00000000..c063767b --- /dev/null +++ b/www/lib/filelogger/.bower.json @@ -0,0 +1,40 @@ +{ + "name": "filelogger", + "version": "1.1.0", + "homepage": "https://github.com/pbakondy/filelogger", + "authors": [ + "Peter Bakondy " + ], + "description": "Cordova library to log messages to local filesystem", + "main": "./dist/filelogger.min.js", + "ignore": [ + "**/.*", + "gulpfile.js", + "src", + "config" + ], + "dependencies": { + "ngCordova": ">= 0.1.14-alpha" + }, + "keywords": [ + "cordova", + "cordova plugin", + "ionic", + "ionic plugin", + "angular module", + "log", + "logger", + "file logger" + ], + "license": "MIT", + "_release": "1.1.0", + "_resolution": { + "type": "version", + "tag": "v1.1.0", + "commit": "4b5f765cf1737a51cc29bcaeca035d72eb6e8272" + }, + "_source": "git://github.com/pbakondy/filelogger.git", + "_target": "~1.1.0", + "_originalSource": "filelogger", + "_direct": true +} diff --git a/www/lib/filelogger/LICENSE b/www/lib/filelogger/LICENSE new file mode 100644 index 00000000..ec5a3987 --- /dev/null +++ b/www/lib/filelogger/LICENSE @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2015 Peter Bakondy + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. \ No newline at end of file diff --git a/www/lib/filelogger/README.md b/www/lib/filelogger/README.md new file mode 100644 index 00000000..1d6c6456 --- /dev/null +++ b/www/lib/filelogger/README.md @@ -0,0 +1,123 @@ +Cordova File Logger +========== + +[![Bower](http://img.shields.io/badge/bower-filelogger-FFCC2F.svg?style=flat)](http://bower.io/search/?q=filelogger) + +Logger module for Cordova/Ionic projects. + +When you run your application in device the Logger writes in the local filesystem (with cordova-plugin-file) and the system logs (with console.log). + +When you run your application in browser with „ionic serve” the Logger uses browsers localStorage and the browser console (with console.log). + +## Dependencies + +- [ngCordova](http://ngcordova.com/) ( required version v0.1.14-alpha ) +- [org.apache.cordova.file](https://github.com/apache/cordova-plugin-file) + +## Installation + +Install manually, or from bower: + +```bash +$ bower install filelogger +``` + +Include *filelogger.min.js* and ng-cordova.js or *ng-cordova.min.js* in your index.html file before cordova.js and after your AngularJS / Ionic file (since ngCordova depends on AngularJS). + +```html + + + +``` + +Comment: you don't have to use the complete ngCordova package. I suggest to create a [Custom Build](http://ngcordova.com/build/) with file module. + + +## Usage + +### $fileLogger.log() + +General logger method. The first parameter is the log level (debug, info, warn, error). The following parameters are the message parts. +You can put here any javascript type (string, number, boolean, object, array). + +In the logfile every item starts with the current UTC timestamp, followed by the log level and the message. + +### $fileLogger.debug() + +Wrapper for $fileLogger.log('debug', ...) + +### $fileLogger.info() + +Wrapper for $fileLogger.log('info', ...) + +### $fileLogger.warn() + +Wrapper for $fileLogger.log('warn', ...) + +### $fileLogger.error() + +Wrapper for $fileLogger.log('error', ...) + +### $fileLogger.setStorageFilename() + +You can set the local filename (default messages.log). It requests one parameter, the filename (type string). + +### $fileLogger.getLogfile() + +You can read the whole logfile from the filestore. This method returns a promise. + +### $fileLogger.deleteLogfile() + +You can delete the logfile from the filestore. This method returns a promise. + + + +### Example use + +```js +angular.module('starter', ['ionic', 'fileLogger']) + .controller('mainCtrl', ['$scope', '$fileLogger', function($scope, $fileLogger) { + + function testing() { + + $fileLogger.setStorageFilename('myLog.txt'); + + $fileLogger.log('debug', 'message'); + $fileLogger.log('info', 'message'); + $fileLogger.log('warn', 'message'); + $fileLogger.log('error', 'message'); + + $fileLogger.debug('message'); + $fileLogger.info('message'); + $fileLogger.warn('message'); + $fileLogger.error('message'); + + $fileLogger.log('error', 'error message', { code: 1, meaning: 'general' }); + + $fileLogger.log('info', 'message', 123, [1, 2, 3], { a: 1, b: '2' }); + + $fileLogger.getLogfile().then(function(l) { + console.log('Logfile content'); + console.log(l); + }); + + $fileLogger.deleteLogfile().then(function() { + console.log('Logfile deleted'); + }); + + } + +}]); +``` + + +## Author + +#### Peter Bakondy + +- https://github.com/pbakondy + + +## LICENSE + +Cordova File Logger is licensed under the MIT Open Source license. For more information, see the LICENSE file in this repository. diff --git a/www/lib/filelogger/bower.json b/www/lib/filelogger/bower.json new file mode 100644 index 00000000..d6a109bd --- /dev/null +++ b/www/lib/filelogger/bower.json @@ -0,0 +1,30 @@ +{ + "name": "filelogger", + "version": "1.1.0", + "homepage": "https://github.com/pbakondy/filelogger", + "authors": [ + "Peter Bakondy " + ], + "description": "Cordova library to log messages to local filesystem", + "main": "./dist/filelogger.min.js", + "ignore": [ + "**/.*", + "gulpfile.js", + "src", + "config" + ], + "dependencies": { + "ngCordova": ">= 0.1.14-alpha" + }, + "keywords": [ + "cordova", + "cordova plugin", + "ionic", + "ionic plugin", + "angular module", + "log", + "logger", + "file logger" + ], + "license": "MIT" +} 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;t0?(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}}])}(); diff --git a/www/lib/filelogger/package.json b/www/lib/filelogger/package.json new file mode 100644 index 00000000..0f1619a0 --- /dev/null +++ b/www/lib/filelogger/package.json @@ -0,0 +1,26 @@ +{ + "name": "filelogger", + "private": false, + "main": "dist/filelogger", + "version": "1.1.0", + "repository": { + "url": "git://github.com/pbakondy/filelogger.git" + }, + "devDependencies": { + "gulp": "^3.7.0", + "gulp-footer": "^1.0.4", + "gulp-header": "^1.0.2", + "gulp-jshint": "^1.6.1", + "gulp-rename": "^1.2.0", + "gulp-uglify": "^0.2.1", + "jshint-stylish": "^0.4.0", + "minimist": "^0.1.0" + }, + "licenses": [ + { + "type": "MIT" + } + ], + "dependencies": { + } +} -- cgit v1.2.3