Skip to content

Commit

Permalink
logging moved into util, with debug option
Browse files Browse the repository at this point in the history
  • Loading branch information
rksm committed Feb 24, 2014
1 parent ab7d3d2 commit cac59a5
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 26 deletions.
4 changes: 2 additions & 2 deletions file-import-task.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ var d = require('./domain');
* as a new version.
*
*/

function processFile(lvfs, fi, thenDo) {
var rootDir = lvfs.getRootDirectory();
fs.readFile(path.join(rootDir, fi.path), handleReadResult);
Expand All @@ -26,7 +26,7 @@ function processFile(lvfs, fi, thenDo) {
content: content
}, thenDo);
} else {
console.log('error reading file %s:', fi.path, err);
console.error('error reading file %s:', fi.path, err);
thenDo(err);
}
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "lively-davfs",
"version": "0.1.5",
"version": "0.1.6",
"description": "Manager for versioning and reading / writing resources.",
"main": "request-handler.js",
"repository": {
Expand Down
35 changes: 14 additions & 21 deletions repository.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,7 @@ var EventEmitter = require("events").EventEmitter;
var livelyDAVPlugin = require('./jsDAV-plugin');
var VersionedFileSystem = require('./VersionedFileSystem');
var d = require('./domain');

// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// debugging
global.dir = function(obj, depth) {
console.log(util.inspect(obj, {depth: depth || 0}));
}
var log = require('./util').log;

// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// Repo
Expand All @@ -30,9 +25,7 @@ util._extend(Repository.prototype, d.bindMethods({
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// intialize-release
initialize: function(options) {
if (global.lively) {
lively.repository = this;
}
if (global.lively) lively.repository = this;
this.fs = new VersionedFileSystem(options);
// we keep a queue for changes b/c they should be committed to the
// versioned file system in their incoming order. Before they can be
Expand Down Expand Up @@ -77,12 +70,12 @@ util._extend(Repository.prototype, d.bindMethods({
var timeToWorry = 60*1000;
this.pendingChangeQueue.forEach(function(change) {
if (Date.now() - change.startTime < timeToWorry) return;
if (!change.statRead) console.log('Change for %s has no file stat', change.record.path);
if (!change.requestDataRead) console.log('Change for %s has no content', change.record.path);
if (!change.statRead) console.warn('Change for %s has no file stat', change.record.path);
if (!change.requestDataRead) console.warn('Change for %s has no content', change.record.path);
});
var change = this.pendingChangeQueue[0];
if (Date.now() - change.startTime > timeToWorry) {
console.log('Took too long to process change for %s, discarding it', change.record.path);
console.warn('Took too long to process change for %s, discarding it', change.record.path);
this.discardPendingChange(change);
}
},
Expand All @@ -95,15 +88,15 @@ util._extend(Repository.prototype, d.bindMethods({
if (!q[i].canBeCommitted()) break;
toCommit.push(q[i].record);
}
console.log("Commiting %s changes to DB", toCommit.length);
log("Commiting %s changes to DB", toCommit.length);
if (!toCommit.length) return;
repo.pendingChangeQueue.splice(0, toCommit.length);
repo.fs.addVersions(toCommit, {}, function(err, version) {
if (err) {
console.error('error in addVersions for records ', toCommit);
}
if (!repo.pendingChangeQueue.length) {
console.log("all pending changes processed");
log("all pending changes processed");
repo.emit('synchronized');
}
});
Expand All @@ -117,7 +110,7 @@ util._extend(Repository.prototype, d.bindMethods({
},

onAfterWrite: function(evt) {
console.log('after write: ', evt.uri);
log('after write: ', evt.uri);
if (!evt.uri) return;
var q = this.pendingChangeQueue, change;
for (var i = 0; i < q.length; i++)
Expand All @@ -127,7 +120,7 @@ util._extend(Repository.prototype, d.bindMethods({
},

captureDAVEvt: function(changeType, readBody, readStat, evt) {
if (!evt.uri) { console.log('Error recording file change, no path', evt); return; }
if (!evt.uri) { console.error('Error recording file change, no path', evt); return; }
var taskData = {
record: {
version: undefined,
Expand Down Expand Up @@ -180,33 +173,33 @@ util._extend(Repository.prototype, d.bindMethods({
if (change.requestDataRead) { this.commitPendingChanges(); return; }
var timeout = 60*1000, ts = Date.now();
if (ts-change.startTime > timeout) {
console.log("reading content for %s timed out", change.record.path);
console.warn("reading content for %s timed out", change.record.path);
change.requestDataRead = true;
this.commitPendingChanges();
return;
}
console.log("waiting for content of %s", change.record.path);
log("waiting for content of %s", change.record.path);
if (!change.incomingContent.isDone) {
setTimeout(this.startReadingRequestContent.bind(
this, change), 500);
return;
}
change.record.content = change.incomingContent.buffer.toString();
change.requestDataRead = true;
console.log("content for %s read", change.record.path);
log("content for %s read", change.record.path);
repo.commitPendingChanges();
},

readFileStat: function(change) {
var repo = this;
console.log("start reading file stat for %s", change.record.path);
log("start reading file stat for %s", change.record.path);
fs.stat(path.join(repo.getRootDirectory(), change.record.path), function(err, stat) {
if (err || !stat) {
console.error('readFileStat: ', err);
repo.discardPendingChange(change);
return;
}
console.log("file stat for %s read", change.record.path, stat);
log("file stat for %s read", change.record.path, stat);
change.record.stat = stat;
change.record.date = stat.mtime.toISOString();
change.statRead = true;
Expand Down
10 changes: 8 additions & 2 deletions util.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"use strict"

var debug = false;
var util = require("util");

function batchify(list, constrainedFunc, context) {
Expand Down Expand Up @@ -34,7 +35,7 @@ function batchify(list, constrainedFunc, context) {

function sum(arr) { return arr.reduce(function(sum,ea) { return sum+ea; },0); }

function sumFileSize(objsWithFilestats) {
function sumFileSize(objsWithFilestats) {
/**/
return sum(pluck(pluck(objsWithFilestats, 'stat'), 'size'));
}
Expand Down Expand Up @@ -78,12 +79,17 @@ function curry(/*func, args*/) {
}
}

function log(/*args*/) {
if (debug) console.log.apply(console, arguments);
}

module.exports = {
curry: curry,
batchify: batchify,
pluck: pluck,
sum: sum,
sumFileSize: sumFileSize,
stringOrRegExp: stringOrRegExp,
humanReadableByteSize: humanReadableByteSize
humanReadableByteSize: humanReadableByteSize,
log: log
}

0 comments on commit cac59a5

Please sign in to comment.