| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- // Copyright 2024 The tk9.0-go Authors. All rights reserved.
- // Use of this source code is governed by a BSD-style
- // license that can be found in the LICENSE file.
- package tk9_0 // import "modernc.org/tk9.0"
- import (
- "fmt"
- "os"
- "path/filepath"
- "runtime"
- "strconv"
- "strings"
- )
- // origin returns caller's short position, skipping skip frames.
- //
- //lint:ignore U1000 debug helper
- func origin(skip int) string {
- pc, fn, fl, _ := runtime.Caller(skip)
- f := runtime.FuncForPC(pc)
- var fns string
- if f != nil {
- fns = f.Name()
- if x := strings.LastIndex(fns, "."); x > 0 {
- fns = fns[x+1:]
- }
- if strings.HasPrefix(fns, "func") {
- num := true
- for _, c := range fns[len("func"):] {
- if c < '0' || c > '9' {
- num = false
- break
- }
- }
- if num {
- return origin(skip + 2)
- }
- }
- }
- return fmt.Sprintf("%s:%d:%s", filepath.Base(fn), fl, fns)
- }
- // todo prints and return caller's position and an optional message tagged with TODO. Output goes to stderr.
- //
- //lint:ignore U1000 debug helper
- func todo(s string, args ...interface{}) string {
- switch {
- case s == "":
- s = fmt.Sprintf(strings.Repeat("%v ", len(args)), args...)
- default:
- s = fmt.Sprintf(s, args...)
- }
- r := fmt.Sprintf("%s\n\tTODO %s", origin(2), s)
- // fmt.Fprintf(os.Stderr, "%s\n", r)
- // os.Stdout.Sync()
- return r
- }
- // trc prints and return caller's position and an optional message tagged with TRC. Output goes to stderr.
- //
- //lint:ignore U1000 debug helper
- func trc(s string, args ...interface{}) string {
- switch {
- case s == "":
- s = fmt.Sprintf(strings.Repeat("%v ", len(args)), args...)
- default:
- s = fmt.Sprintf(s, args...)
- }
- r := fmt.Sprintf("%s: TRC(id=%v) %s", origin(2), goroutineID(), s)
- fmt.Fprintf(os.Stderr, "%s\n", r)
- os.Stderr.Sync()
- return r
- }
- func goroutineID() int {
- var (
- buf [64]byte
- n = runtime.Stack(buf[:], false)
- stk = strings.TrimPrefix(string(buf[:n]), "goroutine")
- )
- idField := strings.Fields(stk)[0]
- id, err := strconv.Atoi(idField)
- if err != nil {
- panic(fmt.Errorf("can not get goroutine id: %v", err))
- }
- return id
- }
|