Last active
February 14, 2016 16:31
-
-
Save jsocol/fd9658266db0de12119d to your computer and use it in GitHub Desktop.
This file contains hidden or 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
'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