Skip to content

Instantly share code, notes, and snippets.

@fluffywaffles
Last active August 29, 2015 14:27
Show Gist options
  • Save fluffywaffles/00b288e765d01c57c2ed to your computer and use it in GitHub Desktop.
Save fluffywaffles/00b288e765d01c57c2ed to your computer and use it in GitHub Desktop.
/** USAGE(jordan, 8/19/15):
*
* // NOTE: StackLogger combines each stacked message with newlines.
*
* var _error = ErrorStackLogger('My Error Generating Class has an Error:');
* // or, equivalently:
* var _error = StackLogger('error', 'My Error Generating Class has an Error:');
*
* _error('Bad input.') // => 'My Error Generating Class has an Error:'
* // ' Bad input.'
*
* method () {
* // StackLogger.push and StackLogger.pop are immutable updates, so _error is unaltered.
* var _methodError = _error.push('Method: [MyErrorGeneratingClass::method]');
* ...
* _methodError('That\'s no good. Stop that.');
* // => 'My Error Generating Class has an Error:'
* // ' Method: [MyErrorGeneratingClass::method]'
* // ' That\'s no good. Stop that.'
* _error('I am still here.')
* // => 'My Error Generating Class has an Error:'
* // ' I am still here.'
* }
*
* ...
* // Suppose _error isn't defined, but _methodError is.
* var _error = _methodError.pop();
*
* _error('Hah, you suck!') // => 'My Error Generating Class has an Error: \nHah, you suck!'
*
* // NOTE: pop() will not remove the immutable topmost log. For _error, that's 'My Error...' etc.
* _error = _error.pop()
* _error('Hah, you suck!') // => (same as above.)
* // This is because the use case for StackLogger is expected to be in a class, where you always
* // want to identify the "top level namespace," so to speak, from which the
* // message originates.
*
* // If you want to have no top level immutable message, just don't pass one into the
* // bare constuctor.
* var noImmutableTopLog = StackLogger('log');
* noImmutableTopLog('Hello.') // => 'Hello.'
*
**/
var StackLogger = function (logLevel, topLog, stack) {
var messageStack = (stack instanceof Array && stack.length) ? stack : [ ];
var logger = function (logs) {
console[logLevel].call(console, logs.join('\n'));
}
var MessageStackAccumulator = function printAccumulatedMessages (finalMessage) {
var logs = messageStack.slice();
logs.shift(topLog);
if (!!finalMessage) logs.push(finalMessage);
logger(logs);
};
var immutableStackUpdate = function (method) {
var newMessageStack = messageStack.slice();
var args = [].slice.call(arguments, 1);
newMessageStack[method].apply(newMessageStack, args);
return StackLogger(logLevel, topLog, newMessageStack);
}
MessageStackAccumulator.push = function accumulateMessageAndReturnNewLogger (message) {
return immutableStackUpdate('push', message);
}
MessageStackAccumulator.pop = function removeNewestMessageAndReturnNewLogger () {
return immutableStackUpdate('pop');
}
return MessageStackAccumulator;
}
ErrorStackLogger = function (topLog) {
return StackLogger('error', topLog);
}
WarnStackLogger = function (topLog) {
return StackLogger('warn', topLog);
}
InfoStackLogger = function (topLog) {
return StackLogger('info', topLog);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment