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:
- "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
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
@ -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:

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
import (