Debug events module

This commit is contained in:
Jakob Borg 2014-07-25 14:50:14 +02:00
parent 0d9dcb2f4f
commit 87c3790fa8
4 changed files with 39 additions and 0 deletions

View File

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

17
events/debug.go Normal file
View File

@ -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
)

View File

@ -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 provides event subscription and polling functionality.
package events package events
@ -93,6 +97,9 @@ func NewLogger() *Logger {
func (l *Logger) Log(t EventType, data interface{}) { func (l *Logger) Log(t EventType, data interface{}) {
l.mutex.Lock() l.mutex.Lock()
if debug {
dl.Debugln("log", l.nextId, t.String(), data)
}
e := Event{ e := Event{
ID: l.nextId, ID: l.nextId,
Time: time.Now(), Time: time.Now(),
@ -114,6 +121,9 @@ func (l *Logger) Log(t EventType, data interface{}) {
func (l *Logger) Subscribe(mask EventType) *Subscription { func (l *Logger) Subscribe(mask EventType) *Subscription {
l.mutex.Lock() l.mutex.Lock()
if debug {
dl.Debugln("subscribe", mask)
}
s := &Subscription{ s := &Subscription{
mask: mask, mask: mask,
id: l.nextId, id: l.nextId,
@ -127,6 +137,9 @@ func (l *Logger) Subscribe(mask EventType) *Subscription {
func (l *Logger) Unsubscribe(s *Subscription) { func (l *Logger) Unsubscribe(s *Subscription) {
l.mutex.Lock() l.mutex.Lock()
if debug {
dl.Debugln("unsubsribe")
}
delete(l.subs, s.id) delete(l.subs, s.id)
close(s.events) close(s.events)
l.mutex.Unlock() l.mutex.Unlock()
@ -136,6 +149,10 @@ func (s *Subscription) Poll(timeout time.Duration) (Event, error) {
s.mutex.Lock() s.mutex.Lock()
defer s.mutex.Unlock() defer s.mutex.Unlock()
if debug {
dl.Debugln("poll", timeout)
}
to := time.After(timeout) to := time.After(timeout)
select { select {
case e, ok := <-s.events: case e, ok := <-s.events:

View File

@ -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 package events_test
import ( import (