-
Notifications
You must be signed in to change notification settings - Fork 8
/
ololog.js
219 lines (143 loc) · 8.75 KB
/
ololog.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
"use strict";
/* ------------------------------------------------------------------------ */
const O = Object
, StackTracey = require ('stacktracey')
, ansi = require ('ansicolor')
, bullet = require ('string.bullet')
, pipez = require ('pipez')
/* ------------------------------------------------------------------------ */
const stringify = require ('string.ify').configure ({
formatter (x, stringify) {
if ((x instanceof Error) && !(typeof Symbol !== 'undefined' && x[Symbol.for ('String.ify')])) {
if (stringify.state.depth > 0) return `<Error: ${x.message}>` // prevents unwanted pretty printing for Errors that are properties of complex objects
const indent = ' '
, why = stringify.limit ((x.message || '').replace (/\r|\n/g, '').trim (), stringify.state.maxErrorMessageLength || 120)
, stack = new StackTracey (x).withSources ().asTable ()
, stackIndented = stack.split ('\n').map (x => indent + x).join ('\n')
, isAssertion = ('actual' in x) && ('expected' in x)
, type = x.constructor.name || 'Error'
if (isAssertion) {
const str = stringify.configure ({ maxStringLength: Number.MAX_VALUE, maxDepth: 8 })
let actual = bullet (indent + 'actual: ', str (x.actual))
, expected = bullet (indent + 'expected: ', str (x.expected))
if ((actual.split ('\n').length > 1) || (expected.split ('\n').length > 1)) // if multiline actual/expected, need extra whitespace inbetween
actual += '\n'
return `[${type}] ${why}\n\n${ansi.red (actual)}\n${ansi.green (expected)}\n\n${stackIndented}\n`
} else {
return `[${type}] ${why}\n\n${stackIndented}\n`
}
}
}
})
/* ------------------------------------------------------------------------ */
const { isBlank, blank } = require ('printable-characters')
, changeLastNonemptyLine = (lines, fn) => {
for (let i = lines.length - 1; i >= 0; i--) {
if ((i === 0) || !isBlank (lines[i])) {
lines[i] = fn (lines[i])
break;
}
}
return lines
}
/* ------------------------------------------------------------------------ */
const log = pipez ({
/* ------------------------------------------------------------------------ */
stringify: (args, cfg, print = cfg.print || stringify.configure (cfg)) => args.map (arg => (typeof arg === 'string') ? arg : print (arg)),
trim: (tokens, { max = undefined }) => !max ? tokens : tokens.map (t => stringify.limit (t, max)),
lines: (tokens, { linebreak = '\n' }) => {
let lines = [[]]
let leftPad = []
for (const t of tokens) {
const [first, ...rest] = t.split (linebreak)
lines[lines.length - 1].push (first)
lines = [...lines, ...rest.map (t => t ? [...leftPad, t] : [])]
const pad = blank (!rest.length ? t : rest[rest.length - 1])
if (pad) { leftPad.push (pad) }
}
return lines
},
concat: (lines, { separator = ' ' }) => lines.map (tokens => tokens.join (separator)),
indent: (lines, { level = 0, pattern = '\t' }) => lines.map (line => pattern.repeat (level) + line),
tag: (lines, { level = '',
levelColor = {
'info': ansi.cyan,
'warn': ansi.yellow,
'debug': ansi.blue,
'error': ansi.bright.red } }) => bullet ((levelColor[level] || (s => s)) (level.toUpperCase ().padStart (6) + '\t'), lines),
time: (lines, { when = new Date (),
format = 'locale',
locale = [],
options = {},
print = when => ansi.darkGray (
((format === 'iso') ? when.toISOString () :
((format === 'locale') ? when.toLocaleString (locale, options) :
((format === 'utc') ? when.toUTCString () :
when.toString ())))) + '\t' }) => bullet (print (when), lines),
locate: (lines, {
shift = 0,
where = (new StackTracey ().clean ().at (1 + shift)),
join = ((a, sep, b) => (a && b) ? (a + sep + b) : (a || b)),
print = ({ calleeShort, fileName = [], line = [] }) => ansi.darkGray ('(' + join (calleeShort, ' @ ', join (fileName, ':', line)) + ')')
}) => changeLastNonemptyLine (lines, line => join (line, ' ', print (where))),
join: (lines, { linebreak = '\n' }) => lines.join (linebreak),
render: (text, {
engine = ((typeof window !== 'undefined') && (window.window === window) && window.navigator)
? (navigator.userAgent.indexOf ('Chrome') >= 0)
? 'chrome'
: 'generic'
: 'ansi',
engines = { /* configurable */ },
consoleMethod = 'log',
defaults = {
ansi: s => console[consoleMethod] (s),
chrome: s => console[consoleMethod] (...ansi.parse (s).asChromeConsoleLogArguments),
generic: s => console[consoleMethod] (ansi.strip (s))
}
}) => ((text && O.assign (defaults, engines)[engine] (text), text)),
returnValue: (__, { initialArguments: [firstArgument] }) => firstArgument
/* ------------------------------------------------------------------------ */
}).configure ({
time: false, // disables some steps (until enabled back explicitly)
tag: false
/* ------------------------------------------------------------------------ */
}).methods ({
get noop () { return pipez ({ returnValue: args => args[0] }).methods (this.methods_) },
get null () { return this.noop }, // LEGACY, DEPRECATED (left here for backward compatibility)
indent (level) { return this.configure ({ indent: { level: level }}) },
get error () { return this.configure ({ tag: { level: 'error' }, render: { consoleMethod: 'error' } }) },
get warn () { return this.configure ({ tag: { level: 'warn' }, render: { consoleMethod: 'warn' } }) },
get info () { return this.configure ({ tag: { level: 'info' }, render: { consoleMethod: 'info' } }) },
get debug () { return this.configure ({ tag: { level: 'debug' }, render: { consoleMethod: 'debug' } }) },
maxArrayLength (n) { return this.configure ({ stringify: { maxArrayLength: n } }) },
maxObjectLength (n) { return this.configure ({ stringify: { maxObjectLength: n } }) },
maxDepth (n) { return this.configure ({ stringify: { maxDepth: n } }) },
maxLength (n) { return this.configure ({ stringify: { maxLength: n } }) },
get unlimited () { return this.configure ({ stringify: { maxStringLength: Number.MAX_VALUE,
maxObjectLength: Number.MAX_VALUE,
maxArrayLength: Number.MAX_VALUE,
maxDepth: Number.MAX_VALUE,
maxErrorMessageLength: Number.MAX_VALUE } }) },
get noPretty () { return this.configure ({ stringify: { pretty: false } }) },
get noFancy () { return this.configure ({ stringify: { fancy: false } }) },
get noRightAlignKeys () { return this.configure ({ stringify: { rightAlignKeys: false } }) },
get noLocate () { return this.configure ({ locate: false }) },
precision (n) { return this.configure ({ stringify: { precision: n } }) },
get serialize () { return this.before ('render') },
get deserialize () { return this.from ('render') },
newline () { return this.from ('join')(['']) },
handleNodeErrors () {
process.on ('uncaughtException', e => { this.bright.red.error.noLocate (e); process.exit (1) })
process.on ('unhandledRejection', e => { this.bright.red.error.noLocate (e); process.exit (1) })
return this
}
})
/* ------------------------------------------------------------------------ */
ansi.names.forEach (color => {
log.methods ({
get [color] () { return this.configure ({ 'concat+': lines => lines.map (ansi[color]) }) }
})
})
/* ------------------------------------------------------------------------ */
module.exports = log
/* ------------------------------------------------------------------------ */