wo-core-coco/cc.js
2024-02-05 09:50:07 +08:00

86 lines
2.7 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

const util = require('util')
function deepStringify (args = []) {
if (globalThis.process?.release?.name === 'node') {
return globalThis.wo?.envar?.logDeep ? util.inspect(args, { showHidden: false, depth: null, colors: typeof globalThis.wo?.envar?.logColor === 'undefined' ? true : globalThis.wo?.envar?.logColor }) : args // JSON.stringify(args, null, 2) // in nodejs console, object only shows children of depth < 3 by default. 如果要完整数据,就要进行扩展。
} else if (globalThis.uni && globalThis.UniApp) {
// 可再分为 web 和 app通过 #ifdef 或 globalThis.window/location 判断
return globalThis.wo?.envar?.logDeep ? util.inspect(args, { showHidden: false, depth: null, colors: typeof globalThis.wo?.envar?.logColor === 'undefined' ? true : globalThis.wo?.envar?.logColor }) : args // in browser console, object is expandable by default.
} else if (globalThis.uniCloud) {
return args
} else {
return args
}
}
function routeNow () {
if (globalThis.process?.release?.name === 'node') {
return {} // { _from: routeNow.caller.name } // new Error().stack?.split('\n') , .match(/at (.*?) /g)[2]
} else if (globalThis.uni && globalThis.UniApp) {
return { _from: globalThis.getCurrentPages?.()?.pop?.()?.route?.substring?.(6) || 'VueApp' }
} else {
return {}
}
}
function expandArgs (args) {
if (args.length > 1) {
return { _args: args }
} else if (typeof args[0] === 'object') {
return args[0]
} else {
return { _arg: args[0] }
}
}
module.exports =
{
cclog (...args) {
console.log(deepStringify({
_at: new Date().toJSON(),
_type: 'CLOG', // arguments.callee.name doesn't work in nodejs strict mode
...routeNow(),
...expandArgs(args)
}))
globalThis.UniApp || console.log(',')
},
ccinfo (...args) {
console.info(deepStringify({
_at: new Date().toJSON(),
_type: 'CINFO',
...routeNow(),
...expandArgs(args)
}))
globalThis.UniApp || console.log(',')
},
ccwarn (...args) {
console.warn(deepStringify({
_at: new Date().toJSON(),
_type: 'CWARN',
...routeNow(),
...expandArgs(args)
}))
globalThis.UniApp || console.log(',')
},
ccerror (...args) {
console.error(deepStringify({
_at: new Date().toJSON(),
_type: 'CERROR',
...routeNow(),
...expandArgs(args)
}))
globalThis.UniApp || console.log(',')
},
ccdebug (...args) {
if (process.env.NODE_ENV !== 'production') {
console.debug(deepStringify({
_at: new Date().toJSON(),
_type: 'CDEBUG',
...routeNow(),
...expandArgs(args)
}))
globalThis.UniApp || console.log(',')
}
}
}