diff --git a/logging.common.js b/logging.common.js index 5c897c2..969d930 100644 --- a/logging.common.js +++ b/logging.common.js @@ -16,17 +16,22 @@ export const UNCOLOR = symbol.create() /* c8 ignore start */ /** - * @param {Array} args + * @param {Array} args * @return {Array} */ export const computeNoColorLoggingArgs = args => { + if (args.length === 1 && args[0]?.constructor === Function) { + args = /** @type {Array} */ (/** @type {[function]} */ (args)[0]()) + } const strBuilder = [] const logArgs = [] // try with formatting until we find something unsupported let i = 0 for (; i < args.length; i++) { const arg = args[i] - if (arg.constructor === String || arg.constructor === Number) { + if (arg === undefined) { + strBuilder.push('undefined') + } else if (arg.constructor === String || arg.constructor === Number) { strBuilder.push(arg) } else if (arg.constructor === Object) { logArgs.push(JSON.stringify(arg)) @@ -57,6 +62,9 @@ export const createModuleLogger = (_print, moduleName) => { return !doLogging ? func.nop : (...args) => { + if (args.length === 1 && args[0]?.constructor === Function) { + args = args[0]() + } const timeNow = time.getUnixTime() const timeDiff = timeNow - lastLoggingTime lastLoggingTime = timeNow @@ -65,7 +73,7 @@ export const createModuleLogger = (_print, moduleName) => { moduleName, UNCOLOR, ...args.map((arg) => { - if (arg != null && arg.constructor !== Uint8Array) { + if (arg != null && arg.constructor === Uint8Array) { arg = Array.from(arg) } const t = typeof arg diff --git a/logging.js b/logging.js index feae871..d6c77c6 100644 --- a/logging.js +++ b/logging.js @@ -32,11 +32,14 @@ const _browserStyleMap = { } /** - * @param {Array} args + * @param {Array} args * @return {Array} */ /* c8 ignore start */ const computeBrowserLoggingArgs = (args) => { + if (args.length === 1 && args[0]?.constructor === Function) { + args = /** @type {Array} */ (/** @type {[function]} */ (args)[0]()) + } const strBuilder = [] const styles = [] const currentStyle = map.create() @@ -53,6 +56,9 @@ const computeBrowserLoggingArgs = (args) => { if (style !== undefined) { currentStyle.set(style.left, style.right) } else { + if (arg === undefined) { + break + } if (arg.constructor === String || arg.constructor === Number) { const style = dom.mapToStyleString(currentStyle) if (i > 0 || style.length > 0) { @@ -195,12 +201,15 @@ const _computeLineSpans = (args) => { // try with formatting until we find something unsupported let i = 0 for (; i < args.length; i++) { - const arg = args[i] + let arg = args[i] // @ts-ignore const style = _browserStyleMap[arg] if (style !== undefined) { currentStyle.set(style.left, style.right) } else { + if (arg === undefined) { + arg = 'undefined ' + } if (arg.constructor === String || arg.constructor === Number) { // @ts-ignore const span = dom.element('span', [ diff --git a/logging.node.js b/logging.node.js index dc54c3d..63d41ec 100644 --- a/logging.node.js +++ b/logging.node.js @@ -23,10 +23,13 @@ const _nodeStyleMap = { /* c8 ignore start */ /** - * @param {Array} args - * @return {Array} + * @param {Array>} args + * @return {Array} */ const computeNodeLoggingArgs = (args) => { + if (args.length === 1 && args[0]?.constructor === Function) { + args = /** @type {Array} */ (/** @type {[function]} */ (args)[0]()) + } const strBuilder = [] const logArgs = [] // try with formatting until we find something unsupported @@ -38,7 +41,9 @@ const computeNodeLoggingArgs = (args) => { if (style !== undefined) { strBuilder.push(style) } else { - if (arg.constructor === String || arg.constructor === Number) { + if (arg === undefined) { + break + } else if (arg.constructor === String || arg.constructor === Number) { strBuilder.push(arg) } else { break @@ -68,7 +73,7 @@ const computeLoggingArgs = env.supportsColor /* c8 ignore stop */ /** - * @param {Array} args + * @param {Array} args */ export const print = (...args) => { console.log(...computeLoggingArgs(args)) diff --git a/logging.test.js b/logging.test.js index c59b91a..eecfd4e 100644 --- a/logging.test.js +++ b/logging.test.js @@ -14,4 +14,15 @@ export const testLogging = () => { log.print(log.BLUE, log.BOLD, 'number', 1) log.print(log.BLUE, log.BOLD, 'number', 1, {}, 's', 2) log.print({}, 'dtrn') + log.print(() => [log.GREEN, 'can lazyprint stuff ', log.RED, 'with formatting']) + log.print(undefined, 'supports undefined') +} + +export const testModuleLogger = () => { + // if you want to see the messages, enable logging: LOG=* npm run test --filter logging + const mlog = log.createModuleLogger('testing') + mlog('can print ', log.GREEN, 'with colors') + mlog(() => ['can lazyprint ', log.GREEN, 'with colors']) + mlog(undefined, 'supports undefined') + mlog(() => [undefined, 'supports lazyprint undefined']) } diff --git a/test.html b/test.html index 19ccb42..93e723b 100644 --- a/test.html +++ b/test.html @@ -29,6 +29,7 @@ "lib0/conditions.js": "./conditions.js", "lib0/dist/conditions.cjs": "./dist/conditions.cjs", "lib0/conditions": "./condititons.js", + "lib0/crypto/jwt": "./crypto/jwt.js", "lib0/crypto/aes-gcm": "./crypto/aes-gcm.js", "lib0/crypto/ecdsa": "./crypto/ecdsa.js", "lib0/crypto/rsa-oaep": "./crypto/rsa-oaep.js",