Created
June 24, 2015 20:01
-
-
Save sehrgut/db5532a40cd4b47b93d1 to your computer and use it in GitHub Desktop.
console.log() with message history
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
console.log = (function (oldlog) { | |
var console_log = oldlog.bind(console); | |
var messages = []; | |
var newlog = function (msg) { | |
messages.push(msg); | |
console_log(msg); | |
}; | |
newlog.read = function () { return messages.slice(); }; | |
newlog.clear = function () { messages.length = 0; }; | |
newlog.lines = function (delim) { | |
delim = typeof delim !== 'undefined' ? delim : "\n"; | |
return messages.join(delim); | |
}; | |
newlog.detach = function () { console.log = oldlog; }; | |
return newlog; | |
})(console.log); | |
//todo: manager object allowing attach/detach without re-evaluating the closure. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment