Skip to content

Instantly share code, notes, and snippets.

@jsocol
Last active February 14, 2016 16:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jsocol/fd9658266db0de12119d to your computer and use it in GitHub Desktop.
Save jsocol/fd9658266db0de12119d to your computer and use it in GitHub Desktop.
'use strict';
var format = require('util').format;
function Logger() {
}
Logger.prototype.log = function $$Logger_log(msg) {
var context = this.getContext();
console.log(format('%s:%s:%s(%s): %s', context.pathname, context.lno, context.col, context.func, msg));
}
Logger.prototype.getContext = function $$Logger_getContext() {
try {
throw new Error();
} catch (e) {
var frames = e.stack.split('\n');
for (var i = 0; i < frames.length; i++) {
var frame = frames[i].trim();
if (frame.indexOf('$$Logger') !== -1 || frame.indexOf('at') === -1) {
continue;
}
var groups = /at (\S+) .*\((.*):(\d+):(\d+)\)/.exec(frame);
return {
func: groups[1] || '',
pathname: groups[2],
lno: groups[3],
col: groups[4]
};
}
return {
func: '',
pathname: '',
lno: '',
col: ''
};
}
}
function g() {
var log = new Logger();
log.log('hi there');
}
g();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment