| 1 |
package check |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"math" |
| 6 |
"reflect" |
| 7 |
"runtime" |
| 8 |
"strings" |
| 9 |
) |
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
func callerFuncName(stack int) string { |
| 18 |
pc, _, _, _ := runtime.Caller(stack + 1) |
| 19 |
name := funcNameAt(pc) |
| 20 |
if strings.HasPrefix(name, "(*") { |
| 21 |
if i := strings.Index(name, ")."); i != -1 { |
| 22 |
name = name[i+2:] |
| 23 |
} |
| 24 |
} |
| 25 |
return name |
| 26 |
} |
| 27 |
|
| 28 |
func funcName(f any) string { |
| 29 |
return funcNameAt(reflect.ValueOf(f).Pointer()) |
| 30 |
} |
| 31 |
|
| 32 |
func funcNameAt(pc uintptr) string { |
| 33 |
name := "<unknown>" |
| 34 |
if f := runtime.FuncForPC(pc); f != nil { |
| 35 |
name = f.Name() |
| 36 |
if i := strings.LastIndex(name, "/"); i != -1 { |
| 37 |
name = name[i+1:] |
| 38 |
} |
| 39 |
if i := strings.Index(name, "."); i != -1 { |
| 40 |
name = name[i+1:] |
| 41 |
} |
| 42 |
} |
| 43 |
return name |
| 44 |
} |
| 45 |
|
| 46 |
func format(msg ...any) string { |
| 47 |
if len(msg) > 1 { |
| 48 |
return fmt.Sprintf(msg[0].(string), msg[1:]...) |
| 49 |
} |
| 50 |
return fmt.Sprint(msg...) |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
func digits(number int) int { |
| 55 |
if number == 0 { |
| 56 |
return 1 |
| 57 |
} |
| 58 |
return int(math.Floor(math.Log10(float64(number)) + 1)) |
| 59 |
} |