Skip to main content
Updated.
Source Link
Gabriel Nahmias
  • 996
  • 3
  • 15
  • 20
(function(context){ // Only global namespace. var Console = { //Settings settings: { debug: { alwaysShowURL: false, enabled: true, showInfo: true }, stackTrace: { enabled: true, collapsed: true, ignoreDebugFuncs: true, spacing: false } } }; // String formatting prototype function. if (!String.prototype.format) { String.prototype.format = function () { var s = this.toString(), args = typeof arguments[0], args = (("string" == args || "number" == args) ? arguments : arguments[0]); if (!arguments.length) return s; for (arg in args) s = s.replace(RegExp("\\{" + arg + "\\}", "gi"), args[arg]); return s; } } // String repeating prototype function. if (!String.prototype.times) { String.prototype.times = function () { var s = this.toString(), tempStr = "", times = arguments[0]; if (!arguments.length) return s; for (var i = 0; i < times; i++) tempStr += s; return tempStr; } } // Commonly used functions Console.debug = function () { if (Console.settings.debug.enabled) { var args = ((typeof arguments !== 'undefined') ? Array.prototype.slice.call(arguments, 0) : []),   sUA = navigator.userAgent,   currentBrowser = {   firefox: /firefox/gi.test(sUA),   webkit: /webkit/gi.test(sUA),   },   aLines = Console.stackTrace().split("\n"),   aCurrentLine,   iCurrIndex = ((currentBrowser.webkit) ? 3 : 2),   sCssBlack = "color:black;",   sCssFormat = "color:{0}; font-weight:bold;",   sLines = "";  if (currentBrowser.firefox) aCurrentLine = aLines[iCurrIndex].replace(/(.*):/, "$1@").split("@"); else if (currentBrowser.webkit) aCurrentLinesLines = aLines[iCurrIndex].replace("at ", "").replace(")", "").replace(/( \()/gi, "@").replace(/(.*):(\d*):(\d*)/, "$1@$2@$3").split("@");"";   if (currentBrowser.firefox) aCurrentLine = aLines[iCurrIndex].replace(/(.*):/, "$1@").split("@"); else if (currentBrowser.webkit) aCurrentLine = aLines[iCurrIndex].replace("at ", "").replace(")", "").replace(/( \()/gi, "@").replace(/(.*):(\d*):(\d*)/, "$1@$2@$3").split("@"); // Show info if the setting is true and there's no extra trace (would be kind of pointless).   if (Console.settings.debug.showInfo && !Console.settings.stackTrace.enabled) {   var sFunc = aCurrentLine[0].trim(),   sURL = aCurrentLine[1].trim(),   sURL = ((!Console.settings.debug.alwaysShowURL && context.location.href == sURL) ? "this page" : sURL),   sLine = aCurrentLine[2].trim(),   sCol;     if (currentBrowser.webkit)   sCol = aCurrentLine[3].trim();     console.info("%cOn line %c{0}%c{1}%c{2}%c of %c{3}%c inside the %c{4}%c function:".format(sLine, ((currentBrowser.webkit) ? ", column " : ""), ((currentBrowser.webkit) ? sCol : ""), sURL, sFunc),   sCssBlack, sCssFormat.format("red"),   sCssBlack, sCssFormat.format("purple"),   sCssBlack, sCssFormat.format("green"),   sCssBlack, sCssFormat.format("blue"),   sCssBlack);   }     // If the setting permits, get rid of the two obvious debug functions (Console.debug and Console.stackTrace).   if (Console.settings.stackTrace.ignoreDebugFuncs) {   // In WebKit (Chrome at least), there's an extra line at the top that says "Error" so adjust for this.   if (currentBrowser.webkit) aLines.shift(); aLines.shift();   aLines.shift(); aLines.shift();}    }  sLines = aLines.join(((Console.settings.stackTrace.spacing) ? "\n\n" : "\n")).trim();     trace = typeof trace !== 'undefined' ? trace : true;   if (typeof console !=== "undefined") &&{  Console.settings.debug.enabled == true) {  for (var arg in args)   console.debug(args[arg]);     if (Console.settings.stackTrace.enabled) {   var sCss = "color:red; font-weight: bold;",   sTitle = "%c Stack Trace" + " ".times(70);     if (Console.settings.stackTrace.collapsed)   console.groupCollapsed(sTitle, sCss);   else   console.group(sTitle, sCss);     console.debug("%c" + sLines, "color: #666666; font-style: italic;");     console.groupEnd(); } } } } Console.stackTrace = function () { var err = new Error(); return err.stack; } context.Console = Console; })(window); 
(function(context){ // Only global namespace. var Console = { //Settings settings: { debug: { alwaysShowURL: false, enabled: true, showInfo: true }, stackTrace: { enabled: true, collapsed: true, ignoreDebugFuncs: true, spacing: false } } }; // String formatting prototype function. if (!String.prototype.format) { String.prototype.format = function () { var s = this.toString(), args = typeof arguments[0], args = (("string" == args || "number" == args) ? arguments : arguments[0]); if (!arguments.length) return s; for (arg in args) s = s.replace(RegExp("\\{" + arg + "\\}", "gi"), args[arg]); return s; } } // String repeating prototype function. if (!String.prototype.times) { String.prototype.times = function () { var s = this.toString(), tempStr = "", times = arguments[0]; if (!arguments.length) return s; for (var i = 0; i < times; i++) tempStr += s; return tempStr; } } // Commonly used functions Console.debug = function () { var args = ((typeof arguments !== 'undefined') ? Array.prototype.slice.call(arguments, 0) : []), sUA = navigator.userAgent, currentBrowser = { firefox: /firefox/gi.test(sUA), webkit: /webkit/gi.test(sUA), }, aLines = Console.stackTrace().split("\n"), aCurrentLine, iCurrIndex = ((currentBrowser.webkit) ? 3 : 2), sCssBlack = "color:black;", sCssFormat = "color:{0}; font-weight:bold;",   sLines = "";  if (currentBrowser.firefox) aCurrentLine = aLines[iCurrIndex].replace(/(.*):/, "$1@").split("@"); else if (currentBrowser.webkit) aCurrentLine = aLines[iCurrIndex].replace("at ", "").replace(")", "").replace(/( \()/gi, "@").replace(/(.*):(\d*):(\d*)/, "$1@$2@$3").split("@"); // Show info if the setting is true and there's no extra trace (would be kind of pointless). if (Console.settings.debug.showInfo && !Console.settings.stackTrace.enabled) { var sFunc = aCurrentLine[0].trim(), sURL = aCurrentLine[1].trim(), sURL = ((!Console.settings.debug.alwaysShowURL && context.location.href == sURL) ? "this page" : sURL), sLine = aCurrentLine[2].trim(), sCol; if (currentBrowser.webkit) sCol = aCurrentLine[3].trim(); console.info("%cOn line %c{0}%c{1}%c{2}%c of %c{3}%c inside the %c{4}%c function:".format(sLine, ((currentBrowser.webkit) ? ", column " : ""), ((currentBrowser.webkit) ? sCol : ""), sURL, sFunc), sCssBlack, sCssFormat.format("red"), sCssBlack, sCssFormat.format("purple"), sCssBlack, sCssFormat.format("green"), sCssBlack, sCssFormat.format("blue"), sCssBlack); } // If the setting permits, get rid of the two obvious debug functions (Console.debug and Console.stackTrace). if (Console.settings.stackTrace.ignoreDebugFuncs) { // In WebKit (Chrome at least), there's an extra line at the top that says "Error" so adjust for this. if (currentBrowser.webkit) aLines.shift(); aLines.shift(); aLines.shift(); }  sLines = aLines.join(((Console.settings.stackTrace.spacing) ? "\n\n" : "\n")).trim(); trace = typeof trace !== 'undefined' ? trace : true; if (typeof console != "undefined" && Console.settings.debug.enabled == true) {  for (var arg in args) console.debug(args[arg]); if (Console.settings.stackTrace.enabled) { var sCss = "color:red; font-weight: bold;", sTitle = "%c Stack Trace" + " ".times(70); if (Console.settings.stackTrace.collapsed) console.groupCollapsed(sTitle, sCss); else console.group(sTitle, sCss); console.debug("%c" + sLines, "color: #666666; font-style: italic;"); console.groupEnd(); } } } Console.stackTrace = function () { var err = new Error(); return err.stack; } context.Console = Console; })(window); 
(function(context){ // Only global namespace. var Console = { //Settings settings: { debug: { alwaysShowURL: false, enabled: true, showInfo: true }, stackTrace: { enabled: true, collapsed: true, ignoreDebugFuncs: true, spacing: false } } }; // String formatting prototype function. if (!String.prototype.format) { String.prototype.format = function () { var s = this.toString(), args = typeof arguments[0], args = (("string" == args || "number" == args) ? arguments : arguments[0]); if (!arguments.length) return s; for (arg in args) s = s.replace(RegExp("\\{" + arg + "\\}", "gi"), args[arg]); return s; } } // String repeating prototype function. if (!String.prototype.times) { String.prototype.times = function () { var s = this.toString(), tempStr = "", times = arguments[0]; if (!arguments.length) return s; for (var i = 0; i < times; i++) tempStr += s; return tempStr; } } // Commonly used functions Console.debug = function () { if (Console.settings.debug.enabled) { var args = ((typeof arguments !== 'undefined') ? Array.prototype.slice.call(arguments, 0) : []),   sUA = navigator.userAgent,   currentBrowser = {   firefox: /firefox/gi.test(sUA),   webkit: /webkit/gi.test(sUA),   },   aLines = Console.stackTrace().split("\n"),   aCurrentLine,   iCurrIndex = ((currentBrowser.webkit) ? 3 : 2),   sCssBlack = "color:black;",   sCssFormat = "color:{0}; font-weight:bold;", sLines = "";   if (currentBrowser.firefox) aCurrentLine = aLines[iCurrIndex].replace(/(.*):/, "$1@").split("@"); else if (currentBrowser.webkit) aCurrentLine = aLines[iCurrIndex].replace("at ", "").replace(")", "").replace(/( \()/gi, "@").replace(/(.*):(\d*):(\d*)/, "$1@$2@$3").split("@"); // Show info if the setting is true and there's no extra trace (would be kind of pointless).   if (Console.settings.debug.showInfo && !Console.settings.stackTrace.enabled) {   var sFunc = aCurrentLine[0].trim(),   sURL = aCurrentLine[1].trim(),   sURL = ((!Console.settings.debug.alwaysShowURL && context.location.href == sURL) ? "this page" : sURL),   sLine = aCurrentLine[2].trim(),   sCol;     if (currentBrowser.webkit)   sCol = aCurrentLine[3].trim();     console.info("%cOn line %c{0}%c{1}%c{2}%c of %c{3}%c inside the %c{4}%c function:".format(sLine, ((currentBrowser.webkit) ? ", column " : ""), ((currentBrowser.webkit) ? sCol : ""), sURL, sFunc),   sCssBlack, sCssFormat.format("red"),   sCssBlack, sCssFormat.format("purple"),   sCssBlack, sCssFormat.format("green"),   sCssBlack, sCssFormat.format("blue"),   sCssBlack);   }     // If the setting permits, get rid of the two obvious debug functions (Console.debug and Console.stackTrace).   if (Console.settings.stackTrace.ignoreDebugFuncs) {   // In WebKit (Chrome at least), there's an extra line at the top that says "Error" so adjust for this.   if (currentBrowser.webkit) aLines.shift(); aLines.shift();   aLines.shift(); }    sLines = aLines.join(((Console.settings.stackTrace.spacing) ? "\n\n" : "\n")).trim();     trace = typeof trace !== 'undefined' ? trace : true;   if (typeof console !== "undefined") {  for (var arg in args)   console.debug(args[arg]);     if (Console.settings.stackTrace.enabled) {   var sCss = "color:red; font-weight: bold;",   sTitle = "%c Stack Trace" + " ".times(70);     if (Console.settings.stackTrace.collapsed)   console.groupCollapsed(sTitle, sCss);   else   console.group(sTitle, sCss);     console.debug("%c" + sLines, "color: #666666; font-style: italic;");     console.groupEnd(); } } } } Console.stackTrace = function () { var err = new Error(); return err.stack; } context.Console = Console; })(window); 
Updated for v1.2.
Source Link
Gabriel Nahmias
  • 996
  • 3
  • 15
  • 20
(function(context){ // Only global namespace.  var Console = {   //Settings   settings: {   debug: {   alwaysShowURL: false,  enabled: true,   showInfo: true   },   stackTrace: {   enabled: true,   collapsed: true,   ignoreDebugFuncs: true,   spacing: false } } } };  // String formatting prototype function.  if (!String.prototype.format) {   String.prototype.format = function () {   var s = this.toString(),   args = typeof arguments[0],   args = (("string" == args || "number" == args) ? arguments : arguments[0]);   if (!arguments.length) return s; for (arg in args) s = s.replace(RegExp("\\{" + arg + "\\}", "gi"), args[arg]); return s; for (arg in args) s = s.replace(RegExp("\\{" + arg + "\\}", "gi"), args[arg]); return s; } }   // String repeating prototype function.  if (!String.prototype.times) {   String.prototype.times = function () {   var s = this.toString(),   tempStr = "",   times = arguments[0];   if (!arguments.length)   return s;   for (var i = 0; i < times; i++)   tempStr += s;   return tempStr; } } }   // Commonly used functions  Console.debug = function () {   var args = ((typeof arguments !== 'undefined') ? Array.prototype.slice.call(arguments, 0) : []), sUA = navigator.userAgent, currentBrowser = { firefox: /firefox/gi.test(sUA), webkit: /webkit/gi.test(sUA), },   aLines = Console.stackTrace().split("\n"),   aCurrentLine,  iCurrIndex = aLines[2].replace(/(currentBrowser.*webkit) ? 3 :/, "$1@").split("@"2),   sCssBlack = "color:black;",   sCssFormat = "color:{0}; font-weight:bold;",   sLines = ""; // Show info  if the(currentBrowser.firefox)  setting is true and there's no extra trace (would be kind ofaCurrentLine pointless= aLines[iCurrIndex].replace(/(.*):/, "$1@").split("@");   else if (Console.settings.debugcurrentBrowser.showInfowebkit)  && !Console.settings.stackTrace.enabled) {  var sFuncaCurrentLine = aCurrentLine[0]aLines[iCurrIndex].replace("at ", "").replace(")", "").replace(/( \()/gi, "@").replace(/(.*):(\d*):(\d*)/, "$1@$2@$3").split("@");    sURL// =Show aCurrentLine[1], info if the setting is true and there's no extra trace (would sLinebe =kind aCurrentLine[2];of pointless). console.infoif ("%cOnConsole.settings.debug.showInfo line&& %c!Console.settings.stackTrace.enabled) {0}%c   in %c{1}%c inside the %c{2}%c function:".format(sLine, sURL, sFunc),  var sFunc = aCurrentLine[0].trim(),  sCssBlack, sCssFormat sURL = aCurrentLine[1].formattrim("red"), sURL = ((!Console.settings.debug.alwaysShowURL && context.location.href sCssBlack,== sCssFormat.format("green"sURL),  ? "this page" : sURL),  sLine sCssBlack,= sCssFormataCurrentLine[2].formattrim("blue"),   sCssBlack); }sCol; // If the setting permits, get rid of the two obvious debug functionsif (Console.debug and ConsolecurrentBrowser.stackTracewebkit).   if (Console.settings.stackTrace.ignoreDebugFuncs) {  aLines.shift();  sCol = aLinesaCurrentLine[3].shifttrim(); } sLines = aLines console.joininfo("%cOn line %c{0}%c{1}%c{2}%c of %c{3}%c inside the %c{4}%c function:".format(sLine, (Console.settings(currentBrowser.stackTracewebkit) ? ", column " : ""), ((currentBrowser.spacingwebkit) ? "\n\n"sCol : "\n"""), sURL, sFunc), sCssBlack, sCssFormat.trimformat("red"), sCssBlack, sCssFormat.format("purple"), sCssBlack, sCssFormat.format("green"), sCssBlack, sCssFormat.format("blue"), sCssBlack); } trace = typeof trace !==// 'undefined'If ?the tracesetting :permits, true; get rid of the iftwo obvious debug functions (typeofConsole.debug consoleand !=Console.stackTrace).  "undefined" &&  if (Console.settings.debugstackTrace.enabled == trueignoreDebugFuncs) {   // In WebKit (Chrome at least), there's an extra line at the top that says "Error" so adjust for this.  if (varcurrentBrowser.webkit)  arg in args aLines.shift(); consoleaLines.debugshift(args[arg]); aLines.shift(); } ifsLines = aLines.join(((Console.settings.stackTrace.enabledspacing) {  var sCss =? "color:red;"\n\n" font-weight: bold;", sTitle = "%c Stack Trace" + " ""\n")).timestrim(70); trace = typeof trace !== 'undefined' ? trace : true; if (typeof console != "undefined" && Console.settings.stackTracedebug.collapsedenabled == true) {   for (var arg in args)  console.groupCollapseddebug(sTitle,args[arg]); if (Console.settings.stackTrace.enabled) {  var sCss = "color:red; font-weight: bold;", sTitle = "%c Stack Trace" + " ".times(70);   else if (Console.settings.stackTrace.collapsed)   console.groupgroupCollapsed(sTitle, sCss); else   console.group(sTitle, sCss);    console.debug("%c" + sLines, "color: #666666; font-style: italic;");     console.groupEnd(); console.groupEnd();} } } }  Console.stackTrace = function () {   var err = new Error();   return err.stack; }   context.Console = Console; })(window); 

Check it out on GitHub (currently v1.2)! You can use it like Console.debug("Whatever"); and it will, depending on the settings in Console, print the output and a stack trace (or just simple info/nothing extra at all). Here's an example:

// Only global namespace. var Console = { //Settings settings: { debug: { enabled: true, showInfo: true }, stackTrace: { enabled: true, collapsed: true, ignoreDebugFuncs: true, spacing: false } } }; // String formatting prototype function. if (!String.prototype.format) { String.prototype.format = function () { var s = this.toString(), args = typeof arguments[0], args = (("string" == args || "number" == args) ? arguments : arguments[0]); if (!arguments.length) return s; for (arg in args) s = s.replace(RegExp("\\{" + arg + "\\}", "gi"), args[arg]); return s; } }  // String repeating prototype function. if (!String.prototype.times) { String.prototype.times = function () { var s = this.toString(), tempStr = "", times = arguments[0]; if (!arguments.length) return s; for (var i = 0; i < times; i++) tempStr += s; return tempStr; } }  // Commonly used functions Console.debug = function () { var args = ((typeof arguments !== 'undefined') ? Array.prototype.slice.call(arguments, 0) : []), aLines = Console.stackTrace().split("\n"), aCurrentLine = aLines[2].replace(/(.*):/, "$1@").split("@"), sCssBlack = "color:black;", sCssFormat = "color:{0}; font-weight:bold;", sLines = ""; // Show info if the setting is true and there's no extra trace (would be kind of pointless). if (Console.settings.debug.showInfo && !Console.settings.stackTrace.enabled) {  var sFunc = aCurrentLine[0], sURL = aCurrentLine[1],  sLine = aCurrentLine[2]; console.info("%cOn line %c{0}%c in %c{1}%c inside the %c{2}%c function:".format(sLine, sURL, sFunc),  sCssBlack, sCssFormat.format("red"), sCssBlack, sCssFormat.format("green"),  sCssBlack, sCssFormat.format("blue"),   sCssBlack); } // If the setting permits, get rid of the two obvious debug functions (Console.debug and Console.stackTrace).   if (Console.settings.stackTrace.ignoreDebugFuncs) {  aLines.shift();  aLines.shift(); } sLines = aLines.join(((Console.settings.stackTrace.spacing) ? "\n\n" : "\n")).trim(); trace = typeof trace !== 'undefined' ? trace : true;  if (typeof console != "undefined" && Console.settings.debug.enabled == true) { for (var arg in args) console.debug(args[arg]); if (Console.settings.stackTrace.enabled) {  var sCss = "color:red; font-weight: bold;", sTitle = "%c Stack Trace" + " ".times(70); if (Console.settings.stackTrace.collapsed) console.groupCollapsed(sTitle, sCss); else console.group(sTitle, sCss); console.debug("%c" + sLines, "color: #666666; font-style: italic;"); console.groupEnd(); } } } Console.stackTrace = function () { var err = new Error(); return err.stack; } 

Check it out on GitHub! You can use it like Console.debug("Whatever"); and it will, depending on the settings in Console, print the output and a stack trace (or just simple info/nothing extra at all). Here's an example:

(function(context){ // Only global namespace.  var Console = {   //Settings   settings: {   debug: {   alwaysShowURL: false,  enabled: true,   showInfo: true   },   stackTrace: {   enabled: true,   collapsed: true,   ignoreDebugFuncs: true,   spacing: false } } };  // String formatting prototype function.  if (!String.prototype.format) {   String.prototype.format = function () {   var s = this.toString(),   args = typeof arguments[0],   args = (("string" == args || "number" == args) ? arguments : arguments[0]);   if (!arguments.length) return s; for (arg in args) s = s.replace(RegExp("\\{" + arg + "\\}", "gi"), args[arg]); return s; } }  // String repeating prototype function.  if (!String.prototype.times) {   String.prototype.times = function () {   var s = this.toString(),   tempStr = "",   times = arguments[0];   if (!arguments.length)   return s;   for (var i = 0; i < times; i++)   tempStr += s;   return tempStr; } }  // Commonly used functions  Console.debug = function () {   var args = ((typeof arguments !== 'undefined') ? Array.prototype.slice.call(arguments, 0) : []), sUA = navigator.userAgent, currentBrowser = { firefox: /firefox/gi.test(sUA), webkit: /webkit/gi.test(sUA), },   aLines = Console.stackTrace().split("\n"),   aCurrentLine,  iCurrIndex = ((currentBrowser.webkit) ? 3 : 2),   sCssBlack = "color:black;",   sCssFormat = "color:{0}; font-weight:bold;",   sLines = "";   if (currentBrowser.firefox)  aCurrentLine = aLines[iCurrIndex].replace(/(.*):/, "$1@").split("@");   else if (currentBrowser.webkit)  aCurrentLine = aLines[iCurrIndex].replace("at ", "").replace(")", "").replace(/( \()/gi, "@").replace(/(.*):(\d*):(\d*)/, "$1@$2@$3").split("@");    // Show info if the setting is true and there's no extra trace (would be kind of pointless). if (Console.settings.debug.showInfo && !Console.settings.stackTrace.enabled) {   var sFunc = aCurrentLine[0].trim(),   sURL = aCurrentLine[1].trim(), sURL = ((!Console.settings.debug.alwaysShowURL && context.location.href == sURL) ? "this page" : sURL),  sLine = aCurrentLine[2].trim(), sCol; if (currentBrowser.webkit) sCol = aCurrentLine[3].trim();  console.info("%cOn line %c{0}%c{1}%c{2}%c of %c{3}%c inside the %c{4}%c function:".format(sLine, ((currentBrowser.webkit) ? ", column " : ""), ((currentBrowser.webkit) ? sCol : ""), sURL, sFunc), sCssBlack, sCssFormat.format("red"), sCssBlack, sCssFormat.format("purple"), sCssBlack, sCssFormat.format("green"), sCssBlack, sCssFormat.format("blue"), sCssBlack); } // If the setting permits, get rid of the two obvious debug functions (Console.debug and Console.stackTrace).    if (Console.settings.stackTrace.ignoreDebugFuncs) {   // In WebKit (Chrome at least), there's an extra line at the top that says "Error" so adjust for this.  if (currentBrowser.webkit)   aLines.shift(); aLines.shift(); aLines.shift(); } sLines = aLines.join(((Console.settings.stackTrace.spacing) ? "\n\n" : "\n")).trim(); trace = typeof trace !== 'undefined' ? trace : true; if (typeof console != "undefined" && Console.settings.debug.enabled == true) {   for (var arg in args)  console.debug(args[arg]); if (Console.settings.stackTrace.enabled) {  var sCss = "color:red; font-weight: bold;", sTitle = "%c Stack Trace" + " ".times(70);    if (Console.settings.stackTrace.collapsed)   console.groupCollapsed(sTitle, sCss); else   console.group(sTitle, sCss);    console.debug("%c" + sLines, "color: #666666; font-style: italic;");     console.groupEnd(); } } }  Console.stackTrace = function () {   var err = new Error();   return err.stack; }   context.Console = Console; })(window); 

Check it out on GitHub (currently v1.2)! You can use it like Console.debug("Whatever"); and it will, depending on the settings in Console, print the output and a stack trace (or just simple info/nothing extra at all). Here's an example:

Updated for v1.15.
Source Link
Gabriel Nahmias
  • 996
  • 3
  • 15
  • 20
// Only global namespace. var Console = { //Settings settings: { debug: { enabled: true, showInfo: true, },  trace: true  stackTrace: {  },  stackTraceenabled: {true, collapsed: true, ignoreDebugFuncs: true, spacing: false } } }; // String formatting prototype function. if (!String.prototype.format) { String.prototype.format = function () { var s = this.toString(), args = typeof arguments[0], args = (("string" == args || "number" == args) ? arguments : arguments[0]); if (!arguments.length) return s; for (arg in args) s = s.replace(RegExp("\\{" + arg + "\\}", "gi"), args[arg]); return s; } } // String repeating prototype function. if (!String.prototype.times) { String.prototype.times = function () { var s = this.toString(), tempStr = "", times = arguments[0]; if (!arguments.length) return s; for (var i = 0; i < times; i++) tempStr += s; return tempStr; } } // Commonly used functions Console.debug = function () { var args = ((typeof arguments !== 'undefined') ? Array.prototype.slice.call(arguments, 0) : []), aLines = Console.stackTrace().split("\n"), aCurrentLine = aLines[2].replace(/(.*):/, "$1@").split("@"), sCssBlack = "color:black;", sCssFormat = "color:{0}; font-weight:bold;", sLines = ""; // Show info if the setting is true and there's no extra trace (would be kind of pointless). if (Console.settings.debug.showInfo && !Console.settings.debugstackTrace.traceenabled) { var sFunc = aCurrentLine[0], sURL = aCurrentLine[1], sLine = aCurrentLine[2]; console.info("%cOn line %c{0}%c in %c{1}%c inside the %c{2}%c function:".format(sLine, sURL, sFunc), sCssBlack, sCssFormat.format("red"), sCssBlack, sCssFormat.format("green"), sCssBlack, sCssFormat.format("blue"), sCssBlack); } // If the setting permits, get rid of the two obvious debug functions (Console.debug and Console.stackTrace). if (Console.settings.stackTrace.ignoreDebugFuncs) { aLines.shift(); aLines.shift(); } sLines = aLines.join(((Console.settings.stackTrace.spacing) ? "\n\n" : "\n")).trim(); trace = typeof trace !== 'undefined' ? trace : true; if (typeof console != "undefined" && Console.settings.debug.enabled == true) { for (var arg in args) console.debug(args[arg]); if (Console.settings.debugstackTrace.traceenabled) { var sCss = "color:red; font-weight: bold;", sTitle = "%c Stack Trace" + " ".times(70); if (Console.settings.stackTrace.collapsed) console.groupCollapsed(sTitle, sCss); else console.group(sTitle, sCss); console.debug("%c" + sLines, "color: #666666; font-style: italic;"); console.groupEnd(); } } } Console.stackTrace = function () { var err = new Error(); return err.stack; } 
// Only global namespace. var Console = { //Settings settings: { debug: { enabled: true, showInfo: true, trace: true  },  stackTrace: { collapsed: true, ignoreDebugFuncs: true, spacing: false } } }; // String formatting prototype function. if (!String.prototype.format) { String.prototype.format = function () { var s = this.toString(), args = typeof arguments[0], args = (("string" == args || "number" == args) ? arguments : arguments[0]); if (!arguments.length) return s; for (arg in args) s = s.replace(RegExp("\\{" + arg + "\\}", "gi"), args[arg]); return s; } } // String repeating prototype function. if (!String.prototype.times) { String.prototype.times = function () { var s = this.toString(), tempStr = "", times = arguments[0]; if (!arguments.length) return s; for (var i = 0; i < times; i++) tempStr += s; return tempStr; } } // Commonly used functions Console.debug = function () { var args = ((typeof arguments !== 'undefined') ? Array.prototype.slice.call(arguments, 0) : []), aLines = Console.stackTrace().split("\n"), aCurrentLine = aLines[2].replace(/(.*):/, "$1@").split("@"), sCssBlack = "color:black;", sCssFormat = "color:{0}; font-weight:bold;", sLines = ""; // Show info if the setting is true and there's no extra trace (would be kind of pointless). if (Console.settings.debug.showInfo && !Console.settings.debug.trace) { var sFunc = aCurrentLine[0], sURL = aCurrentLine[1], sLine = aCurrentLine[2]; console.info("%cOn line %c{0}%c in %c{1}%c inside the %c{2}%c function:".format(sLine, sURL, sFunc), sCssBlack, sCssFormat.format("red"), sCssBlack, sCssFormat.format("green"), sCssBlack, sCssFormat.format("blue"), sCssBlack); } // If the setting permits, get rid of the two obvious debug functions (Console.debug and Console.stackTrace). if (Console.settings.stackTrace.ignoreDebugFuncs) { aLines.shift(); aLines.shift(); } sLines = aLines.join(((Console.settings.stackTrace.spacing) ? "\n\n" : "\n")).trim(); trace = typeof trace !== 'undefined' ? trace : true; if (typeof console != "undefined" && Console.settings.debug.enabled == true) { for (var arg in args) console.debug(args[arg]); if (Console.settings.debug.trace) { var sCss = "color:red; font-weight: bold;", sTitle = "%c Stack Trace" + " ".times(70); if (Console.settings.stackTrace.collapsed) console.groupCollapsed(sTitle, sCss); else console.group(sTitle, sCss); console.debug("%c" + sLines, "color: #666666; font-style: italic;"); console.groupEnd(); } } } Console.stackTrace = function () { var err = new Error(); return err.stack; } 
// Only global namespace. var Console = { //Settings settings: { debug: { enabled: true, showInfo: true },  stackTrace: {  enabled: true, collapsed: true, ignoreDebugFuncs: true, spacing: false } } }; // String formatting prototype function. if (!String.prototype.format) { String.prototype.format = function () { var s = this.toString(), args = typeof arguments[0], args = (("string" == args || "number" == args) ? arguments : arguments[0]); if (!arguments.length) return s; for (arg in args) s = s.replace(RegExp("\\{" + arg + "\\}", "gi"), args[arg]); return s; } } // String repeating prototype function. if (!String.prototype.times) { String.prototype.times = function () { var s = this.toString(), tempStr = "", times = arguments[0]; if (!arguments.length) return s; for (var i = 0; i < times; i++) tempStr += s; return tempStr; } } // Commonly used functions Console.debug = function () { var args = ((typeof arguments !== 'undefined') ? Array.prototype.slice.call(arguments, 0) : []), aLines = Console.stackTrace().split("\n"), aCurrentLine = aLines[2].replace(/(.*):/, "$1@").split("@"), sCssBlack = "color:black;", sCssFormat = "color:{0}; font-weight:bold;", sLines = ""; // Show info if the setting is true and there's no extra trace (would be kind of pointless). if (Console.settings.debug.showInfo && !Console.settings.stackTrace.enabled) { var sFunc = aCurrentLine[0], sURL = aCurrentLine[1], sLine = aCurrentLine[2]; console.info("%cOn line %c{0}%c in %c{1}%c inside the %c{2}%c function:".format(sLine, sURL, sFunc), sCssBlack, sCssFormat.format("red"), sCssBlack, sCssFormat.format("green"), sCssBlack, sCssFormat.format("blue"), sCssBlack); } // If the setting permits, get rid of the two obvious debug functions (Console.debug and Console.stackTrace). if (Console.settings.stackTrace.ignoreDebugFuncs) { aLines.shift(); aLines.shift(); } sLines = aLines.join(((Console.settings.stackTrace.spacing) ? "\n\n" : "\n")).trim(); trace = typeof trace !== 'undefined' ? trace : true; if (typeof console != "undefined" && Console.settings.debug.enabled == true) { for (var arg in args) console.debug(args[arg]); if (Console.settings.stackTrace.enabled) { var sCss = "color:red; font-weight: bold;", sTitle = "%c Stack Trace" + " ".times(70); if (Console.settings.stackTrace.collapsed) console.groupCollapsed(sTitle, sCss); else console.group(sTitle, sCss); console.debug("%c" + sLines, "color: #666666; font-style: italic;"); console.groupEnd(); } } } Console.stackTrace = function () { var err = new Error(); return err.stack; } 
Updated for v1.1.
Source Link
Gabriel Nahmias
  • 996
  • 3
  • 15
  • 20
Loading
Added new option.
Source Link
Gabriel Nahmias
  • 996
  • 3
  • 15
  • 20
Loading
Added new option.
Source Link
Gabriel Nahmias
  • 996
  • 3
  • 15
  • 20
Loading
Source Link
Gabriel Nahmias
  • 996
  • 3
  • 15
  • 20
Loading