@@ -18,7 +18,7 @@ func (c StackCaller) String() string {
18
18
19
19
// Format is implementation of fmt.Formatter.
20
20
func (c StackCaller ) Format (f fmt.State , verb rune ) {
21
- buf := bytes .NewBuffer (nil )
21
+ buf := bytes .NewBuffer (make ([] byte , 0 , 4096 ) )
22
22
switch verb {
23
23
case 's' , 'v' :
24
24
pad , wid , prec := byte ('\t' ), 0 , 1
@@ -34,27 +34,32 @@ func (c StackCaller) Format(f fmt.State, verb rune) {
34
34
}
35
35
padding := bytes .Repeat ([]byte {pad }, wid )
36
36
indent := bytes .Repeat ([]byte {pad }, prec )
37
- var str string
38
37
buf .Write (padding )
39
- str = "???"
38
+ fn : = "???"
40
39
if c .Function != "" {
41
- str = trimSrcPath (c .Function )
40
+ fn = trimSrcPath (c .Function )
42
41
}
43
- buf .WriteString (fmt .Sprintf ("%s(%#x)" , str , c .Entry ))
42
+ buf .WriteString (fmt .Sprintf ("%s(%#x)" , fn , c .Entry ))
44
43
if f .Flag ('+' ) {
45
44
buf .WriteRune ('\n' )
46
45
buf .Write (padding )
47
46
buf .Write (indent )
48
- str = trimSrcPath (c .File )
49
- if f .Flag ('#' ) {
50
- str = trimDirs (str )
47
+ file , line := "???" , 0
48
+ if c .File != "" {
49
+ file = trimSrcPath (c .File )
50
+ if f .Flag ('#' ) {
51
+ file = trimDirs (file )
52
+ }
51
53
}
52
- buf .WriteString (fmt .Sprintf ("%s:%d +%#x" , str , c .Line , c .PC - c .Entry ))
54
+ if c .Line > 0 {
55
+ line = c .Line
56
+ }
57
+ buf .WriteString (fmt .Sprintf ("%s:%d +%#x" , file , line , c .PC - c .Entry ))
53
58
}
59
+ default :
60
+ return
54
61
}
55
- if buf .Len () > 0 {
56
- _ , _ = f .Write (buf .Bytes ())
57
- }
62
+ _ , _ = f .Write (buf .Bytes ())
58
63
}
59
64
60
65
// StackTrace stores the information of stack trace.
@@ -107,7 +112,7 @@ func (t *StackTrace) String() string {
107
112
108
113
// Format is implementation of fmt.Formatter.
109
114
func (t * StackTrace ) Format (f fmt.State , verb rune ) {
110
- buf := bytes .NewBuffer (nil )
115
+ buf := bytes .NewBuffer (make ([] byte , 0 , 4096 ) )
111
116
switch verb {
112
117
case 's' , 'v' :
113
118
format := "%"
@@ -129,10 +134,10 @@ func (t *StackTrace) Format(f fmt.State, verb rune) {
129
134
}
130
135
buf .WriteString (fmt .Sprintf (format , c ))
131
136
}
137
+ default :
138
+ return
132
139
}
133
- if buf .Len () > 0 {
134
- _ , _ = f .Write (buf .Bytes ())
135
- }
140
+ _ , _ = f .Write (buf .Bytes ())
136
141
}
137
142
138
143
// PC returns program counters.
0 commit comments