Browse Source

Debug events module

Jakob Borg 11 years ago
parent
commit
87c3790fa8
4 changed files with 39 additions and 0 deletions
  1. 1 0
      cmd/syncthing/main.go
  2. 17 0
      events/debug.go
  3. 17 0
      events/events.go
  4. 4 0
      events/events_test.go

+ 1 - 0
cmd/syncthing/main.go

@@ -107,6 +107,7 @@ The following enviroment variables are interpreted by syncthing:
                facility strings:
                - "beacon"   (the beacon package)
                - "discover" (the discover package)
+               - "events"   (the events package)
                - "files"    (the files package)
                - "net"      (the main package; connections & network messages)
                - "model"    (the model package)

+ 17 - 0
events/debug.go

@@ -0,0 +1,17 @@
+// Copyright (C) 2014 Jakob Borg and Contributors (see the CONTRIBUTORS file).
+// All rights reserved. Use of this source code is governed by an MIT-style
+// license that can be found in the LICENSE file.
+
+package events
+
+import (
+	"os"
+	"strings"
+
+	"github.com/calmh/syncthing/logger"
+)
+
+var (
+	debug = strings.Contains(os.Getenv("STTRACE"), "events") || os.Getenv("STTRACE") == "all"
+	dl    = logger.DefaultLogger
+)

+ 17 - 0
events/events.go

@@ -1,3 +1,7 @@
+// Copyright (C) 2014 Jakob Borg and Contributors (see the CONTRIBUTORS file).
+// All rights reserved. Use of this source code is governed by an MIT-style
+// license that can be found in the LICENSE file.
+
 // Package events provides event subscription and polling functionality.
 package events
 
@@ -93,6 +97,9 @@ func NewLogger() *Logger {
 
 func (l *Logger) Log(t EventType, data interface{}) {
 	l.mutex.Lock()
+	if debug {
+		dl.Debugln("log", l.nextId, t.String(), data)
+	}
 	e := Event{
 		ID:   l.nextId,
 		Time: time.Now(),
@@ -114,6 +121,9 @@ func (l *Logger) Log(t EventType, data interface{}) {
 
 func (l *Logger) Subscribe(mask EventType) *Subscription {
 	l.mutex.Lock()
+	if debug {
+		dl.Debugln("subscribe", mask)
+	}
 	s := &Subscription{
 		mask:   mask,
 		id:     l.nextId,
@@ -127,6 +137,9 @@ func (l *Logger) Subscribe(mask EventType) *Subscription {
 
 func (l *Logger) Unsubscribe(s *Subscription) {
 	l.mutex.Lock()
+	if debug {
+		dl.Debugln("unsubsribe")
+	}
 	delete(l.subs, s.id)
 	close(s.events)
 	l.mutex.Unlock()
@@ -136,6 +149,10 @@ func (s *Subscription) Poll(timeout time.Duration) (Event, error) {
 	s.mutex.Lock()
 	defer s.mutex.Unlock()
 
+	if debug {
+		dl.Debugln("poll", timeout)
+	}
+
 	to := time.After(timeout)
 	select {
 	case e, ok := <-s.events:

+ 4 - 0
events/events_test.go

@@ -1,3 +1,7 @@
+// Copyright (C) 2014 Jakob Borg and Contributors (see the CONTRIBUTORS file).
+// All rights reserved. Use of this source code is governed by an MIT-style
+// license that can be found in the LICENSE file.
+
 package events_test
 
 import (