2018-08-16 22:30:59 +00:00
|
|
|
/*
|
2024-01-13 20:03:36 +00:00
|
|
|
* Copyright (c) 2010-2024 Tim Düsterhus.
|
2018-08-16 22:30:59 +00:00
|
|
|
*
|
|
|
|
* Use of this software is governed by the Business Source License
|
|
|
|
* included in the LICENSE file.
|
|
|
|
*
|
2024-03-04 19:42:52 +00:00
|
|
|
* Change Date: 2028-03-14
|
2018-08-16 22:30:59 +00:00
|
|
|
*
|
|
|
|
* On the date above, in accordance with the Business Source
|
|
|
|
* License, use of this software will be governed by version 2
|
|
|
|
* or later of the General Public License.
|
|
|
|
*/
|
|
|
|
|
2020-11-01 16:41:19 +00:00
|
|
|
define([], function () {
|
|
|
|
'use strict'
|
2018-08-16 22:30:59 +00:00
|
|
|
|
|
|
|
const start = Date.now()
|
2020-11-01 16:41:19 +00:00
|
|
|
let last = start
|
2018-08-16 22:30:59 +00:00
|
|
|
|
|
|
|
const group = function () {
|
|
|
|
if (window.console.group) window.console.group()
|
|
|
|
}
|
|
|
|
|
|
|
|
const groupCollapsed = function () {
|
|
|
|
if (window.console.groupCollapsed) window.console.groupCollapsed()
|
|
|
|
}
|
|
|
|
|
|
|
|
const groupEnd = function () {
|
|
|
|
if (window.console.groupEnd) window.console.groupEnd()
|
|
|
|
}
|
|
|
|
|
|
|
|
const println = function (type, ...args) {
|
|
|
|
window.console[type](...args)
|
|
|
|
}
|
|
|
|
|
|
|
|
const log = function (...args) {
|
|
|
|
println('log', ...args)
|
|
|
|
}
|
|
|
|
|
|
|
|
const warn = function (...args) {
|
|
|
|
println('warn', ...args)
|
|
|
|
}
|
|
|
|
|
|
|
|
const error = function (...args) {
|
|
|
|
println('error', ...args)
|
|
|
|
}
|
|
|
|
|
|
|
|
const debug = function (handler, ...args) {
|
2020-11-01 16:41:19 +00:00
|
|
|
const now = Date.now()
|
|
|
|
const time = [now - start, `\t+${now - last}ms\t`]
|
2018-08-16 22:30:59 +00:00
|
|
|
|
|
|
|
if (args.length) {
|
|
|
|
println('debug', ...time, `[${handler}]\t`, ...args)
|
2020-11-01 16:41:19 +00:00
|
|
|
} else {
|
2018-08-16 22:30:59 +00:00
|
|
|
println('debug', ...time, handler)
|
|
|
|
}
|
|
|
|
|
|
|
|
last = now
|
|
|
|
}
|
|
|
|
|
|
|
|
const debugException = function (error) {
|
|
|
|
if (error instanceof Error) {
|
2020-11-01 16:41:19 +00:00
|
|
|
let message = `[${error.name}] „${error.message}“ in ${error.fileName} on line ${error.lineNumber}\n`
|
2018-08-16 22:30:59 +00:00
|
|
|
|
|
|
|
if (error.stack) {
|
|
|
|
message += 'Stacktrace:\n'
|
|
|
|
message += error.stack
|
|
|
|
}
|
|
|
|
|
|
|
|
println('error', message)
|
2020-11-01 16:41:19 +00:00
|
|
|
} else if (error.code && error.message) {
|
2018-08-16 22:30:59 +00:00
|
|
|
debugAjaxException(error)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const debugAjaxException = function (error) {
|
|
|
|
groupCollapsed()
|
|
|
|
let details = `[${error.code}] ${error.message}`
|
|
|
|
|
2020-11-01 16:41:19 +00:00
|
|
|
const br2nl = (string) =>
|
|
|
|
string
|
|
|
|
.split('\n')
|
|
|
|
.map((line) => line.replace(/<br\s*\/?>$/i, ''))
|
|
|
|
.join('\n')
|
2018-08-16 22:30:59 +00:00
|
|
|
|
|
|
|
if (error.stacktrace) {
|
|
|
|
details += `\nStacktrace:\n${br2nl(error.stacktrace)}`
|
2020-11-01 16:41:19 +00:00
|
|
|
} else if (error.exceptionID) {
|
2018-08-16 22:30:59 +00:00
|
|
|
details += `\nException ID: ${error.exceptionID}`
|
|
|
|
}
|
|
|
|
|
|
|
|
println('debug', details)
|
|
|
|
|
2020-11-01 16:41:19 +00:00
|
|
|
error.previous.forEach((previous) => {
|
2018-08-16 22:30:59 +00:00
|
|
|
let details = ''
|
|
|
|
|
|
|
|
group()
|
|
|
|
|
|
|
|
details += `${previous.message}\n`
|
|
|
|
details += `Stacktrace:\n${br2nl(previous.stacktrace)}`
|
|
|
|
|
|
|
|
println('debug', details)
|
|
|
|
})
|
|
|
|
|
2020-11-01 16:41:19 +00:00
|
|
|
error.previous.forEach((_) => groupEnd())
|
2018-08-16 22:30:59 +00:00
|
|
|
groupEnd()
|
|
|
|
}
|
|
|
|
|
2020-11-01 16:41:19 +00:00
|
|
|
return {
|
|
|
|
log,
|
|
|
|
warn,
|
|
|
|
error,
|
|
|
|
debug,
|
|
|
|
debugException,
|
|
|
|
group,
|
|
|
|
groupCollapsed,
|
|
|
|
groupEnd,
|
|
|
|
}
|
|
|
|
})
|