expensive.go 604 B

12345678910111213141516171819202122232425
  1. // Copyright (C) 2025 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at https://mozilla.org/MPL/2.0/.
  6. package slogutil
  7. import (
  8. "log/slog"
  9. )
  10. // Expensive wraps a log value that is expensive to compute and should only
  11. // be called if the log line is actually emitted.
  12. func Expensive(fn func() any) expensive {
  13. return expensive{fn}
  14. }
  15. type expensive struct {
  16. fn func() any
  17. }
  18. func (e expensive) LogValue() slog.Value {
  19. return slog.AnyValue(e.fn())
  20. }