debug.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. // Copyright (C) 2015 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 http://mozilla.org/MPL/2.0/.
  6. package sync
  7. import (
  8. "os"
  9. "strconv"
  10. "strings"
  11. "time"
  12. "github.com/syncthing/syncthing/lib/logger"
  13. )
  14. var (
  15. threshold = time.Duration(100 * time.Millisecond)
  16. l = logger.DefaultLogger.NewFacility("sync", "Mutexes")
  17. // We make an exception in this package and have an actual "if debug { ...
  18. // }" variable, as it may be rather performance critical and does
  19. // nonstandard things (from a debug logging PoV).
  20. debug = strings.Contains(os.Getenv("STTRACE"), "sync") || os.Getenv("STTRACE") == "all"
  21. )
  22. func init() {
  23. l.SetDebug("sync", strings.Contains(os.Getenv("STTRACE"), "sync") || os.Getenv("STTRACE") == "all")
  24. if n, err := strconv.Atoi(os.Getenv("STLOCKTHRESHOLD")); err == nil {
  25. threshold = time.Duration(n) * time.Millisecond
  26. }
  27. l.Debugf("Enabling lock logging at %v threshold", threshold)
  28. }
  29. func shouldDebug() bool {
  30. return l.ShouldDebug("sync")
  31. }