summaryrefslogtreecommitdiff
path: root/logFlag.go
blob: ca6d7af157b56a31537d3367c6727309165096ce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
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.Info("LogFlag.SetValue() before SetValue() log.flag =", f.lf.Bool(), "checkbox =", f.checkbox.Bool())
	f.checkbox.SetChecked(b)
	f.lf.SetBool(b)
	log.Info("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.SetValue(lf.Bool())

	return &f
}