debug.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  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/sasha-s/go-deadlock"
  13. "github.com/syncthing/syncthing/lib/logger"
  14. )
  15. var (
  16. threshold = 100 * time.Millisecond
  17. l = logger.DefaultLogger.NewFacility("sync", "Mutexes")
  18. // We make an exception in this package and have an actual "if debug { ...
  19. // }" variable, as it may be rather performance critical and does
  20. // nonstandard things (from a debug logging PoV).
  21. debug = strings.Contains(os.Getenv("STTRACE"), "sync") || os.Getenv("STTRACE") == "all"
  22. useDeadlock = os.Getenv("STDEADLOCK") != ""
  23. )
  24. func init() {
  25. l.SetDebug("sync", strings.Contains(os.Getenv("STTRACE"), "sync") || os.Getenv("STTRACE") == "all")
  26. if n, err := strconv.Atoi(os.Getenv("STLOCKTHRESHOLD")); err == nil {
  27. threshold = time.Duration(n) * time.Millisecond
  28. }
  29. if n, err := strconv.Atoi(os.Getenv("STDEADLOCK")); err == nil {
  30. deadlock.Opts.DeadlockTimeout = time.Duration(n) * time.Second
  31. }
  32. l.Debugf("Enabling lock logging at %v threshold", threshold)
  33. }