print.go 815 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. package p
  2. import (
  3. "fmt"
  4. "time"
  5. "git.bvbej.com/bvbej/base-golang/pkg/trace"
  6. )
  7. type Option func(*option)
  8. type Trace = trace.T
  9. type option struct {
  10. Trace *trace.Trace
  11. Debug *trace.Debug
  12. }
  13. func newOption() *option {
  14. return &option{}
  15. }
  16. func Println(key string, value any, options ...Option) {
  17. ts := time.Now()
  18. opt := newOption()
  19. defer func() {
  20. if opt.Trace != nil {
  21. opt.Debug.Key = key
  22. opt.Debug.Value = value
  23. opt.Debug.CostSeconds = time.Since(ts).Seconds()
  24. opt.Trace.AppendDebug(opt.Debug)
  25. }
  26. }()
  27. for _, f := range options {
  28. f(opt)
  29. }
  30. fmt.Println(fmt.Sprintf("KEY: %s | VALUE: %v", key, value))
  31. }
  32. // WithTrace 设置trace信息
  33. func WithTrace(t Trace) Option {
  34. return func(opt *option) {
  35. if t != nil {
  36. opt.Trace = t.(*trace.Trace)
  37. opt.Debug = new(trace.Debug)
  38. }
  39. }
  40. }