gadgets/logFlag.go

121 lines
3.1 KiB
Go
Raw Normal View History

/*
A simple GUI gadget to control on a 'log' package flag
-----------------------------------------------
| | |
| [ X ] | INFO (controls log.Info() |
| | |
-----------------------------------------------
*/
package gadgets
import (
"errors"
"go.wit.com/gui"
"go.wit.com/log"
)
type LogFlag struct {
// p *gui.Node // parent widget
checkbox *gui.Node // checkbox widget
lf *log.LogFlag // this is the object from the 'log' package
name string
subsystem string
desc string
orig bool
// b bool
Custom func()
}
// probably a better name than GetValue()
func (f *LogFlag) Bool() bool {
if f.lf.Bool() != f.checkbox.Bool() {
log.Error(errors.New("gadget.LogFlag error. actual flag.Bool() does not match checkbox.Bool()"))
// set the checkbox to reflect the actual flag value
f.checkbox.SetChecked(f.lf.Bool())
}
return f.lf.Bool()
}
func (f *LogFlag) GetValue() bool {
return f.checkbox.Bool()
}
func (f *LogFlag) GetName() string {
return f.name
}
func (f *LogFlag) GetSubsystem() string {
return f.subsystem
}
func (f *LogFlag) GetDesc() string {
return f.name
}
func (f *LogFlag) SetValue(b bool) {
log.Log(GADGETS, "LogFlag.SetValue() before SetValue() log.flag =", f.lf.Bool(), "checkbox =", f.checkbox.Bool())
f.checkbox.SetChecked(b)
f.lf.SetBool(b)
log.Log(GADGETS, "LogFlag.SetValue() after SetValue() log.flag =", f.lf.Bool(), "checkbox =", f.checkbox.Bool())
}
// should be RestoreDefault() ?
func (f *LogFlag) RestoreDefault() {
f.lf.SetDefault()
}
func NewLogFlag(n *gui.Node, lf *log.LogFlag) *LogFlag {
f := LogFlag{
lf: lf,
name: lf.GetName(),
subsystem: lf.GetSubsystem(),
desc: lf.GetDesc(),
orig: lf.GetDefault(),
}
// make the checkbox for turning on and off the logging flag
f.checkbox = n.NewCheckbox(f.name + ": " + f.desc)
// when the user checks the logging flag, do a sanity check here
// since this is likely the one place, when everything else
// has gone black, that the user(probably a developer in this case)
// will have. if the toolkit, general GUI code or whatever else
// is not working, this will sanity check that the checkboxes
// are at least working to set the logging flags
// which will let the developer have some home of seeing the
// output errors from whatever might be broken
// this is useful when trying to debug toolkit plugins
var watcher bool = true
var counter int = 0
f.checkbox.Custom = func() {
if counter == 0 {
// store the first clicked value
watcher = f.lf.Bool()
} else {
// if the value has changed, then everything is fine
if watcher != f.lf.Bool() {
watcher = f.lf.Bool()
counter = 0
}
// this means the value has not changed 3 times
// in a row. something is wrong! inform the user
// (in the GUI since that is where they are clicking
// the checkbox so we know they can seen things there
if counter > 4 {
f.checkbox.SetLabel(f.name + ": log settings are not working")
counter = 0
}
}
counter += 1
f.lf.SetBool(f.checkbox.Bool())
}
f.checkbox.SetChecked(lf.Bool())
return &f
}