mirror of https://github.com/micromata/borgbackup-butler.git

Kai Reinhard
19.24.2021 7f78d2ed3b282c6bd6bba9dde4110cd4f3d558cf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/////////////////////////////////////////////////////////////////////////////
//
// Project ProjectForge Community Edition
//         www.projectforge.org
//
// Copyright (C) 2001-2021 Micromata GmbH, Germany (www.micromata.com)
//
// ProjectForge is dual-licensed.
//
// This community edition is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License as published
// by the Free Software Foundation; version 3 of the License.
//
// This community edition is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
// Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, see http://www.gnu.org/licenses/.
//
/////////////////////////////////////////////////////////////////////////////
 
package de.micromata.borgbutler.server.rest
 
/**
 * Prints stack-trace without foreign packages in much shorter form than log.error(ex.message, ex) does.
 * @author Kai Reinhard (k.reinhard@micromata.de)
 */
object ExceptionStackTracePrinter {
 
    /**
     * @param showExceptionMessage If true, the exception message itself will be prepended to stack trace.
     * @param stopBeforeForeignPackages If true, after showing stack trace elements of own package 'org.projectforge' any further element is hidden if foreign stack element is reached.
     * @param depth Maximum depth of stack trace elements to show (default is 10).
     * @param showPackagesOnly Only stack elements started with one of these string is classified as own package to print. If nothing given, all 'org.projectforge' classes are
     * classified as own packages.
     */
    @JvmStatic
    @JvmOverloads
    fun toString(ex: Exception, showExceptionMessage: Boolean = true, stopBeforeForeignPackages: Boolean = true, depth: Int = 10, vararg showPackagesOnly: String): String {
        val sb = StringBuilder()
        if (showExceptionMessage) {
            sb.append(ex::class.java.name).append(":").append(ex.message).append("\n")
        }
        var counter = 0
        val showPackages = if (showPackagesOnly.isEmpty()) OWN_PACKAGES else showPackagesOnly
        var placeHolderPrinted = false
        var ownStackelementsPrinted = false
        for (element in ex.stackTrace) {
            if (!showPackages.any { element.className.startsWith(it) }) {
                if (ownStackelementsPrinted && stopBeforeForeignPackages) {
                    sb.append("...following foreign packages are hidden...\n")
                    break
                }
                if (!placeHolderPrinted) {
                    sb.append("...(foreign packages are hidden)...\n")
                    placeHolderPrinted = true
                }
                continue // Don't show foreign class entries.
            }
            ownStackelementsPrinted = true
            sb.append("at ${element.className}.${element.methodName} (${element.fileName}:${element.lineNumber})\n")
            if (++counter >= depth) {
                break
            }
        }
        return sb.toString()
    }
 
    val OWN_PACKAGES = arrayOf("de.micromata.borgbutler")
}