loleaflet: log utility

It's used to log communication between the server and the client
It can print or download a csv formated file
This commit is contained in:
Mihai Varga 2015-07-16 13:55:53 +03:00
parent d5f9250bd8
commit 51592a4c53
2 changed files with 54 additions and 0 deletions

View file

@ -1,6 +1,7 @@
var deps = {
Core: {
src: ['Leaflet.js',
'core/Log.js',
'core/Util.js',
'core/Class.js',
'core/Events.js',

53
loleaflet/src/core/Log.js Normal file
View file

@ -0,0 +1,53 @@
/*
* L.Log contains methods for logging the activity
*/
L.Log = {
log: function (msg, direction, tileCoords) {
var time = Date.now();
if (!this._logs) {
this._logs = [];
}
msg = msg.replace(/(\r\n|\n|\r)/gm,' ');
this._logs.push({msg : msg, direction : direction,
coords : tileCoords, time : time});
},
_getEntries: function () {
this._logs.sort(function (a, b) {
if (a.time < b.time) { return -1; }
if (a.time > b.time) { return 1; }
return 0;
});
var data = '';
for (var i = 0; i < this._logs.length; i++) {
data += this._logs[i].time + '.' + this._logs[i].direction + '.' +
this._logs[i].msg + '.' + this._logs[i].coords;
data += '\n';
}
return data;
},
print: function () {
console.log(this._getEntries());
},
save: function () {
var blob = new Blob([this._getEntries()], {type: 'text/csv'}),
e = document.createEvent('MouseEvents'),
a = document.createElement('a');
a.download = Date.now() + '.csv';
a.href = window.URL.createObjectURL(blob);
a.dataset.downloadurl = ['text/csv', a.download, a.href].join(':');
e.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
a.dispatchEvent(e);
},
clear: function () {
this._logs = [];
}
};
L.INCOMING = 'INCOMING';
L.OUTGOING = 'OUTGOING';