File: stack.go

package info (click to toggle)
golang-github-evanw-esbuild 0.25.10-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 10,184 kB
  • sloc: javascript: 28,602; makefile: 856; sh: 17
file content (50 lines) | stat: -rw-r--r-- 1,103 bytes parent folder | download | duplicates (3)
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
package helpers

import (
	"runtime/debug"
	"strings"
)

func PrettyPrintedStack() string {
	lines := strings.Split(strings.TrimSpace(string(debug.Stack())), "\n")

	// Strip the first "goroutine" line
	if len(lines) > 0 {
		if first := lines[0]; strings.HasPrefix(first, "goroutine ") && strings.HasSuffix(first, ":") {
			lines = lines[1:]
		}
	}

	sb := strings.Builder{}

	for _, line := range lines {
		// Indented lines are source locations
		if strings.HasPrefix(line, "\t") {
			line = line[1:]
			line = strings.TrimPrefix(line, "github.com/evanw/esbuild/")
			if offset := strings.LastIndex(line, " +0x"); offset != -1 {
				line = line[:offset]
			}
			sb.WriteString(" (")
			sb.WriteString(line)
			sb.WriteString(")")
			continue
		}

		// Other lines are function calls
		if sb.Len() > 0 {
			sb.WriteByte('\n')
		}
		if strings.HasSuffix(line, ")") {
			if paren := strings.LastIndexByte(line, '('); paren != -1 {
				line = line[:paren]
			}
		}
		if slash := strings.LastIndexByte(line, '/'); slash != -1 {
			line = line[slash+1:]
		}
		sb.WriteString(line)
	}

	return sb.String()
}