File: index.js

package info (click to toggle)
node-component-consoler 2.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid, stretch
  • size: 88 kB
  • ctags: 11
  • sloc: makefile: 4; sh: 2
file content (115 lines) | stat: -rw-r--r-- 2,809 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
var debug = require('debug')('component-consoler');
var slice = Array.prototype.slice;

/**
 * Error types where we show the stack trace.
 * These are generally user errors, not "our" errors.
 **/
var showstack = [
  'ParseError',
  'SyntaxError',
  'URIError'
];

/**
 * Log message with `type` and `message` interpolated by `substitutes`.
 *
 * @param {String} type
 * @param {String} message
 * @param {...String} [substitutes]
 * @api public
**/
exports.log = function (type, message /*, substitutes */) {
  log('log', 36, type, message, slice.call(arguments, 2));
};

/**
 * Log warning message with `type` and `message` interpolated by `substitutes`.
 *
 * @param {String} type
 * @param {String} message
 * @param {...String} [substitutes]
 * @api public
**/
exports.warn = function (type, message /*, substitutes */) {
  log('warn', 33, type, message, slice.call(arguments, 2));
};

/**
 * Log error message with "error" type and `message` interpolated by `substitutes`.
 *
 * @param {String} message
 * @param {...String} [substitutes]
 * @api public
**/
exports.error = function (message /*, substitutes */) {
  log('error', 31, 'error', message, slice.call(arguments, 1));
};

/**
 * Log error message and exit with "fatal" type and `error` interpolated by `substitutes`.
 * Depending on the error type, show the stack trace.
 *
 * @param {String|Error} error
 * @param {...String} [substitutes]
 * @api public
**/
exports.fatal = function (error /*, substitutes */) {
  var message = error;

  if (error instanceof Error) {
    debug(error.stack);

    if (error.stack && ~showstack.indexOf(error.name)) {
      message = error.stack;
    } else {
      message = error.message;
    }
  }

  console.error();
  log('error', 31, 'fatal', message, slice.call(arguments, 1));
  console.error();
  process.exit(1);
};

/**
 * Log message in console `method` with `color`, `type`, `message`, `substitutes`.
 *
 * @param {String} method
 * @param {Number} color
 * @param {String} type
 * @param {String} message
 * @param {String[]} substitues
 * @api private
**/
function log (method, color, type, message, substitutes) {
  console[method].apply(console, [stylize(type, message, color)].concat(substitutes));
}

/**
 * Stylize message, use `color` for `type` before `message`.
 *
 * @param {String} type
 * @param {String} message
 * @param {Number} color
 * @return {String}
 * @api private
**/
function stylize (type, message, color) {
  return '  \033[' + color + 'm' + pad(type) + '\033[m : \033[90m' + message + '\033[m';
}

/**
 * Add whitespace indentation before text.
 *
 * @param {String} text
 * @return {String}
 * @api private
**/
function pad (text) {
  var width = 10;
  var length = Math.max(0, width - text.length);
  var space = Array(length + 1).join(' ');
  return space + text;
}