Created
July 22, 2013 17:27
-
-
Save salamanders/6055792 to your computer and use it in GitHub Desktop.
In phonegap, buffer the outputs to console.log to help hunting down errors that would otherwise be missed.
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
var console_buffer = { | |
isBuffering: false, | |
logBuffer: [], | |
preservedConsoleLog: null, | |
start: function() { | |
"use strict"; | |
console.log('INFO: log buffer starting'); // will be lost | |
console_buffer.preservedConsoleLog = console.log; | |
console_buffer.isBuffering = true; | |
console.log = function() { | |
// needs to be from within scope | |
console_buffer.preservedConsoleLog.apply(console, arguments); | |
if (console_buffer.isBuffering) { | |
console_buffer.logBuffer.push(arguments); | |
} | |
}; | |
console.log('INFO: log buffer started'); | |
}, | |
empty_buffer: function() { | |
console_buffer.logBuffer = []; | |
}, | |
end: function() { | |
// TODO: Auto-end with a timeout in case never called. | |
console_buffer.isBuffering = false; | |
for (var i = 0; i < console_buffer.logBuffer.length; i++) { | |
console.log('BUFFERED:' + console_buffer.logBuffer[i][0]); | |
} | |
console_buffer.empty_buffer(); | |
console.log('INFO: log buffer ended'); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment