From cac59a50411405f3059160732765d71784e1a539 Mon Sep 17 00:00:00 2001 From: Robert Krahn Date: Mon, 24 Feb 2014 14:50:30 -0800 Subject: [PATCH] logging moved into util, with debug option --- file-import-task.js | 4 ++-- package.json | 2 +- repository.js | 35 ++++++++++++++--------------------- util.js | 10 ++++++++-- 4 files changed, 25 insertions(+), 26 deletions(-) diff --git a/file-import-task.js b/file-import-task.js index 3025ab7..33a28bb 100644 --- a/file-import-task.js +++ b/file-import-task.js @@ -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); @@ -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); } } diff --git a/package.json b/package.json index 3edad44..e649dd7 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/repository.js b/repository.js index 443f5be..979676b 100644 --- a/repository.js +++ b/repository.js @@ -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 @@ -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 @@ -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); } }, @@ -95,7 +88,7 @@ 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) { @@ -103,7 +96,7 @@ util._extend(Repository.prototype, d.bindMethods({ 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'); } }); @@ -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++) @@ -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, @@ -180,12 +173,12 @@ 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); @@ -193,20 +186,20 @@ util._extend(Repository.prototype, d.bindMethods({ } 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; diff --git a/util.js b/util.js index ba26d2b..49cbca1 100644 --- a/util.js +++ b/util.js @@ -1,5 +1,6 @@ "use strict" +var debug = false; var util = require("util"); function batchify(list, constrainedFunc, context) { @@ -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')); } @@ -78,6 +79,10 @@ function curry(/*func, args*/) { } } +function log(/*args*/) { + if (debug) console.log.apply(console, arguments); +} + module.exports = { curry: curry, batchify: batchify, @@ -85,5 +90,6 @@ module.exports = { sum: sum, sumFileSize: sumFileSize, stringOrRegExp: stringOrRegExp, - humanReadableByteSize: humanReadableByteSize + humanReadableByteSize: humanReadableByteSize, + log: log }