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
| 'use strict'
|
| const procLog = require('proc-log')
| const { format } = require('util')
|
| // helper to emit log messages with a predefined prefix
| const logLevels = Object.keys(procLog).filter((k) => typeof procLog[k] === 'function')
| const withPrefix = (prefix) => logLevels.reduce((acc, level) => {
| acc[level] = (...args) => procLog[level](prefix, ...args)
| return acc
| }, {})
|
| // very basic ansi color generator
| const COLORS = {
| wrap: (str, colors) => {
| const codes = colors.filter(c => typeof c === 'number')
| return `\x1b[${codes.join(';')}m${str}\x1b[0m`
| },
| inverse: 7,
| fg: {
| black: 30,
| red: 31,
| green: 32,
| yellow: 33,
| blue: 34,
| magenta: 35,
| cyan: 36,
| white: 37
| },
| bg: {
| black: 40,
| red: 41,
| green: 42,
| yellow: 43,
| blue: 44,
| magenta: 45,
| cyan: 46,
| white: 47
| }
| }
|
| class Logger {
| #buffer = []
| #paused = null
| #level = null
| #stream = null
|
| // ordered from loudest to quietest
| #levels = [{
| id: 'silly',
| display: 'sill',
| style: { inverse: true }
| }, {
| id: 'verbose',
| display: 'verb',
| style: { fg: 'cyan', bg: 'black' }
| }, {
| id: 'info',
| style: { fg: 'green' }
| }, {
| id: 'http',
| style: { fg: 'green', bg: 'black' }
| }, {
| id: 'notice',
| style: { fg: 'cyan', bg: 'black' }
| }, {
| id: 'warn',
| display: 'WARN',
| style: { fg: 'black', bg: 'yellow' }
| }, {
| id: 'error',
| display: 'ERR!',
| style: { fg: 'red', bg: 'black' }
| }]
|
| constructor (stream) {
| process.on('log', (...args) => this.#onLog(...args))
| this.#levels = new Map(this.#levels.map((level, index) => [level.id, { ...level, index }]))
| this.level = 'info'
| this.stream = stream
| procLog.pause()
| }
|
| get stream () {
| return this.#stream
| }
|
| set stream (stream) {
| this.#stream = stream
| }
|
| get level () {
| return this.#levels.get(this.#level) ?? null
| }
|
| set level (level) {
| this.#level = this.#levels.get(level)?.id ?? null
| }
|
| isVisible (level) {
| return this.level?.index <= this.#levels.get(level)?.index ?? -1
| }
|
| #onLog (...args) {
| const [level] = args
|
| if (level === 'pause') {
| this.#paused = true
| return
| }
|
| if (level === 'resume') {
| this.#paused = false
| this.#buffer.forEach((b) => this.#log(...b))
| this.#buffer.length = 0
| return
| }
|
| if (this.#paused) {
| this.#buffer.push(args)
| return
| }
|
| this.#log(...args)
| }
|
| #color (str, { fg, bg, inverse }) {
| if (!this.#stream?.isTTY) {
| return str
| }
|
| return COLORS.wrap(str, [
| COLORS.fg[fg],
| COLORS.bg[bg],
| inverse && COLORS.inverse
| ])
| }
|
| #log (levelId, msgPrefix, ...args) {
| if (!this.isVisible(levelId) || typeof this.#stream?.write !== 'function') {
| return
| }
|
| const level = this.#levels.get(levelId)
|
| const prefixParts = [
| this.#color('gyp', { fg: 'white', bg: 'black' }),
| this.#color(level.display ?? level.id, level.style)
| ]
| if (msgPrefix) {
| prefixParts.push(this.#color(msgPrefix, { fg: 'magenta' }))
| }
|
| const prefix = prefixParts.join(' ').trim() + ' '
| const lines = format(...args).split(/\r?\n/).map(l => prefix + l.trim())
|
| this.#stream.write(lines.join('\n') + '\n')
| }
| }
|
| // used to suppress logs in tests
| const NULL_LOGGER = !!process.env.NODE_GYP_NULL_LOGGER
|
| module.exports = {
| logger: new Logger(NULL_LOGGER ? null : process.stderr),
| stdout: NULL_LOGGER ? () => {} : (...args) => console.log(...args),
| withPrefix,
| ...procLog
| }
|
|