2023-02-09 05:49:48 +00:00
|
|
|
export = (namepsace) =>
|
|
|
|
(...args) => {
|
|
|
|
console.trace(`[${namepsace}] ` + format(...args))
|
|
|
|
} //console.debug(namepsace, ...args)
|
2022-11-21 15:18:39 +00:00
|
|
|
let formatters: any = {}
|
|
|
|
formatters.s = function (v) {
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
formatters.j = function (v) {
|
|
|
|
try {
|
|
|
|
return JSON.stringify(v)
|
|
|
|
} catch (error: any) {
|
2023-02-09 05:49:48 +00:00
|
|
|
return "[UnexpectedJSONParseError]: " + error.message
|
2022-11-21 15:18:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
2023-02-09 05:49:48 +00:00
|
|
|
* Coerce `val`.
|
|
|
|
*
|
|
|
|
* @param {Mixed} val
|
|
|
|
* @return {Mixed}
|
|
|
|
* @api private
|
|
|
|
*/
|
2022-11-21 15:18:39 +00:00
|
|
|
function coerce(val) {
|
|
|
|
if (val instanceof Error) {
|
|
|
|
return val.stack || val.message
|
|
|
|
}
|
|
|
|
return val
|
|
|
|
}
|
|
|
|
function format(...args) {
|
|
|
|
// Apply any `formatters` transformations
|
|
|
|
args[0] = coerce(args[0])
|
|
|
|
|
2023-02-09 05:49:48 +00:00
|
|
|
if (typeof args[0] !== "string") {
|
2022-11-21 15:18:39 +00:00
|
|
|
// Anything else let's inspect with %O
|
2023-02-09 05:49:48 +00:00
|
|
|
args.unshift("%O")
|
2022-11-21 15:18:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let index = 0
|
|
|
|
args[0] = args[0].replace(/%([a-zA-Z%])/g, (match, format) => {
|
|
|
|
// If we encounter an escaped % then don't increase the array index
|
2023-02-09 05:49:48 +00:00
|
|
|
if (match === "%%") {
|
|
|
|
return "%"
|
2022-11-21 15:18:39 +00:00
|
|
|
}
|
|
|
|
index++
|
|
|
|
const formatter = formatters[format]
|
2023-02-09 05:49:48 +00:00
|
|
|
if (typeof formatter === "function") {
|
2022-11-21 15:18:39 +00:00
|
|
|
const val = args[index]
|
|
|
|
match = formatter.call(format, val)
|
|
|
|
|
|
|
|
// Now we need to remove `args[index]` since it's inlined in the `format`
|
|
|
|
args.splice(index, 1)
|
|
|
|
index--
|
|
|
|
}
|
|
|
|
return match
|
|
|
|
})
|
|
|
|
return args[0]
|
|
|
|
}
|