summaryrefslogtreecommitdiffstats
path: root/lib/book.js
diff options
context:
space:
mode:
authorSamy Pessé <samypesse@gmail.com>2015-01-23 18:03:15 +0100
committerSamy Pessé <samypesse@gmail.com>2015-01-23 18:03:15 +0100
commit112782d11837428c260211bb3afeb3263e479593 (patch)
tree3288c6972ab6d769ccbf8aade56ef6188c1c1761 /lib/book.js
parentb88b4a0578c28af78a01a28591772f94d0dfe82e (diff)
downloadgitbook-112782d11837428c260211bb3afeb3263e479593.zip
gitbook-112782d11837428c260211bb3afeb3263e479593.tar.gz
gitbook-112782d11837428c260211bb3afeb3263e479593.tar.bz2
Add logs methods to Book object
Diffstat (limited to 'lib/book.js')
-rw-r--r--lib/book.js31
1 files changed, 30 insertions, 1 deletions
diff --git a/lib/book.js b/lib/book.js
index dcf0a33..4c1c33f 100644
--- a/lib/book.js
+++ b/lib/book.js
@@ -16,13 +16,18 @@ var Plugin = require("./plugin");
var generators = require("./generators");
+
+
var Book = function(root, context, parent) {
this.context = _.defaults(context || {}, {
// Extend book configuration
config: {},
// Log function
- log: console.log.bind(console)
+ log: console.log.bind(console),
+
+ // Log level
+ logLevel: Book.LOG_LEVELS.INFO
})
// Root folder of the book
@@ -73,6 +78,14 @@ var Book = function(root, context, parent) {
});
};
+Book.LOG_LEVELS = {
+ DEBUG: 0,
+ INFO: 1,
+ WARNING: 2,
+ ERROR: 3,
+ DISABLED: 10
+};
+
// Initialize and parse the book: config, summary, glossary
Book.prototype.parse = function() {
var that = this;
@@ -170,13 +183,16 @@ Book.prototype.generate = function(generator) {
if (!file) return;
if (file[file.length -1] == "/") {
+ that.logDebug("transferring folder", file);
return Q(generator.transferFolder(file));
} else if (_.contains(parsers.extensions, path.extname(file)) && that.navigation[file]) {
+ that.logDebug("parsing", file);
return that.parsePage(file)
.then(function(content) {
return Q(generator.writeParsedFile(content, file));
});
} else {
+ that.logDebug("transferring file", file);
return Q(generator.transferFile(file));
}
})
@@ -449,4 +465,17 @@ Book.prototype.indexPage = function(page) {
});
};
+// Log message
+Book.prototype.log = function(level) {
+ if (level < this.context.logLevel) return;
+
+ var args = Array.prototype.slice.apply(arguments);
+ args[0] = _.findKey(Book.LOG_LEVELS)+":";
+ this.context.log.apply(null, args);
+};
+Book.prototype.logDebug = _.partial(Book.prototype.log, Book.LOG_LEVELS.DEBUG);
+Book.prototype.logInfo = _.partial(Book.prototype.log, Book.LOG_LEVELS.INFO);
+Book.prototype.logWarn = _.partial(Book.prototype.log, Book.LOG_LEVELS.WARNING);
+Book.prototype.logError = _.partial(Book.prototype.log, Book.LOG_LEVELS.ERROR);
+
module.exports= Book;