UNPKG

1.32 kBJavaScriptView Raw
1'use strict'
2
3var chalk = require('chalk')
4var util = require('util')
5var map = require('../lib/common/array/map')
6
7var newLine = '\n'
8
9exports.warn = function (x, y, z) { return helper('yellow', x, y, z) }
10exports.log = function (x, y, z) { return helper('green', x, y, z) }
11exports.info = function (x, y, z) { return helper('blue', x, y, z) }
12exports.debug = function (x, y, z) { return helper('cyan', x, y, z) }
13
14exports.error = function () {
15 var i, j, argument, output, lines, error, trace
16 for (i = 0, j = arguments.length; i < j; i++) {
17 argument = arguments[i]
18 if (argument instanceof Error) {
19 output = argument.stack || argument.name
20 lines = output.split(newLine)
21 error = lines[0]
22 trace = map(lines.slice(1), formatLine).join(newLine)
23
24 helper('red', error)
25 if (trace.length) helper('gray', trace)
26
27 continue
28 }
29 helper('red', argument)
30 }
31}
32
33function formatLine (line) {
34 return ' ' + line.trim()
35}
36
37function helper (color, x, y, z) {
38 var output = map([x, y, z], function (argument) {
39 return typeof argument === 'object' ?
40 util.inspect(argument, { depth: null }) :
41 argument
42 }).join(' ')
43
44 map(output.split(newLine), function (line) {
45 if (line.trim())
46 console.log('# ' + chalk[color](line)) // eslint-disable-line
47 })
48}