Skip to content

Commit

Permalink
Refactor main Dust module's global context
Browse files Browse the repository at this point in the history
Uses the same UMD pattern as parser and compiler now, instead of a slightly different one. `root` should only be used to refer to the browser window, not Node's `global`, so now we use `console` directly instead of `root.console`.

Fixed up tests to ensure that we call dust.log even though debugLevel is turned off to avoid spam, to make sure that we test the code path.

Fixes #500 by removing the IIFE in favor of `this`.
Fixes #524 by allowing DEBUG=dust to be set on the command line to turn on debugging.
  • Loading branch information
Seth Kinast committed Mar 24, 2015
1 parent 6302f25 commit 93280c9
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 62 deletions.
106 changes: 48 additions & 58 deletions lib/dust.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,18 @@
(function(root) {
(function (root, factory) {
/*global define*/
if (typeof define === 'function' && define.amd && define.amd.dust === true) {
define('dust.core', [], factory);
} else if (typeof exports === 'object') {
module.exports = factory();
} else {
root.dust = factory();
}
}(this, function() {
var dust = {
"version": "2.6.1"
},
NONE = 'NONE',
ERROR = 'ERROR',
WARN = 'WARN',
INFO = 'INFO',
DEBUG = 'DEBUG',
loggingLevels = {
DEBUG: 0,
INFO: 1,
WARN: 2,
ERROR: 3,
NONE: 4
},
EMPTY_FUNC = function() {},
logger = {},
originalLog,
loggerContext;

dust.debugLevel = NONE;
NONE = 'NONE', ERROR = 'ERROR', WARN = 'WARN', INFO = 'INFO', DEBUG = 'DEBUG',
EMPTY_FUNC = function() {};

dust.config = {
whitespace: false,
Expand All @@ -41,42 +34,47 @@
"helper": "h"
};

// Try to find the console in global scope
if (root && root.console && root.console.log) {
loggerContext = root.console;
originalLog = root.console.log;
}
(function initLogging() {
/*global process, console*/
var loggingLevels = { DEBUG: 0, INFO: 1, WARN: 2, ERROR: 3, NONE: 4 },
consoleLog,
log;

// robust logger for node.js, modern browsers, and IE <= 9.
logger.log = loggerContext ? function() {
// Do this for normal browsers
if (typeof originalLog === 'function') {
logger.log = function() {
originalLog.apply(loggerContext, arguments);
if (typeof console !== 'undefined' && console.log) {
consoleLog = console.log;
if(typeof consoleLog === 'function') {
log = function() {
consoleLog.apply(console, arguments);
};
} else {
// Do this for IE <= 9
logger.log = function() {
var message = Array.prototype.slice.apply(arguments).join(' ');
originalLog(message);
log = function() {
consoleLog(Array.prototype.slice.apply(arguments).join(' '));
};
}
logger.log.apply(this, arguments);
} : EMPTY_FUNC;
} else {
log = EMPTY_FUNC;
}

/**
* Filters messages based on `dust.debugLevel`.
* This default implementation will print to the console if it exists.
* @param {String|Error} message the message to print/throw
* @param {String} type the severity of the message(ERROR, WARN, INFO, or DEBUG)
* @public
*/
dust.log = function(message, type) {
type = type || INFO;
if (loggingLevels[type] >= loggingLevels[dust.debugLevel]) {
log('[DUST:' + type + ']', message);
}
};

/**
* Filters messages based on `dust.debugLevel`.
* This default implementation will print to the console if it exists.
* @param {String|Error} message the message to print/throw
* @param {String} type the severity of the message(ERROR, WARN, INFO, or DEBUG)
* @public
*/
dust.log = function(message, type) {
type = type || INFO;
if (loggingLevels[type] >= loggingLevels[dust.debugLevel]) {
logger.log('[DUST:' + type + ']', message);
dust.debugLevel = NONE;
if(typeof process !== 'undefined' && process.env && /\bdust\b/.test(process.env.DEBUG)) {
dust.debugLevel = DEBUG;
}
};

}());

dust.helpers = {};

Expand Down Expand Up @@ -893,14 +891,6 @@
}
};

if (typeof define === "function" && define.amd && define.amd.dust === true) {
define("dust.core", function() {
return dust;
});
} else if (typeof exports === 'object') {
module.exports = dust;
} else {
root.dust = dust;
}
return dust;

})((function(){return this;})());
}));
1 change: 0 additions & 1 deletion test/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,6 @@ function testRender(unit, source, context, expected, options, baseContext, error
messageInLog = '';
try {
dust.isDebug = !!(error || logMessage);
dust.debugLevel = 'DEBUG';
dust.config = config || { whitespace: false };
dust.loadSource(dust.compile(source, name));
if (baseContext){
Expand Down
5 changes: 2 additions & 3 deletions test/jasmine-test/spec/renderTestSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,18 @@ describe ('Test the basic functionality of dust', function() {
});

// Absorb all logs into a log queue for testing purposes
var dustLog = dust.log;
dust.logQueue = [];
dust.log = function(msg, type) {
dust.logQueue.push({ message: msg, type: type });
dustLog.call(this, msg, type);
};

function render(test) {
var messageInLog = false;
return function() {
var context;
try {
dust.debugLevel = 'DEBUG';
dust.config = test.config || { whitespace: false };
dust.loadSource(dust.compile(test.source, test.name, test.strip));
context = test.context;
Expand Down Expand Up @@ -69,7 +70,6 @@ function stream(test) {
output = '';
log = [];
try {
dust.debugLevel = 'DEBUG';
dust.config = test.config || { whitespace: false };
dust.loadSource(dust.compile(test.source, test.name));
context = test.context;
Expand Down Expand Up @@ -158,7 +158,6 @@ function pipe(test) {
messageInLog = false;
messageInLogTwo = false;
try {
dust.debugLevel = 'DEBUG';
dust.config = test.config || { whitespace: false };
dust.loadSource(dust.compile(test.source, test.name));
context = test.context;
Expand Down
2 changes: 2 additions & 0 deletions test/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ var dust = require('../lib/server');
require('./jasmine-test/spec/testHelpers');

// Absorb all logs into a log queue for testing purposes
var dustLog = dust.log;
dust.logQueue = [];
dust.log = function(msg, type) {
dust.logQueue.push({ message: msg, type: type });
dustLog.call(this, msg, type);
};

function dumpError(err) {
Expand Down

0 comments on commit 93280c9

Please sign in to comment.