Created
May 18, 2012 18:51
-
-
Save buzzedword/2727017 to your computer and use it in GitHub Desktop.
Debug module for enabling and disabling console on the fly. Useful for server environment settings.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DebugModule: { | |
! | |
function(undefined) { | |
// Global debug object. Enable and disable console functionality. Defaults to off. | |
var Debug = function(bool) { | |
var func = {}, | |
state = undefined, | |
Console = undefined, | |
// Common console functionality. | |
consoleFunctionality = { | |
log: function() {}, | |
warn: function() {}, | |
info: function() {} | |
}, | |
RegisteredCallers = []; | |
StartupFunctionality: { | |
// Check for existance of console, if none then define common functionality. | |
if (typeof console == "undefined") { | |
window.console = consoleFunctionality; | |
} | |
}; | |
// Internal reference to the global instance of console. | |
Console = window.console; | |
PublicModuleFunctions: { | |
// Enable console functionality, if available. If terseFlag is set to true, only registered callers can log. | |
func.enable = function(terseFlag) { | |
if (terseFlag != true) { | |
window.console = Console; | |
} else { | |
window.console = consoleFunctionality; | |
window.console.log = function(caller) { | |
if (RegisteredCallers.indexOf(caller) != -1) { | |
Console.log("Terse mode. Output: \n\r", Array.prototype.slice.call(arguments, 1)); | |
} | |
}; | |
} | |
return state = true; | |
}; | |
// Disable console function. | |
func.disable = function() { | |
window.console = consoleFunctionality; | |
return state = false; | |
}; | |
// Return the state of debug mode. | |
func.getState = function() { | |
return state; | |
}; | |
// Register a module for calling. Any module not registered not be logged. Terse mode. | |
func.registerCaller = function(caller) { | |
if (typeof caller === "string") { | |
RegisteredCallers.push(caller); | |
return "Caller registered: " + caller; | |
} else { | |
return "Cannot register caller."; | |
} | |
}; | |
}; | |
DefaultBehavior: { | |
if (typeof bool !== "undefined") { | |
switch (bool) { | |
case true: | |
func.enable(); | |
break; | |
case false: | |
func.disable(); | |
break; | |
} | |
} | |
}; | |
return func; | |
}; | |
// Set global debug object, and disable console messages. | |
window.debug = new Debug(false); | |
}(); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment