diff options
author | Samy Pessé <samypesse@gmail.com> | 2015-01-25 14:55:37 +0100 |
---|---|---|
committer | Samy Pessé <samypesse@gmail.com> | 2015-01-25 14:55:37 +0100 |
commit | 06ab84abe33a131c089f42bf0bd9ede8deb6e671 (patch) | |
tree | 2b011ec994ad3e085fd7e8db741294f8c67b09a0 /lib/book.js | |
parent | bb5761e88548f607e3b0dc6b8e03c652c93a9511 (diff) | |
download | gitbook-06ab84abe33a131c089f42bf0bd9ede8deb6e671.zip gitbook-06ab84abe33a131c089f42bf0bd9ede8deb6e671.tar.gz gitbook-06ab84abe33a131c089f42bf0bd9ede8deb6e671.tar.bz2 |
Improve logger module
Diffstat (limited to 'lib/book.js')
-rw-r--r-- | lib/book.js | 89 |
1 files changed, 43 insertions, 46 deletions
diff --git a/lib/book.js b/lib/book.js index 1f5c5c4..2e0ec19 100644 --- a/lib/book.js +++ b/lib/book.js @@ -1,6 +1,7 @@ var Q = require("q"); var _ = require("lodash"); var path = require("path"); +var util = require("util"); var lunr = require('lunr'); var parsers = require("gitbook-parsers"); var color = require('bash-color'); @@ -10,6 +11,7 @@ var parseNavigation = require("./utils/navigation"); var parseProgress = require("./utils/progress"); var pageUtil = require("./utils/page"); var links = require("./utils/links"); +var logger = require("./utils/logger"); var Configuration = require("./configuration"); var TemplateEngine = require("./template"); @@ -23,11 +25,16 @@ var Book = function(root, context, parent) { config: {}, // Log function - log: console.log.bind(console), + log: function(msg) { + process.stdout.write(msg); + }, // Log level logLevel: Book.LOG_LEVELS.INFO - }) + }); + + // Log + this.log = logger(this.context.log, this.context.logLevel); // Root folder of the book this.root = path.resolve(root); @@ -101,7 +108,7 @@ Book.prototype.parse = function() { var multilingual = false; - that.logDebug("start parsing configuration"); + that.log.debug.ln("start parsing configuration"); return this.config.load() .then(function() { @@ -112,11 +119,11 @@ Book.prototype.parse = function() { return that.parseLangs() .then(function() { multilingual = that.langs.length > 0; - if (multilingual) that.logInfo("Parsing multilingual book, with", that.langs.length, "lanuages"); + if (multilingual) that.log.info.ln("Parsing multilingual book, with", that.langs.length, "lanuages"); // Sub-books that inherit from the current book configuration that.books = _.map(that.langs, function(lang) { - that.logInfo("Preparing language book", lang.lang); + that.log.info.ln("Preparing language book", lang.lang); return new Book( path.join(that.root, lang.path), _.merge({}, that.context, { @@ -165,7 +172,7 @@ Book.prototype.generate = function(generator) { var that = this; that.options.generator = generator || that.options.generator; - that.logInfo("start generation with", that.options.generator, "generator"); + that.log.info.ln("start generation with", that.options.generator, "generator"); return Q() // Clean output folder @@ -197,16 +204,16 @@ Book.prototype.generate = function(generator) { if (!file) return; if (file[file.length -1] == "/") { - that.logDebug("transferring folder", file); + that.log.debug.ln("transferring folder", file); return Q(generator.transferFolder(file)); } else if (_.contains(parsers.extensions, path.extname(file)) && that.navigation[file]) { - that.logDebug("parsing", file); + that.log.debug.ln("parsing", file); return that.parsePage(file) .then(function(content) { return Q(generator.writeParsedFile(content, file)); }); } else { - that.logDebug("transferring file", file); + that.log.debug.ln("transferring file", file); return Q(generator.transferFile(file)); } }) @@ -226,7 +233,7 @@ Book.prototype.generate = function(generator) { return generator.callHook("finish"); }) .then(function() { - that.logInfo("generation is finished"); + that.log.info.ln("generation is finished"); }); }; @@ -274,11 +281,14 @@ Book.prototype.generateFile = function(output, options) { _tmpDir = path.join(_tmpDir, lang); } - book.logInfo("copy ebook to", output); + book.log.info("copy ebook to", output, "..."); return fs.copy( path.join(_tmpDir, "index."+options.ebookFormat), _outputFile - ); + ) + .then(function() { + book.log.info.ok(); + }); }; // Multi-langs book @@ -309,16 +319,18 @@ Book.prototype.parsePlugins = function() { // Load plugins that.plugins = _.map(that.options.plugins, function(plugin) { var plugin = new Plugin(that, plugin.name); + that.log.info("Load plugin", plugin.name, "...."); + if (!plugin.isValid()) { - that.logError("Failed to load plugin", plugin.name); + that.log.info.fail(); failed.push(plugin.name); } else { - that.logInfo("Load plugin", plugin.name); + that.log.info.ok(); } return plugin; }); - that.logDebug("load plugins:", that.plugins.length, "loaded, ", failed.length, "failed"); + that.log.debug.ln("load plugins:", that.plugins.length, "loaded, ", failed.length, "failed"); if (_.size(failed) > 0) return Q.reject(new Error("Error loading plugins: "+failed.join(",")+". Run 'gitbook install' to install plugins from NPM.")); return Q(); }; @@ -327,7 +339,7 @@ Book.prototype.parsePlugins = function() { Book.prototype.parseReadme = function() { var that = this; var structure = that.config.getStructure("readme"); - that.logDebug("start parsing readme:", structure); + that.log.debug.ln("start parsing readme:", structure); return that.findFile(structure) .then(function(readme) { @@ -337,7 +349,7 @@ Book.prototype.parseReadme = function() { that.readmeFile = readme.path; that._defaultsStructure(that.readmeFile); - that.logDebug("readme located at", that.readmeFile); + that.log.debug.ln("readme located at", that.readmeFile); return that.template.renderFile(that.readmeFile) .then(function(content) { return readme.parser.readme(content); @@ -355,7 +367,7 @@ Book.prototype.parseLangs = function() { var that = this; var structure = that.config.getStructure("langs"); - that.logDebug("start parsing languages index:", structure); + that.log.debug.ln("start parsing languages index:", structure); return that.findFile(structure) .then(function(langs) { @@ -364,7 +376,7 @@ Book.prototype.parseLangs = function() { that.langsFile = langs.path; that._defaultsStructure(that.langsFile); - that.logDebug("languages index located at", that.langsFile); + that.log.debug.ln("languages index located at", that.langsFile); return that.template.renderFile(that.langsFile) .then(function(content) { return langs.parser.langs(content); @@ -380,7 +392,7 @@ Book.prototype.parseSummary = function() { var that = this; var structure = that.config.getStructure("summary"); - that.logDebug("start parsing summary:", structure); + that.log.debug.ln("start parsing summary:", structure); return that.findFile(structure) .then(function(summary) { @@ -391,7 +403,7 @@ Book.prototype.parseSummary = function() { that._defaultsStructure(that.summaryFile); that.files = _.without(that.files, that.summaryFile); - that.logDebug("summary located at", that.summaryFile); + that.log.debug.ln("summary located at", that.summaryFile); return that.template.renderFile(that.summaryFile) .then(function(content) { return summary.parser.summary(content, { @@ -411,7 +423,7 @@ Book.prototype.parseGlossary = function() { var that = this; var structure = that.config.getStructure("glossary"); - that.logDebug("start parsing glossary: ", structure); + that.log.debug.ln("start parsing glossary: ", structure); return that.findFile(structure) .then(function(glossary) { @@ -422,7 +434,7 @@ Book.prototype.parseGlossary = function() { that._defaultsStructure(that.glossaryFile); that.files = _.without(that.files, that.glossaryFile); - that.logDebug("glossary located at", that.glossaryFile); + that.log.debug.ln("glossary located at", that.glossaryFile); return that.template.renderFile(that.glossaryFile) .then(function(content) { return glossary.parser.glossary(content); @@ -437,17 +449,17 @@ Book.prototype.parseGlossary = function() { Book.prototype.parsePage = function(filename) { var that = this; - that.logDebug("start parsing file", filename); + that.log.debug.ln("start parsing file", filename); var extension = path.extname(filename); var filetype = parsers.get(extension); if (!filetype) return Q.reject(new Error("Can't parse file: "+filename)); - that.logDebug("render template", filename); + that.log.debug.ln("render template", filename); return that.template.renderFile(filename) .then(function(content) { - that.logDebug("use file parser", filetype.name, "for", filename); + that.log.debug.ln("use file parser", filetype.name, "for", filename); return filetype.parser.page(content); }) .then(function(page) { @@ -562,7 +574,7 @@ Book.prototype.indexPage = function(page) { var nav = this.navigation[page.path]; if (!nav) return; - this.logDebug("index page", page.path); + this.log.debug.ln("index page", page.path); this.searchIndex.add({ url: this.contentLink(page.path), title: nav.title, @@ -581,21 +593,6 @@ Book.prototype._defaultsStructure = function(filename) { that.langsFile = that.langsFile || that.config.getStructure("langs")+extension; } -// Log message -Book.prototype.log = function(level) { - if (level < this.context.logLevel) return; - - var args = Array.prototype.slice.apply(arguments); - var levelKey = _.findKey(Book.LOG_LEVELS, function(v) { return v == args[0]; }); - - args[0] = Book.LOG_COLORS[levelKey](levelKey.toLowerCase()+":"); - 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); - // Init and return a book Book.init = function(root) { @@ -616,10 +613,10 @@ Book.init = function(root) { }, []); }; - book.logInfo("init book at", root); + book.log.infoLn("init book at", root); return fs.mkdirp(root) .then(function() { - book.logInfo("detect structure from SUMMARY (if it exists)"); + book.log.infoLn("detect structure from SUMMARY (if it exists)"); return book.parseSummary(); }) .fail(function() { @@ -652,7 +649,7 @@ Book.init = function(root) { return fs.exists(absolutePath) .then(function(exists) { - book.logInfo("create", chapter.path); + book.log.infoLn("create", chapter.path); if(exists) return; return fs.mkdirp(path.dirname(absolutePath)) @@ -663,7 +660,7 @@ Book.init = function(root) { })); }) .then(function() { - book.logInfo("initialization is finished"); + book.log.infoLn("initialization is finished"); }); }; |