Skip to content

Commit

Permalink
Add console.warn() and formatted logging
Browse files Browse the repository at this point in the history
  • Loading branch information
kasperisager committed Apr 25, 2024
1 parent 50c2cb3 commit 1c5b0c2
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 21 deletions.
33 changes: 14 additions & 19 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const inspect = require('bare-inspect')
const { formatWithOptions } = require('bare-format')
const hrtime = require('bare-hrtime')

module.exports = class Console {
Expand All @@ -11,6 +11,7 @@ module.exports = class Console {

if (opts.bind) {
this.log = this.log.bind(this)
this.warn = this.warn.bind(this)
this.error = this.error.bind(this)
this.time = this.time.bind(this)
this.timeEnd = this.timeEnd.bind(this)
Expand All @@ -19,11 +20,15 @@ module.exports = class Console {
}

log (...args) {
this._stdout.write(formatArgs(args, { colors: this._colors }) + '\n')
this._stdout.write(formatWithOptions({ colors: this._colors }, ...args) + '\n')
}

warn (...args) {
this._stderr.write(formatWithOptions({ colors: this._colors }, ...args) + '\n')
}

error (...args) {
this._stderr.write(formatArgs(args, { colors: this._colors }) + '\n')
this._stderr.write(formatWithOptions({ colors: this._colors }, ...args) + '\n')
}

time (label = 'default') {
Expand Down Expand Up @@ -52,26 +57,16 @@ module.exports = class Console {
}

trace (...args) {
const err = { name: 'Trace', message: formatArgs(args, { colors: this._colors }) }
Error.captureStackTrace(err, this.trace)
const err = { name: 'Trace', message: formatWithOptions({ colors: this._colors }, ...args) }

if (Error.captureStackTrace) {
Error.captureStackTrace(err, this.trace)
}

this.error(err.stack)
}
}

function adaptStream (stream) {
return typeof stream === 'function' ? { write: stream } : stream
}

function formatArgs (args, opts) {
let out = ''
let first = true

for (const arg of args) {
if (first) first = false
else out += ' '

out += typeof arg === 'string' ? arg : inspect(arg, opts)
}

return out
}
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
"homepage": "https://github.com/holepunchto/bare-console#readme",
"dependencies": {
"bare-events": "^2.2.0",
"bare-hrtime": "^2.0.0",
"bare-inspect": "^3.0.0"
"bare-format": "^1.0.0",
"bare-hrtime": "^2.0.0"
},
"devDependencies": {
"bare-stream": "^1.0.0",
Expand Down
42 changes: 42 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,48 @@ test('log', (t) => {
console.log('hello')
})

test('log with format', (t) => {
t.plan(1)

const stdout = new Writable({
write (data, cb) {
t.is(data, 'hello world\n')
cb(null)
}
})

const stderr = new Writable({
write () {
t.fail()
}
})

const console = new Console({ stdout, stderr })

console.log('hello %s', 'world')
})

test('warn', (t) => {
t.plan(1)

const stdout = new Writable({
write () {
t.fail()
}
})

const stderr = new Writable({
write (data, cb) {
t.is(data, 'hello\n')
cb(null)
}
})

const console = new Console({ stdout, stderr })

console.warn('hello')
})

test('error', (t) => {
t.plan(1)

Expand Down

0 comments on commit 1c5b0c2

Please sign in to comment.