Skip to content

Commit

Permalink
Get storage file data
Browse files Browse the repository at this point in the history
  • Loading branch information
pbakondy committed Jun 30, 2015
1 parent 8b1f2ca commit 34d8cc5
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 22 deletions.
77 changes: 62 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,30 +41,30 @@ Comment: you don't have to use the complete ngCordova package. I suggest to crea

## Usage

### $fileLogger.log()
### $fileLogger.log(level, ...message)

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()
### $fileLogger.debug(...message)

Wrapper for $fileLogger.log('debug', ...)

### $fileLogger.info()
### $fileLogger.info(...message)

Wrapper for $fileLogger.log('info', ...)

### $fileLogger.warn()
### $fileLogger.warn(...message)

Wrapper for $fileLogger.log('warn', ...)

### $fileLogger.error()
### $fileLogger.error(...message)

Wrapper for $fileLogger.log('error', ...)

### $fileLogger.setStorageFilename()
### $fileLogger.setStorageFilename(filename)

You can set the local filename (default messages.log). It requests one parameter, the filename (type string).

Expand All @@ -76,13 +76,49 @@ You can read the whole logfile from the filestore. This method returns a promise

You can delete the logfile from the filestore. This method returns a promise.

### $fileLogger.checkFile()

Get storage file data. This method returns an object.

```js
// response on iOS
{
"name": "myLog.txt",
"localURL": "cdvfile://localhost/library-nosync/myLog.txt",
"type": null,
"lastModified": 1435668606000,
"lastModifiedDate": 1435668606000,
"size": 450,
"start": 0,
"end": 450
}

// response on Android
{
"name": "myLog.txt",
"localURL": "cdvfile://localhost/files/myLog.txt",
"type": "text/plain",
"lastModified": 1435669292000,
"lastModifiedDate": 1435669292000,
"size": 450,
"start": 0,
"end": 450
}

// response in Browser
{
"name": "myLog.txt",
"localURL": "localStorage://localhost/myLog.txt",
"type": "text/plain",
"size": 450
}
```

### Example use

```js
angular.module('starter', ['ionic', 'fileLogger'])
.controller('mainCtrl', ['$scope', '$fileLogger', function($scope, $fileLogger) {
.controller('mainCtrl', ['$scope', '$fileLogger', '$timeout', function($scope, $fileLogger, $timeout) {

function testing() {

Expand All @@ -102,14 +138,25 @@ angular.module('starter', ['ionic', 'fileLogger'])

$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');
});
$timeout(function(){
$fileLogger.getLogfile().then(function(l) {
console.log('Logfile content');
console.log(l);
});
}, 1000);

$timeout(function(){
$fileLogger.checkFile().then(function(d) {
console.log('Logfile data');
console.log(JSON.stringify(d));
});
}, 2000);

$timeout(function(){
$fileLogger.deleteLogfile().then(function() {
console.log('Logfile deleted');
});
}, 3000);

}

Expand Down
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "filelogger",
"version": "1.1.1",
"version": "1.2.0",
"homepage": "https://github.com/pbakondy/filelogger",
"authors": [
"Peter Bakondy <[email protected]>"
Expand Down
30 changes: 28 additions & 2 deletions dist/filelogger.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
(function(){
/* global angular, console, cordova */

// install : cordova plugin add org.apache.cordova.file
// install : cordova plugin add cordova-plugin-file

angular.module('fileLogger', ['ngCordova.plugins.file'])

.factory('$fileLogger', ['$q', '$window', '$cordovaFile', '$timeout', function ($q, $window, $cordovaFile, $timeout) {
.factory('$fileLogger', ['$q', '$window', '$cordovaFile', '$timeout',
function ($q, $window, $cordovaFile, $timeout) {

'use strict';


Expand Down Expand Up @@ -241,6 +243,29 @@ angular.module('fileLogger', ['ngCordova.plugins.file'])
}


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 {

$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');
Expand Down Expand Up @@ -274,6 +299,7 @@ angular.module('fileLogger', ['ngCordova.plugins.file'])
getLogfile: getLogfile,
deleteLogfile: deleteLogfile,
setStorageFilename: setStorageFilename,
checkFile: checkFile,
debug: debug,
info: info,
warn: warn,
Expand Down
2 changes: 1 addition & 1 deletion dist/filelogger.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "filelogger",
"private": false,
"main": "dist/filelogger",
"version": "1.1.1",
"version": "1.2.0",
"repository": {
"url": "git://github.com/pbakondy/filelogger.git"
},
Expand Down
30 changes: 28 additions & 2 deletions src/filelogger.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
/* global angular, console, cordova */

// install : cordova plugin add org.apache.cordova.file
// install : cordova plugin add cordova-plugin-file

angular.module('fileLogger', ['ngCordova.plugins.file'])

.factory('$fileLogger', ['$q', '$window', '$cordovaFile', '$timeout', function ($q, $window, $cordovaFile, $timeout) {
.factory('$fileLogger', ['$q', '$window', '$cordovaFile', '$timeout',
function ($q, $window, $cordovaFile, $timeout) {

'use strict';


Expand Down Expand Up @@ -235,6 +237,29 @@ angular.module('fileLogger', ['ngCordova.plugins.file'])
}


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 {

$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');
Expand Down Expand Up @@ -268,6 +293,7 @@ angular.module('fileLogger', ['ngCordova.plugins.file'])
getLogfile: getLogfile,
deleteLogfile: deleteLogfile,
setStorageFilename: setStorageFilename,
checkFile: checkFile,
debug: debug,
info: info,
warn: warn,
Expand Down

0 comments on commit 34d8cc5

Please sign in to comment.