From 87c3790fa8f5cb7b6bfbfe05321587aa3057b2fb Mon Sep 17 00:00:00 2001 From: Jakob Borg Date: Fri, 25 Jul 2014 14:50:14 +0200 Subject: [PATCH] Debug events module --- cmd/syncthing/main.go | 1 + events/debug.go | 17 +++++++++++++++++ events/events.go | 17 +++++++++++++++++ events/events_test.go | 4 ++++ 4 files changed, 39 insertions(+) create mode 100644 events/debug.go diff --git a/cmd/syncthing/main.go b/cmd/syncthing/main.go index 016b7b8b1..b66b1f6d8 100644 --- a/cmd/syncthing/main.go +++ b/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) diff --git a/events/debug.go b/events/debug.go new file mode 100644 index 000000000..64c22b7cc --- /dev/null +++ b/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 +) diff --git a/events/events.go b/events/events.go index 52b95b7b0..d464ed8af 100644 --- a/events/events.go +++ b/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: diff --git a/events/events_test.go b/events/events_test.go index ffc4adfe4..4a44284ed 100644 --- a/events/events_test.go +++ b/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 (