Skip to content

Commit

Permalink
[logging] lazyprint support
Browse files Browse the repository at this point in the history
  • Loading branch information
dmonad committed Feb 28, 2024
1 parent 835c92b commit d0ee703
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 9 deletions.
14 changes: 11 additions & 3 deletions logging.common.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,22 @@ export const UNCOLOR = symbol.create()

/* c8 ignore start */
/**
* @param {Array<string|Symbol|Object|number>} args
* @param {Array<undefined|string|Symbol|Object|number|function():any>} args
* @return {Array<string|object|number>}
*/
export const computeNoColorLoggingArgs = args => {
if (args.length === 1 && args[0]?.constructor === Function) {
args = /** @type {Array<string|Symbol|Object|number>} */ (/** @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))
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
13 changes: 11 additions & 2 deletions logging.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,14 @@ const _browserStyleMap = {
}

/**
* @param {Array<string|Symbol|Object|number>} args
* @param {Array<string|Symbol|Object|number|function():any>} args
* @return {Array<string|object|number>}
*/
/* c8 ignore start */
const computeBrowserLoggingArgs = (args) => {
if (args.length === 1 && args[0]?.constructor === Function) {
args = /** @type {Array<string|Symbol|Object|number>} */ (/** @type {[function]} */ (args)[0]())
}
const strBuilder = []
const styles = []
const currentStyle = map.create()
Expand All @@ -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) {
Expand Down Expand Up @@ -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', [
Expand Down
13 changes: 9 additions & 4 deletions logging.node.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,13 @@ const _nodeStyleMap = {

/* c8 ignore start */
/**
* @param {Array<string|Symbol|Object|number>} args
* @return {Array<string|object|number>}
* @param {Array<string|undefined|Symbol|Object|number|function():Array<any>>} args
* @return {Array<string|object|number|undefined>}
*/
const computeNodeLoggingArgs = (args) => {
if (args.length === 1 && args[0]?.constructor === Function) {
args = /** @type {Array<string|Symbol|Object|number>} */ (/** @type {[function]} */ (args)[0]())
}
const strBuilder = []
const logArgs = []
// try with formatting until we find something unsupported
Expand All @@ -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
Expand Down Expand Up @@ -68,7 +73,7 @@ const computeLoggingArgs = env.supportsColor
/* c8 ignore stop */

/**
* @param {Array<string|Symbol|Object|number>} args
* @param {Array<string|Symbol|Object|number|undefined>} args
*/
export const print = (...args) => {
console.log(...computeLoggingArgs(args))
Expand Down
11 changes: 11 additions & 0 deletions logging.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'])
}
1 change: 1 addition & 0 deletions test.html
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit d0ee703

Please sign in to comment.