From 98a8daa27f6cb0b548c64556aa7ea5645db94c61 Mon Sep 17 00:00:00 2001 From: Peter Bakondy Date: Sat, 21 Feb 2015 18:58:17 +0100 Subject: [PATCH] include original file to package --- bower.json | 2 +- dist/filelogger.js | 213 +++++++++++++++++++++++++++++++++++++++++++++ gulpfile.js | 6 ++ package.json | 4 +- 4 files changed, 222 insertions(+), 3 deletions(-) create mode 100644 dist/filelogger.js diff --git a/bower.json b/bower.json index e7c6734..6eec1f5 100644 --- a/bower.json +++ b/bower.json @@ -1,6 +1,6 @@ { "name": "filelogger", - "version": "1.0.2", + "version": "1.0.3", "homepage": "https://github.com/pbakondy/filelogger", "authors": [ "Peter Bakondy " diff --git a/dist/filelogger.js b/dist/filelogger.js new file mode 100644 index 0000000..f0e5624 --- /dev/null +++ b/dist/filelogger.js @@ -0,0 +1,213 @@ +/*! + * fileLogger + * Copyright 2015 Peter Bakondy https://github.com/pbakondy + * See LICENSE in this repository for license information + */ +(function(){ +/* global angular, console */ + +// install : cordova plugin add org.apache.cordova.file + +angular.module('fileLogger', ['ngCordova']) + + .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 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]); + } + } + + 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); + } + } + + 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 (!$window.cordova) { + // running in browser with 'ionic serve' + + if (!$window.localStorage[storageFilename]) { + $window.localStorage[storageFilename] = ''; + } + + $window.localStorage[storageFilename] += message; + q.resolve(); + + } else { + + $cordovaFile.writeFile(storageFilename, message, { append: true }).then( + function() { + q.resolve(); + }, + function(error) { + q.reject(error); + } + ); + + } + + return q.promise; + } + + + function getLogfile() { + var q = $q.defer(); + + if (!$window.cordova) { + q.resolve($window.localStorage[storageFilename]); + } else { + $cordovaFile.readAsText(storageFilename).then( + function(result) { + q.resolve(result); + }, + function(error) { + q.reject(error); + } + ); + } + + return q.promise; + } + + + function deleteLogfile() { + var q = $q.defer(); + + if (!$window.cordova) { + $window.localStorage.removeItem(storageFilename); + q.resolve(); + } else { + $cordovaFile.removeFile(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; + } + } + + + return { + log: log, + getLogfile: getLogfile, + deleteLogfile: deleteLogfile, + setStorageFilename: setStorageFilename + }; + + }]); + +})(); \ No newline at end of file diff --git a/gulpfile.js b/gulpfile.js index e3772a1..5084d54 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -9,6 +9,12 @@ var gulp = require('gulp'), gulp.task('default', ['build']); gulp.task('build', function () { + gulp.src(buildConfig.pluginFiles) + .pipe(header(buildConfig.closureStart)) + .pipe(footer(buildConfig.closureEnd)) + .pipe(header(buildConfig.banner)) + .pipe(gulp.dest(buildConfig.dist)); + return gulp.src(buildConfig.pluginFiles) .pipe(header(buildConfig.closureStart)) .pipe(footer(buildConfig.closureEnd)) diff --git a/package.json b/package.json index b31774e..27779dd 100644 --- a/package.json +++ b/package.json @@ -1,8 +1,8 @@ { - "name": "Cordova File Logger", + "name": "filelogger", "private": false, "main": "dist/filelogger", - "version": "1.0.2", + "version": "1.0.3", "repository": { "url": "git://github.com/pbakondy/filelogger.git" },