Last active
August 29, 2015 14:22
-
-
Save huttj/6a3be86a97b87b65a4ae to your computer and use it in GitHub Desktop.
Log Module
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
var cwd = process.cwd(); | |
var reg = new RegExp(/([^()]+)/); | |
var colors = { | |
black : '0', | |
red : '1', | |
green : '2', | |
yellow : '3', | |
blue : '4', | |
magenta : '5', | |
cyan : '6', | |
white : '7', | |
} | |
function color(str, color, intense) { | |
return [ | |
'\x1b[3', | |
(colors[color] || colors.white), | |
(intense ? ';1' : ''), | |
'm', | |
str, | |
'\x1b[0m' | |
].join(''); | |
} | |
function spaces(n) { | |
if (n<1) return ''; | |
var s = ''; | |
while (n--) { | |
s += ' '; | |
} | |
return s; | |
} | |
function pad(str, length) { | |
return str + spaces(length - str.length); | |
} | |
function log() { | |
var stack = new Error().stack; | |
// The second line of Error.stack is this log function; the third is the | |
// calling function. Get that line and pull out the calling function's | |
// name and path | |
var line = stack.split('\n')[2].match(/\s+at\s+(.+)/)[1].split(' '); | |
// The calling function's name will be the first part of the line | |
var fnName = line[1] ? line[0] : '<anonymous>'; | |
// the path will be the last part of the line | |
var fileName = line[line.length - 1].match(reg)[1] | |
// Split on cwd to get the path minus the current working directory | |
.split(cwd).join('').substr(1); | |
// Add spaces to the end of each, to simulate table structure | |
stack = pad(color(fnName, 'magenta'), 30) + pad(color(fileName, 'yellow'), 30); | |
// We're going to apply console.log, so we need to insert the stack into the arguments | |
var args = Array.prototype.slice.call(arguments); | |
args.unshift(stack); | |
console.log.apply(console, args); | |
} | |
module.exports = log; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment