This commit is contained in:
Luk
2026-02-07 11:32:54 +08:00
parent 420f7f1701
commit f8477d46a0
2 changed files with 86 additions and 51 deletions

123
cc.js
View File

@@ -1,24 +1,29 @@
const util = require('util') const util = require('util')
const my = {
inUniapp: typeof globalThis.getApp === 'function' && typeof globalThis.uni === 'object',
}
function deepStringify (args = []) { function deepStringify (args = []) {
// 在 nodejs 的 prod 环境,上色导致 pm2 log 里有特殊的颜色字符。在浏览器里prod 时上色无效,反而导致console输出特殊颜色字符。
if (globalThis.process?.release?.name === 'node') { if (globalThis.process?.release?.name === 'node') {
// in nodejs console, object only shows children of depth <= 3 by default. 如果要完整数据,就要进行扩展。 // in nodejs console, object only shows children of depth <= 3 by default. 如果要完整数据,就要进行扩展。
return globalThis.wo?.envar?.logDeep return globalThis.wo?.envar?.logDeep
? util.inspect(args, { ? util.inspect(args, {
showHidden: false, showHidden: false,
depth: null, depth: null,
colors: typeof globalThis.wo?.envar?.logColor === 'undefined' ? true : globalThis.wo?.envar?.logColor colors: process.env.NODE_ENV === 'production' ? false : typeof globalThis.wo?.envar?.logColor === 'undefined' ? true : globalThis.wo?.envar?.logColor,
}) })
: args // JSON.stringify(args, null, 2) : args // JSON.stringify(args, null, 2)
} else if (globalThis.uni && globalThis.UniApp) { } else if (my.inUniapp) {
// 可再分为 web 和 app通过 #ifdef 或 globalThis.window/location 判断 // 可再分为 web 和 app通过 #ifdef 或 globalThis.window/location 判断
// in browser console, object is expandable by default. // in browser console, object is expandable by default.
return globalThis.wo?.envar?.logDeep return globalThis.wo?.envar?.logDeep
? util.inspect(args, { ? util.inspect(args, {
showHidden: false, showHidden: false,
depth: null, depth: null,
colors: typeof globalThis.wo?.envar?.logColor === 'undefined' ? true : globalThis.wo?.envar?.logColor colors: process.env.NODE_ENV === 'production' ? false : typeof globalThis.wo?.envar?.logColor === 'undefined' ? true : globalThis.wo?.envar?.logColor,
}) })
: args : args
} else if (globalThis.uniCloud) { } else if (globalThis.uniCloud) {
return args return args
@@ -29,9 +34,10 @@ function deepStringify (args = []) {
function fromPath () { function fromPath () {
if (globalThis.process?.release?.name === 'node') { if (globalThis.process?.release?.name === 'node') {
return { _from: new Error().stack?.match?.(/\bat (.*?) /g)?.[2] } // { _from: fromPath.caller.name } // new Error().stack?.split('\n') , .match(/at (.*?) /g)[2] // new Error().stack?.split('\n') , .match(/at (.*?) /g)[2] 其中 0 是本函数名本身1 是 cclog 等
return { _from: new Error().stack?.match?.(/\bat (.*?) /g)?.[2] }
} else if (globalThis.uni && globalThis.UniApp) { } else if (globalThis.uni && globalThis.UniApp) {
return { _from: globalThis.getCurrentPages?.()?.pop?.()?.route?.substring?.(6) || 'VueApp' } return { _from: globalThis.getCurrentPages?.()?.pop?.()?.route?.substring?.(6) || 'App' }
} else { } else {
return {} return {}
} }
@@ -47,53 +53,70 @@ function expandArgs (args) {
} }
} }
module.exports = module.exports = {
{
cclog (...args) { cclog (...args) {
console.log(deepStringify({ if (!my.inUniapp || process.env.NODE_ENV !== 'production') {
_at: new Date().toJSON(), console.log(
_type: 'CLOG', // arguments.callee.name, // TypeError: 'caller', 'callee', and 'arguments' properties may not be accessed on strict mode functions or the arguments objects for calls to them deepStringify({
...fromPath(), _at: new Date().toJSON(),
...expandArgs(args) _type: 'CLOG', // arguments.callee.name, // TypeError: 'caller', 'callee', and 'arguments' properties may not be accessed on strict mode functions or the arguments objects for calls to them
})) ...fromPath(),
globalThis.UniApp || console.log(',') ...expandArgs(args),
})
)
my.inUniapp || console.log(',')
}
}, },
ccinfo (...args) { ccinfo (...args) {
console.info(deepStringify({ if (!my.inUniapp || process.env.NODE_ENV !== 'production') {
_at: new Date().toJSON(), console.info(
_type: 'CINFO', deepStringify({
...fromPath(), _at: new Date().toJSON(),
...expandArgs(args) _type: 'CINFO',
})) ...fromPath(),
globalThis.UniApp || console.log(',') ...expandArgs(args),
})
)
my.inUniapp || console.log(',')
}
}, },
ccwarn (...args) { ccwarn (...args) {
console.warn(deepStringify({ if (!my.inUniapp || process.env.NODE_ENV !== 'production') {
_at: new Date().toJSON(), console.warn(
_type: 'CWARN', deepStringify({
...fromPath(), _at: new Date().toJSON(),
...expandArgs(args) _type: 'CWARN',
})) ...fromPath(),
globalThis.UniApp || console.log(',') ...expandArgs(args),
})
)
my.inUniapp || console.log(',')
}
}, },
ccerror (...args) { ccerror (...args) {
console.error(deepStringify({ if (!my.inUniapp || process.env.NODE_ENV !== 'production') {
_at: new Date().toJSON(), console.error(
_type: 'CERROR', deepStringify({
...fromPath(), _at: new Date().toJSON(),
...expandArgs(args) _type: 'CERROR',
})) ...fromPath(),
globalThis.UniApp || console.log(',') ...expandArgs(args),
})
)
my.inUniapp || console.log(',')
}
}, },
ccdebug (...args) { ccdebug (...args) {
if (process.env.NODE_ENV !== 'production') { if (!my.inUniapp || process.env.NODE_ENV !== 'production') {
console.debug(deepStringify({ console.debug(
_at: new Date().toJSON(), deepStringify({
_type: 'CDEBUG', _at: new Date().toJSON(),
...fromPath(), _type: 'CDEBUG',
...expandArgs(args) ...fromPath(),
})) ...expandArgs(args),
globalThis.UniApp || console.log(',') })
)
my.inUniapp || console.log(',')
} }
} },
} }

View File

@@ -22,6 +22,12 @@
*.nosf/ *.nosf/
*.nosf.*/ *.nosf.*/
## everything 'git pull or fetch' will update `.git/FETCH_HEAD`, even if the content doesn't change. To avoid too many useless updates of this file in Seafile history:
FETCH_HEAD
*/FETCH_HEAD
.Trash/
.DS_Store .DS_Store
*/.DS_Store */.DS_Store
@@ -48,12 +54,18 @@ _desktop.ini
node_modules/ node_modules/
*/node_modules/ */node_modules/
package-lock.json package-lock.json
*/package-lock.json
pages4loader.json5 pages4loader.json5
*/pages4loader.json5
.deploy_git/ .deploy_git/
*/.deploy_git/ */.deploy_git/
# next.js 项目
.next/
*/.next/
# HBuilder 目录 # HBuilder 目录
unpackage/ unpackage/
*/unpackage/ */unpackage/