Permalink
Browse files

[api] Improving debug messages

  • Loading branch information...
1 parent 33bc6b9 commit 340aa8e5a8c04206b726654f24d5729436ae0c4c @Marak Marak committed Jan 1, 2012
Showing with 43 additions and 24 deletions.
  1. +43 −24 lib/hnet.js
View
@@ -3,9 +3,9 @@ var fs = require('fs'),
EventEmitter = require('EventEmitter2').EventEmitter2,
util = require('util'),
async = require('async'),
+ colors = require('colors'),
hnet = exports;
-
var Hnet = exports.Hnet = function (options) {
var self = this;
@@ -42,7 +42,7 @@ var Hnet = exports.Hnet = function (options) {
// TODO: upgrade EE2 to version that has "**"
//
self.on('*::*', function(data){
- console.log('debug:', this.event, data);
+ console.log(pad(this.event.magenta, 25) + ' ' + JSON.stringify(data).grey);
});
//
@@ -135,31 +135,30 @@ Hnet.prototype.parseDocument = function (node, doc, options) {
function _load () {
self.load(d.params, { currentDepth: options.currentDepth + 1, maxDepth: self.maxDepth });
}
-
- if (self.ttl) {
- console.log('setting timeout');
+ if (self.ttl > 0) {
+ self.emit('node::ttl', self.ttl)
setTimeout(function(){
_load();
}, self.ttl)
} else {
- console.log('NOT setting timeout');
_load();
}
}
}
+
+ //
+ // Determine if we've reached the capped size, if so, shift off some records,
+ // we dont need them
+ //
+ if(self.store.length >= self.MAXRECORDS) {
+ self.store.shift();
+ }
+ self.store.push(doc);
+
});
}
- doc.forEach(function(record){
- //
- // Determine if we've reached the capped size, if so, shift off some records,
- // we dont need them
- //
- if(self.store.length >= self.MAXRECORDS) {
- self.store.shift();
- }
- self.store.push(record);
- });
+
}
//
@@ -197,7 +196,7 @@ Hnet.prototype.load = function(nodes, options, cb){
}
if (options.currentDepth >= self.maxDepth) {
- console.log('Will not load: ' + JSON.stringify(nodes) + ' Reached maximum depth of: ' + self.maxDepth);
+ self.emit('warn::maxDepth', 'Reached maximum depth of: ' + self.maxDepth)
return false;
}
@@ -236,7 +235,8 @@ Hnet.prototype.load = function(nodes, options, cb){
}
if(self.nodes[node.uri] && self.ttl < 0){
- return cb(new Error('Already loaded ' + node.uri + ' Will not re-load circular dependency'));
+ self.emit('warn::circular', 'No ttl detected. Will not re-load circular dependency: ' + node.uri);
+ return;
}
function loadSet () {
@@ -257,7 +257,7 @@ Hnet.prototype.load = function(nodes, options, cb){
if (err) {
// TODO: do something meaningful with this error state
- console.log(err.message);
+ console.log(err);
return;
}
@@ -299,7 +299,7 @@ Hnet.prototype.save = function(cb){
// Remark: If we have no nodes at all, we'll just create a new one using hnet.iriscouch.com
//
if (_keys.length === 0) {
- console.log('no known nodes, using default');
+ self.emit('warn::no-nodes', 'no known nodes provided, using default')
backLinks.push({uri: 'http://hnet.iriscouch.com/public/0', type: 'couchdb'});
} else if (_keys.length === 1) {
//
@@ -316,8 +316,11 @@ Hnet.prototype.save = function(cb){
backLinks.push(_link(last));
}
-
- dataSet = self.store;
+
+ dataSet = [];
+ self.store.forEach(function(record){
+ dataSet.push(record);
+ });
backLinks.forEach(function(savePoint){
dataSet.push(savePoint);
});
@@ -339,7 +342,7 @@ Hnet.prototype.save = function(cb){
self.nodes['http://hnet.iriscouch.com/public/' + body.id];
}
self.emit('node::added', 'http://hnet.iriscouch.com/public/' + body.id)
- cb(null, body);
+ cb(null, response, body);
});
}
});
@@ -351,4 +354,20 @@ Hnet.prototype.save = function(cb){
//
function _link(node) {
return { "method": "link", "params": [node] };
-}
+}
+
+function pad (str, len, chr) {
+ var s;
+
+ if (!chr) {
+ chr = ' ';
+ }
+
+ s = str;
+ if (str.length < len) {
+ for (var i = 0; i < (len - str.length); i++) {
+ s += chr;
+ }
+ }
+ return s;
+}

0 comments on commit 340aa8e

Please sign in to comment.