warning.go 435 B

12345678910111213141516171819202122232425262728293031
  1. package warning
  2. import (
  3. "sync"
  4. "github.com/sagernet/sing-box/log"
  5. )
  6. type Warning struct {
  7. logger log.Logger
  8. check CheckFunc
  9. message string
  10. checkOnce sync.Once
  11. }
  12. type CheckFunc = func() bool
  13. func New(checkFunc CheckFunc, message string) Warning {
  14. return Warning{
  15. check: checkFunc,
  16. message: message,
  17. }
  18. }
  19. func (w *Warning) Check() {
  20. w.checkOnce.Do(func() {
  21. if w.check() {
  22. log.Warn(w.message)
  23. }
  24. })
  25. }