General, high-level question for people who work with code regularly.
I was repurposing some old, outdated Javascript code into a Chrome extension, and to help me understand it, I wrote some quick code to help (see below). I regularly put messages in my code such as dbmsg('Begin function loadDefaults()'); or dbmsg('Settings saved to storage'); and the message is sent to the console. Whenever I need to debug something where the standard console messages aren't helping, I just turn on debugging. I figure this method is pretty simple and can be ported to other languages.
What other ways do you build debugging into your programs? I'm self-taught, so I don't know the standard practices, but I'm more interested in unique ways you've found to do this sort of thing.
/** Debugging Tools
*
* Set debugMode to 1 to show error messages, 0 to hide messages.
*/
var debugMode = 1;
function dbmsg(message) {
if ( debugMode == 1 ) {
var date = new Date();
var time = date.getHours() + ':'
+ date.getMinutes() + ':'
+ date.getSeconds();
console.log('DBM - ' + time + ': ' + message);
if ( chrome.runtime.lastError ) {
console.log('DBM lastError - ' + chrome.runtime.lastError);
}
} else if ( debugMode == 0 ) {
return;
}
}
if ( debugMode == 1 ) {
console.log('Debug mode is currently ON. Each debug message is preceeded with "DBM" and the time the message was generated.');
// Listen for changes in storage
chrome.storage.onChanged.addListener(function(changes, namespace) {
for (key in changes) {
var storageChange = changes[key];
dbmsg('Storage key "%s" in namespace "%s" changed. ' +
'Old value was "%s", new value is "%s".',
key,
namespace,
storageChange.oldValue,
storageChange.newValue);
}
});
}
debugger;is usually all I use, that or set a breakpoint and examine from there, 1 step at a time. It's much more efficient then placingconsole.log's and judging by which one was fired when.debuggeris not a debugging message as much as it is a development tool. It's fine for when you want your code to stop somewhere, but is hardly the end-all.