UNPKG

1.7 kBJavaScriptView Raw
1'use strict';
2
3var util = require('util');
4var Console = require('console').Console;
5var supportsColor = require('color-support');
6
7var console = new Console({
8 stdout: process.stdout,
9 stderr: process.stderr,
10 colorMode: false,
11});
12
13function hasFlag(flag) {
14 return process.argv.indexOf('--' + flag) !== -1;
15}
16
17function hasColors() {
18 if (hasFlag('no-color')) {
19 return false;
20 }
21
22 if (hasFlag('color')) {
23 return true;
24 }
25
26 if (supportsColor()) {
27 return true;
28 }
29
30 return false;
31}
32
33function Timestamp() {
34 this.now = new Date();
35}
36
37Timestamp.prototype[util.inspect.custom] = function (depth, opts) {
38 var timestamp = this.now.toLocaleTimeString('en', { hour12: false });
39 return '[' + opts.stylize(timestamp, 'date') + ']';
40};
41
42function getTimestamp() {
43 return util.inspect(new Timestamp(), { colors: hasColors() });
44}
45
46function log() {
47 var time = getTimestamp();
48 process.stdout.write(time + ' ');
49 console.log.apply(console, arguments);
50 return this;
51}
52
53function info() {
54 var time = getTimestamp();
55 process.stdout.write(time + ' ');
56 console.info.apply(console, arguments);
57 return this;
58}
59
60function dir() {
61 var time = getTimestamp();
62 process.stdout.write(time + ' ');
63 console.dir.apply(console, arguments);
64 return this;
65}
66
67function warn() {
68 var time = getTimestamp();
69 process.stderr.write(time + ' ');
70 console.warn.apply(console, arguments);
71 return this;
72}
73
74function error() {
75 var time = getTimestamp();
76 process.stderr.write(time + ' ');
77 console.error.apply(console, arguments);
78 return this;
79}
80
81module.exports = log;
82module.exports.info = info;
83module.exports.dir = dir;
84module.exports.warn = warn;
85module.exports.error = error;