[Pkg-javascript-commits] [less.js] 152/285: Add logger
Jonas Smedegaard
dr at jones.dk
Mon Oct 26 23:23:49 UTC 2015
This is an automated email from the git hooks/post-receive script.
js pushed a commit to annotated tag v2.0.0
in repository less.js.
commit 8d431179e57c713d770650bd246c8ac5fac3d2d2
Author: Luke Page <luke.a.page at gmail.com>
Date: Sun Oct 5 20:36:23 2014 +0100
Add logger
---
bin/lessc | 30 ++++++++++++-----
lib/less-browser/browser-import.js | 6 ++--
lib/less-browser/index.js | 67 +++++++++++++++++++++++---------------
lib/less-node/environment.js | 3 --
lib/less-node/url-import.js | 5 +--
lib/less/contexts.js | 2 --
lib/less/functions/data-uri.js | 7 ++--
lib/less/index.js | 3 +-
lib/less/logger.js | 34 +++++++++++++++++++
9 files changed, 109 insertions(+), 48 deletions(-)
diff --git a/bin/lessc b/bin/lessc
index afb75c3..9a497bd 100755
--- a/bin/lessc
+++ b/bin/lessc
@@ -9,12 +9,12 @@ var less = require('../lib/less-node'),
pluginManager = new less.PluginManager(less);
var args = process.argv.slice(1);
-var options = {
+var silent = false,
+ verbose = false,
+ options = {
depends: false,
compress: false,
max_line_len: -1,
- silent: false,
- verbose: false,
lint: false,
paths: [],
color: true,
@@ -90,11 +90,11 @@ args = args.filter(function (arg) {
continueProcessing = false;
break;
case 'verbose':
- options.verbose = true;
+ verbose = true;
break;
case 's':
case 'silent':
- options.silent = true;
+ silent = true;
break;
case 'l':
case 'lint':
@@ -364,15 +364,29 @@ var parseLessFile = function (e, data) {
options.plugins = pluginManager;
+ less.logger.addListener({
+ info: function(msg) {
+ if (verbose) {
+ console.log(msg);
+ }
+ },
+ warn: function(msg) {
+ if (!silent) {
+ console.warn(msg);
+ }
+ },
+ error: function(msg) {
+ console.log(msg);
+ }
+ });
+
less.render(data, options)
.then(function(result) {
if(!options.lint) {
if (output) {
ensureDirectory(output);
fs.writeFileSync(output, result.css, 'utf8');
- if (options.verbose) {
- console.log('lessc: wrote ' + output);
- }
+ less.logger.info('lessc: wrote ' + output);
} else {
process.stdout.write(result.css);
}
diff --git a/lib/less-browser/browser-import.js b/lib/less-browser/browser-import.js
index 8e4b4a0..e5279c0 100644
--- a/lib/less-browser/browser-import.js
+++ b/lib/less-browser/browser-import.js
@@ -1,6 +1,6 @@
/*global window, XMLHttpRequest */
-module.exports = function(options, isFileProtocol, log, logLevel) {
+module.exports = function(options, isFileProtocol, logger) {
var PromiseConstructor = typeof Promise === 'undefined' ? require('promise') : Promise,
AbstractFileManager = require("../less/environment/abstract-file-manager.js");
@@ -18,7 +18,7 @@ function getXMLHttpRequest() {
/*global ActiveXObject */
return new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {
- log("browser doesn't support AJAX.", logLevel.errors);
+ logger.error("browser doesn't support AJAX.");
return null;
}
}
@@ -46,7 +46,7 @@ BrowserImport.prototype.doXHR = function doXHR(url, type, callback, errback) {
if (typeof(xhr.overrideMimeType) === 'function') {
xhr.overrideMimeType('text/css');
}
- log("XHR: Getting '" + url + "'", logLevel.debug);
+ logger.debug("XHR: Getting '" + url + "'");
xhr.open('GET', url, async);
xhr.setRequestHeader('Accept', type || 'text/x-less, text/css; q=0.9, */*; q=0.5');
xhr.send(null);
diff --git a/lib/less-browser/index.js b/lib/less-browser/index.js
index 2d48326..e75f067 100644
--- a/lib/less-browser/index.js
+++ b/lib/less-browser/index.js
@@ -3,23 +3,8 @@
//
/*global window, document, location */
-var logLevel = {
- debug: 3,
- info: 2,
- errors: 1,
- none: 0
-};
-
var less;
-function log(str, level) {
- if (typeof(console) !== 'undefined' && less.logLevel >= level) {
- //TODO no idea why console.log has stopped working in phantom
- //move to a proper logger and registering a listener
- console.warn('less: ' + str);
- }
-}
-
/*
TODO - options is now hidden - we should expose it on the less object, but not have it "as" the less object
alternately even have it on environment
@@ -30,11 +15,41 @@ function log(str, level) {
*/
var isFileProtocol = /^(file|chrome(-extension)?|resource|qrc|app):/.test(location.protocol),
- options = window.less || {},
- environment = require("./environment"),
- browserImport = require("./browser-import")(options, isFileProtocol, log, logLevel);
+ options = window.less || {};
-window.less = less = require('../less')(environment, [browserImport]);
+window.less = less = require('../less')(require("./environment"));
+var environment = less.environment,
+ browserImport = require("./browser-import")(options, isFileProtocol, less.logger);
+environment.addFileManager(browserImport);
+
+var logLevel = {
+ debug: 3,
+ info: 2,
+ errors: 1,
+ none: 0
+};
+less.logger.addListener({
+ debug: function(msg) {
+ if (options.logLevel >= logLevel.debug) {
+ console.log(msg);
+ }
+ },
+ info: function(msg) {
+ if (options.logLevel >= logLevel.info) {
+ console.log(msg);
+ }
+ },
+ warn: function(msg) {
+ if (options.logLevel >= logLevel.warn) {
+ console.warn(msg);
+ }
+ },
+ error: function(msg) {
+ if (options.logLevel >= logLevel.error) {
+ console.error(msg);
+ }
+ }
+});
less.env = options.env || (location.hostname == '127.0.0.1' ||
location.hostname == '0.0.0.0' ||
@@ -113,7 +128,7 @@ function errorConsole(e, rootHref) {
} else if (e.stack) {
content += e.stack;
}
- log(content, logLevel.errors);
+ less.logger.error(content);
}
function createCSS(styles, sheet, lastModified) {
@@ -172,13 +187,13 @@ function createCSS(styles, sheet, lastModified) {
// Don't update the local store if the file wasn't modified
if (lastModified && cache) {
- log('saving ' + href + ' to cache.', logLevel.info);
+ less.logger.info('saving ' + href + ' to cache.');
try {
cache.setItem(href, styles);
cache.setItem(href + ':timestamp', lastModified);
} catch(e) {
//TODO - could do with adding more robust error handling
- log('failed to save', logLevel.errors);
+ less.logger.error('failed to save');
}
}
}
@@ -513,15 +528,15 @@ less.refresh = function (reload, modifyVars) {
return error(e, e.href || sheet.href);
}
if (webInfo.local) {
- log("loading " + sheet.href + " from cache.", logLevel.info);
+ less.logger.info("loading " + sheet.href + " from cache.");
} else {
- log("rendered " + sheet.href + " successfully.", logLevel.debug);
+ less.logger.info("rendered " + sheet.href + " successfully.");
css = postProcessCSS(css);
createCSS(css, sheet, webInfo.lastModified);
}
- log("css for " + sheet.href + " generated in " + (new Date() - endTime) + 'ms', logLevel.info);
+ less.logger.info("css for " + sheet.href + " generated in " + (new Date() - endTime) + 'ms');
if (webInfo.remaining === 0) {
- log("less has finished. css generated in " + (new Date() - startTime) + 'ms', logLevel.info);
+ less.logger.info("less has finished. css generated in " + (new Date() - startTime) + 'ms');
}
endTime = new Date();
}, reload, modifyVars);
diff --git a/lib/less-node/environment.js b/lib/less-node/environment.js
index 63b564b..4df4e18 100644
--- a/lib/less-node/environment.js
+++ b/lib/less-node/environment.js
@@ -1,7 +1,4 @@
module.exports = {
- warn: function(msg) {
- console.warn(msg);
- },
encodeBase64: function encodeBase64(str) {
return new Buffer(str).toString('base64');
},
diff --git a/lib/less-node/url-import.js b/lib/less-node/url-import.js
index 7d5aa46..63a2041 100644
--- a/lib/less-node/url-import.js
+++ b/lib/less-node/url-import.js
@@ -2,7 +2,8 @@ var isUrlRe = /^(?:https?:)?\/\//i,
url = require('url'),
request,
PromiseConstructor = typeof Promise === 'undefined' ? require('promise') : Promise,
- AbstractFileManager = require("../less/environment/abstract-file-manager.js");
+ AbstractFileManager = require("../less/environment/abstract-file-manager.js"),
+ logger = require("../less/logger");
var UrlImport = function() {
};
@@ -42,7 +43,7 @@ UrlImport.prototype.loadFile = function(filename, currentDirectory, options, env
return;
}
if (!body) {
- environment.warn('Warning: Empty body (HTTP '+ res.statusCode + ') returned by "' + urlStr +'"');
+ logger.warn('Warning: Empty body (HTTP '+ res.statusCode + ') returned by "' + urlStr +'"');
}
fullfill({ contents: body, filename: urlStr });
});
diff --git a/lib/less/contexts.js b/lib/less/contexts.js
index 84da3e6..ca34051 100644
--- a/lib/less/contexts.js
+++ b/lib/less/contexts.js
@@ -40,8 +40,6 @@ contexts.Parse = function(options) {
};
var evalCopyProperties = [
- 'silent', // whether to swallow errors and warnings
- 'verbose', // whether to log more activity
'compress', // whether to compress
'ieCompat', // whether to enforce IE compatibility (IE8 data-uri)
'strictMath', // whether math has to be within parenthesis
diff --git a/lib/less/functions/data-uri.js b/lib/less/functions/data-uri.js
index 205dfb4..dc7b977 100644
--- a/lib/less/functions/data-uri.js
+++ b/lib/less/functions/data-uri.js
@@ -4,7 +4,8 @@ module.exports = function(environment) {
functionRegistry = require("./function-registry"),
fallback = function(functionThis, node) {
return new URL(node, functionThis.index, functionThis.currentFileInfo).eval(functionThis.context);
- };
+ },
+ logger = require('../logger');
functionRegistry.add("data-uri", function(mimetypeNode, filePathNode) {
@@ -49,7 +50,7 @@ module.exports = function(environment) {
var fileSync = fileManager.loadFileSync(filePath, currentDirectory, this.context, environment);
if (!fileSync.contents) {
- environment.warn("Skipped data-uri embedding because file not found");
+ logger.warn("Skipped data-uri embedding because file not found");
return fallback(this, filePathNode || mimetypeNode);
}
var buf = fileSync.contents;
@@ -61,7 +62,7 @@ module.exports = function(environment) {
if (fileSizeInKB >= DATA_URI_MAX_KB) {
if (this.context.ieCompat !== false) {
- environment.warn("Skipped data-uri embedding of %s because its size (%dKB) exceeds IE8-safe %dKB!", filePath, fileSizeInKB, DATA_URI_MAX_KB);
+ logger.warn("Skipped data-uri embedding of %s because its size (%dKB) exceeds IE8-safe %dKB!", filePath, fileSizeInKB, DATA_URI_MAX_KB);
return fallback(this, filePathNode || mimetypeNode);
}
diff --git a/lib/less/index.js b/lib/less/index.js
index b2be18b..43546e0 100644
--- a/lib/less/index.js
+++ b/lib/less/index.js
@@ -20,7 +20,8 @@ module.exports = function(environment, fileManagers) {
LessError: require('./less-error'),
transformTree: require('./transform-tree'),
utils: require('./utils'),
- PluginManager: require('./plugin-manager')
+ PluginManager: require('./plugin-manager'),
+ logger: require('./logger')
};
return less;
diff --git a/lib/less/logger.js b/lib/less/logger.js
new file mode 100644
index 0000000..d073a01
--- /dev/null
+++ b/lib/less/logger.js
@@ -0,0 +1,34 @@
+module.exports = {
+ error: function(msg) {
+ this._fireEvent("error", msg);
+ },
+ warn: function(msg) {
+ this._fireEvent("warn", msg);
+ },
+ info: function(msg) {
+ this._fireEvent("info", msg);
+ },
+ debug: function(msg) {
+ this._fireEvent("debug", msg);
+ },
+ addListener: function(listener) {
+ this._listeners.push(listener);
+ },
+ removeListener: function(listener) {
+ for(var i = 0; i < this._listeners.length; i++) {
+ if (this._listeners[i] === listener) {
+ this._listeners.splice(i, 1);
+ return;
+ }
+ }
+ },
+ _fireEvent: function(type, msg) {
+ for(var i = 0; i < this._listeners.length; i++) {
+ var logFunction = this._listeners[i]["type"];
+ if (logFunction) {
+ logFunction(msg);
+ }
+ }
+ },
+ _listeners: []
+};
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/less.js.git
More information about the Pkg-javascript-commits
mailing list