Add Debug variable to control verbose output.

This commit is contained in:
Greg 2019-02-07 10:18:53 -05:00
parent 20c89c6e56
commit 814c6ffa20

View File

@ -17,6 +17,8 @@ import (
bolt "github.com/coreos/bbolt" bolt "github.com/coreos/bbolt"
) )
var Debug bool
// allow persistant storage and reloading of variables to bolt database. // allow persistant storage and reloading of variables to bolt database.
// everything is time stamped. // everything is time stamped.
// bucket is the variable name // bucket is the variable name
@ -101,13 +103,15 @@ func New(name string, x interface{},ps ...bool) *Var {
fmt.Println("Load error:",err) fmt.Println("Load error:",err)
wchan <- encode(ret) wchan <- encode(ret)
} }
fmt.Println("New(): ",ret.name) if Debug { fmt.Println("New(): ",ret.name) }
mux.Lock() mux.Lock()
if loaded == nil { if loaded == nil {
loaded = make(chan *Var,conf.MaxVars) // buffered channel loaded = make(chan *Var,conf.MaxVars) // buffered channel
} }
mux.Unlock() mux.Unlock()
if Debug { fmt.Println("New(): loaded<- ret") }
loaded<- ret loaded<- ret
if Debug { fmt.Println("New(): returning") }
return ret return ret
} }
@ -155,7 +159,7 @@ func (p *Var) Load(ts ...time.Time) error {
p.Time = t p.Time = t
} }
} else { } else {
fmt.Println("Load(",p.name,"): type =",reflect.TypeOf(p.X)) if Debug { fmt.Println("Load(",p.name,"): type =",reflect.TypeOf(p.X)) }
} }
return err return err
} }
@ -194,7 +198,7 @@ func Checkpoints() {
} }
func Undo() { func Undo() {
fmt.Println("Undo()") if Debug { fmt.Println("Undo()") }
mux.Lock() mux.Lock()
__checkpoints.DeleteSync(lastCheckpoint) __checkpoints.DeleteSync(lastCheckpoint)
h,_ := __checkpoints.History() h,_ := __checkpoints.History()
@ -303,7 +307,7 @@ func start() {
var err error var err error
dbdir := path.Dir(conf.DBPath) dbdir := path.Dir(conf.DBPath)
if _, err = os.Stat(dbdir); os.IsNotExist(err) { if _, err = os.Stat(dbdir); os.IsNotExist(err) {
log.Print("creating db dir") if Debug { log.Print("creating db dir") }
err = os.MkdirAll(dbdir,0700) err = os.MkdirAll(dbdir,0700)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
@ -327,13 +331,13 @@ func start() {
h, err := __checkpoints.History() h, err := __checkpoints.History()
if len(h) > 0 { if len(h) > 0 {
lastCheckpoint = h[len(h)-1].Time lastCheckpoint = h[len(h)-1].Time
fmt.Println("lastCheckpoint = ",lastCheckpoint) if Debug { fmt.Println("lastCheckpoint = ",lastCheckpoint) }
} }
mux.Unlock() mux.Unlock()
} }
func writer() { func writer() {
fmt.Println("launching writer thread") if Debug { fmt.Println("launching writer thread") }
runtime.UnlockOSThread() runtime.UnlockOSThread()
for p := range wchan { for p := range wchan {
wmux.Lock() wmux.Lock()
@ -344,7 +348,7 @@ func writer() {
if err != nil { if err != nil {
log.Fatal("Cannot create bucket for ",string(p.name)) log.Fatal("Cannot create bucket for ",string(p.name))
} }
fmt.Println("writer(): created bucket for ",string(p.name)) if Debug { fmt.Println("writer(): created bucket for ",string(p.name)) }
b = tx.Bucket(p.name) b = tx.Bucket(p.name)
if b == nil { if b == nil {
log.Fatal("Bucket error") log.Fatal("Bucket error")
@ -361,7 +365,7 @@ func writer() {
case []byte: case []byte:
t := time.Now() t := time.Now()
if string(p.name) == "__checkpoints" { if string(p.name) == "__checkpoints" {
fmt.Println("saving: ",string(p.name),t) if Debug { fmt.Println("saving: ",string(p.name),t) }
lastCheckpoint = t lastCheckpoint = t
} }
err = b.Put(tencode(t),v) err = b.Put(tencode(t),v)
@ -379,9 +383,9 @@ func writer() {
close(p.donech) close(p.donech)
} }
} }
fmt.Println("leaving writer thread") if Debug { fmt.Println("leaving writer thread") }
db.Close() db.Close()
fmt.Println("DB is closed") if Debug { fmt.Println("DB is closed") }
wg.Done() wg.Done()
} }
@ -463,7 +467,7 @@ func ExpireAfterFunc(exp time.Duration) TidyFunc {
if exp > 0 { if exp > 0 {
exp = -1 * exp exp = -1 * exp
} }
fmt.Println("ExpireAfter(",exp,")") if Debug { fmt.Println("ExpireAfter(",exp,")") }
return func(b *bolt.Bucket) int { return func(b *bolt.Bucket) int {
stime := time.Now() stime := time.Now()
etime := stime.Add(exp) etime := stime.Add(exp)
@ -485,10 +489,10 @@ func ExpireAfterFunc(exp time.Duration) TidyFunc {
} }
func tidyThread() { func tidyThread() {
fmt.Println("tidyThread() starting") if Debug { fmt.Println("tidyThread() starting") }
lchan := make(chan string) lchan := make(chan string)
go func() { go func() {
fmt.Println("starting loaded reading thread") if Debug { fmt.Println("starting loaded reading thread") }
if loaded == nil { if loaded == nil {
loaded = make(chan *Var,conf.MaxVars) // buffered channel loaded = make(chan *Var,conf.MaxVars) // buffered channel
} }
@ -499,21 +503,21 @@ func tidyThread() {
loaded<- p // put it back in the queue loaded<- p // put it back in the queue
} }
} }
fmt.Println("closing lchan") if Debug { fmt.Println("closing lchan") }
close(lchan) close(lchan)
} }
}() }()
tick := time.NewTicker(conf.Tidy.Interval) tick := time.NewTicker(conf.Tidy.Interval)
for { select { for { select {
case <-tchan: case <-tchan:
fmt.Println("closing loaded channel") if Debug { fmt.Println("closing loaded channel") }
close(loaded) close(loaded)
tg.Done() tg.Done()
fmt.Println("tidy(): returning") if Debug { fmt.Println("tidy(): returning") }
return return
case <-tick.C: case <-tick.C:
name := <-lchan name := <-lchan
fmt.Println("tidyThread(): tidying ", name) if Debug { fmt.Println("tidyThread(): tidying ", name) }
Tidy(name) Tidy(name)
} } } }
} }
@ -534,8 +538,8 @@ func Tidy(name string) {
}) })
mux.Unlock() mux.Unlock()
if i > 0 { if i > 0 {
fmt.Println("tidy(): deleted ",i," entries in ",time.Since(stime)) if Debug { fmt.Println("tidy(): deleted ",i," entries in ",time.Since(stime)) }
} }
fmt.Println("tidy(): done tidying",name) if Debug { fmt.Println("tidy(): done tidying",name) }
} }