/
index.js
99 lines (81 loc) · 3.2 KB
/
index.js
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
var colors = require('./colors.js'),
util = require('util')
require('./date.format.js')
var Logger = function (context, contextColor) {
Logger.padding = Math.max(Logger.padding, context.length)
this.contextColorCode = contextColor ? colors.bold[contextColor] : colors.bold.white
this.doPadding = function () {
var ct = context
while (ct.length < Logger.padding) {
ct = ct.length % 2 ? ct + ' ' : ' ' + ct
}
context = ct
this.context = ct
}
this.prepareText = function (text) {
this.doPadding()
if (typeof text != 'string') text = util.inspect(text, false, 0)
return text
}
this.log = function (text, textColor) {
var last = Logger.lastUsed
!!last && !!last.id && !this.equals(last) && util.puts('') //TODO DIRTY
this.rawPrint(colorize(this.prepareText(text), textColor))
Logger.lastUsed = this
return this
}
var colorize = function (text, color) { return color + text + colors.reset }
this.header = function () {
return block(context, this.contextColorCode) +
(this.appendDateTime ? block(new Date().format(this.dateTimeFormat), colors.bold[this.dateTimeColor]) : '')
}
var block = function (text, color) { return '[' + colorize(text, color) + '] ' }
this.rawPrint = function (text) {
util.puts(this.header() + text)
}
var that = this
this.info = function (text) { return that.log(text, colors.white) }
this.debug = function (text) { return that.showDebug ? that.log(text, colors.cyan) : that }
this.err =
this.error = function (text) { return that.log(text, colors.bold.red) }
this.warning =
this.warn = function (text) { return that.log(text, colors.bold.yellow) }
this.subcontext = function (id) {
var mama = new Logger(context, contextColor)
mama.rawPrint = function (text) {
util.print(this.header() + colorize('|' + id + '|', colors.bold.white) + ' ' + text)
}
return mama
}
this.buffered = function (id) {
if (typeof(id) == 'undefined') id = 'buffer'
var mama = new Logger(context, contextColor)
mama.id = id
mama.line = function() {
Logger.lastUsed = {id: ' '}
return this
}
mama.rawPrint = function (text) {
var last = Logger.lastUsed
if (!this.equals(last)) {
util.print(this.header() + colorize('|' + id + '|', colors.bold.white) + ' ')
}
util.print(text + ' ')
}
return mama
}
this.equals = function (logger) {
return !!logger && context == logger.context && (!!this.id == !!logger.id) && (!this.id || this.id == logger.id)
}
}
Logger.prototype = Logger.default = module.exports.default = {
showDebug: false,
appendDateTime: false,
dateTimeFormat: 'HH:MM:ss dd.mm',
dateTimeColor: 'cyan'
}
Logger.padding = 0
Logger.lastUsed = null
module.exports.colors = colors
module.exports.forContext = function (context, color) { return new Logger(context, color) }
module.exports.setPadding = function (padding) { Logger.padding = Math.max(padding, Logger.padding) }