Last active
November 21, 2017 09:39
-
-
Save kvanbere/4c2f601f00ee2905d98f945b7c1b8ad0 to your computer and use it in GitHub Desktop.
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
// ~ 120k log op/s | |
(function() { | |
var originalArrayValueOf = Array.prototype.valueOf; | |
var logs = {}; | |
var index = 1; | |
function hasher(str) { | |
var target = Array.from(str); | |
target.length > 16 && (target = target.slice(0, 16)); // perf: avoid alloc | |
return target | |
.reduce(function(acc, i) { | |
return 0 | ((acc << 5) - acc) + i.charCodeAt(0); }, 0); | |
} | |
Array.prototype.valueOf = function() { | |
if (this.length > 0 && typeof this[0] === 'string') { | |
var bucket = hasher(this[0]); | |
switch (bucket) { | |
case 57093687: // :show | |
console.info('$' + index, | |
window['$' + index++] = Array.prototype.concat.apply([], this | |
.slice(1) | |
.map(function(x) { return (logs[hasher(x)] || []).map(JSON.parse); })) | |
); | |
break; | |
case -904836167: // :unload | |
Array.prototype.valueOf = originalArrayValueOf; | |
break; | |
case 56763003: // :help | |
[ "Valid commands are", | |
" :show, bucket [, bucket2..]", | |
" :unload" | |
].map(function(s) { console.info(s); }); | |
break; | |
default: | |
logs[bucket] === undefined && (logs[bucket] = []); | |
logs[bucket].push(JSON.stringify(this.slice(1))); | |
break; | |
} | |
} | |
return this; | |
} | |
})(); |
Author
kvanbere
commented
Nov 17, 2017
- config = { max_logs per bucket, log expiry ... }
- get/set configs
- clear a bucket or clear all logs
- return all logs
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment