etc_musl.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. // Copyright 2023 The Libc Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. //go:build linux && (amd64 || arm64 || loong64 || ppc64le || s390x || riscv64 || 386 || arm)
  5. package libc // import "modernc.org/libc"
  6. import (
  7. "fmt"
  8. "os"
  9. "path/filepath"
  10. "runtime"
  11. "strings"
  12. )
  13. // origin returns caller's short position, skipping skip frames.
  14. func origin(skip int) string {
  15. pc, fn, fl, _ := runtime.Caller(skip)
  16. f := runtime.FuncForPC(pc)
  17. var fns string
  18. if f != nil {
  19. fns = f.Name()
  20. if x := strings.LastIndex(fns, "."); x > 0 {
  21. fns = fns[x+1:]
  22. }
  23. if strings.HasPrefix(fns, "func") {
  24. num := true
  25. for _, c := range fns[len("func"):] {
  26. if c < '0' || c > '9' {
  27. num = false
  28. break
  29. }
  30. }
  31. if num {
  32. return origin(skip + 2)
  33. }
  34. }
  35. }
  36. return fmt.Sprintf("%s:%d:%s", filepath.Base(fn), fl, fns)
  37. }
  38. // todo prints and return caller's position and an optional message tagged with TODO. Output goes to stderr.
  39. func todo(s string, args ...interface{}) string {
  40. switch {
  41. case s == "":
  42. s = fmt.Sprintf(strings.Repeat("%v ", len(args)), args...)
  43. default:
  44. s = fmt.Sprintf(s, args...)
  45. }
  46. r := fmt.Sprintf("%s\n\tTODO %s", origin(2), s)
  47. // fmt.Fprintf(os.Stderr, "%s\n", r)
  48. // os.Stdout.Sync()
  49. return r
  50. }
  51. // trc prints and return caller's position and an optional message tagged with TRC. Output goes to stderr.
  52. func trc(s string, args ...interface{}) string {
  53. switch {
  54. case s == "":
  55. s = fmt.Sprintf(strings.Repeat("%v ", len(args)), args...)
  56. default:
  57. s = fmt.Sprintf(s, args...)
  58. }
  59. r := fmt.Sprintf("%s: TRC %s", origin(2), s)
  60. fmt.Fprintf(os.Stderr, "%s\n", r)
  61. os.Stderr.Sync()
  62. return r
  63. }