From b7c90c8bbccb5eb15a06bc6a370074c756d698ff Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Sat, 6 Jan 2024 11:12:10 -0600 Subject: [PATCH] stepping through init on a window with state Signed-off-by: Jeff Carr --- args.go | 9 +++++- gui.go | 15 ++++++++-- linuxstatus/args.go | 62 +++++++++++++--------------------------- linuxstatus/common.go | 40 ++++++++++++++++++++++++-- linuxstatus/draw.go | 1 + linuxstatus/hostname.go | 4 ++- linuxstatus/linuxloop.go | 4 +-- linuxstatus/net.go | 16 +++++------ linuxstatus/new.go | 12 ++++---- linuxstatus/structs.go | 1 + linuxstatus/update.go | 25 ++++++++++------ 11 files changed, 117 insertions(+), 72 deletions(-) diff --git a/args.go b/args.go index 6527c5b..7d441ae 100644 --- a/args.go +++ b/args.go @@ -19,6 +19,7 @@ var args struct { } var NOW log.LogFlag +var INFO log.LogFlag var NET log.LogFlag var DNS log.LogFlag var PROC log.LogFlag @@ -36,6 +37,12 @@ func init() { NOW.Desc = "temp debugging stuff" NOW.Register() + INFO.B = false + INFO.Name = "INFO" + INFO.Subsystem = "cpdns" + INFO.Desc = "normal debugging stuff" + INFO.Register() + NET.B = false NET.Name = "NET" NET.Subsystem = "cpdns" @@ -60,7 +67,7 @@ func init() { SPEW.Desc = "spew logging" SPEW.Register() - CHANGE.B = false + CHANGE.B = true CHANGE.Name = "CHANGE" CHANGE.Subsystem = "cpdns" CHANGE.Desc = "show droplet state changes" diff --git a/gui.go b/gui.go index 53e9343..fbdc3fc 100644 --- a/gui.go +++ b/gui.go @@ -229,16 +229,25 @@ func mainWindow(title string) { gr.NewButton("linuxstatus.New()", func () { me.statusOS = linuxstatus.New() }) - gr.NewButton("statusOS.Ready()", func () { - me.statusOS.Ready() + gr.NewButton("SetParent()", func () { + me.statusOS.SetParent(me.myGui) + }) + gr.NewButton("InitWindow()", func () { + me.statusOS.InitWindow() }) gr.NewButton("statusOS.Draw()", func () { + me.statusOS.Draw() + }) + gr.NewButton("statusOS.Draw2()", func () { + me.statusOS.Draw2() + }) + gr.NewButton("statusOS.Ready()", func () { me.statusOS.Ready() }) gr.NewButton("statusOS.Update()", func () { me.statusOS.Update() }) - gr.NewButton("Linux details", func () { + gr.NewButton("Linux Status", func () { me.statusOS.Toggle() }) gr.NewButton("resolver status", func () { diff --git a/linuxstatus/args.go b/linuxstatus/args.go index 9457410..d15a166 100644 --- a/linuxstatus/args.go +++ b/linuxstatus/args.go @@ -9,53 +9,31 @@ import ( ) var NOW log.LogFlag +var INFO log.LogFlag var NET log.LogFlag var DNS log.LogFlag var PROC log.LogFlag var SPEW log.LogFlag +var WARN log.LogFlag var CHANGE log.LogFlag var STATUS log.LogFlag -func init() { - NOW.B = false - NOW.Name = "NOW" - NOW.Subsystem = "cpdns" - NOW.Desc = "temp debugging stuff" - NOW.Register() - - NET.B = false - NET.Name = "NET" - NET.Subsystem = "cpdns" - NET.Desc = "Network logging" - NET.Register() - - DNS.B = false - DNS.Name = "DNS" - DNS.Subsystem = "cpdns" - DNS.Desc = "dnsStatus.update()" - DNS.Register() - - PROC.B = false - PROC.Name = "PROC" - PROC.Subsystem = "cpdns" - PROC.Desc = "/proc logging" - PROC.Register() - - SPEW.B = false - SPEW.Name = "SPEW" - SPEW.Subsystem = "cpdns" - SPEW.Desc = "spew logging" - SPEW.Register() - - CHANGE.B = false - CHANGE.Name = "CHANGE" - CHANGE.Subsystem = "cpdns" - CHANGE.Desc = "show droplet state changes" - CHANGE.Register() - - STATUS.B = false - STATUS.Name = "STATUS" - STATUS.Subsystem = "cpdns" - STATUS.Desc = "updateStatus()" - STATUS.Register() +func myreg(f *log.LogFlag, b bool, name string, desc string) { + f.B = b + f.Subsystem = "go.wit.com/control-panels/dns/linuxstatus" + f.Short = "linux" + f.Desc = desc + f.Name = name + f.Register() +} + +func init() { + myreg(&NOW, true, "NOW", "temp debugging stuff") + myreg(&NET, false, "NET", "Network Logging") + myreg(&DNS, false, "DNS", "dnsStatus.update()") + myreg(&PROC, false, "PROC", "/proc logging") + myreg(&SPEW, false, "SPEW", "spew stuff") + myreg(&WARN, true, "WARN", "bad things") + myreg(&CHANGE, true, "CHANGE", "show droplet state changes") + myreg(&STATUS, false, "STATUS", "Update() details") } diff --git a/linuxstatus/common.go b/linuxstatus/common.go index 8bedaf8..b568146 100644 --- a/linuxstatus/common.go +++ b/linuxstatus/common.go @@ -3,30 +3,52 @@ package linuxstatus import ( "go.wit.com/log" + "go.wit.com/gui/gui" ) +func (ls *LinuxStatus) Draw() { + log.Log(CHANGE, "linuxStatus.Draw() window") + if ! ls.Ready() {return} + log.Log(CHANGE, "linuxStatus.Draw() window ready =", ls.ready) + ls.window.Draw() + ls.ready = true +} +func (ls *LinuxStatus) Draw2() { + log.Log(CHANGE, "draw(ls)") + if ! ls.Ready() {return} + log.Log(CHANGE, "draw(ls) ready =", ls.ready) + draw(ls) +} + func (ls *LinuxStatus) Show() { log.Log(CHANGE, "linuxStatus.Show() window") + if ! ls.Ready() {return} + log.Log(CHANGE, "linuxStatus.Show() window ready =", ls.ready) ls.window.Show() ls.hidden = false } func (ls *LinuxStatus) Hide() { log.Log(CHANGE, "linuxStatus.Hide() window") + if ! ls.Ready() {return} + log.Log(CHANGE, "linuxStatus.Hide() window ready =", ls.ready) ls.window.Hide() ls.hidden = true } func (ls *LinuxStatus) Toggle() { log.Log(CHANGE, "linuxStatus.Toggle() window") + if ! ls.Ready() {return} + log.Log(CHANGE, "linuxStatus.Toggle() window ready =", ls.ready) if ls.hidden { - ls.window.Show() + ls.Show() } else { - ls.window.Hide() + ls.Hide() } } func (ls *LinuxStatus) Ready() bool { + log.Log(CHANGE, "Ready() ls =", ls) if me == nil {return false} if ls == nil {return false} if ls.window == nil {return false} @@ -34,8 +56,22 @@ func (ls *LinuxStatus) Ready() bool { } func (ls *LinuxStatus) Initialized() bool { + log.Log(CHANGE, "checking Initialized() ls =", ls) if me == nil {return false} if ls == nil {return false} if ls.parent == nil {return false} return true } + +func (ls *LinuxStatus) SetParent(p *gui.Node) { + log.Log(CHANGE, "Attempting SetParent =", p) + if me == nil {return} + if ls == nil {return} + if ls.parent == nil { + log.Log(CHANGE, "SetParent =", p) + ls.parent = p + return + } else { + log.Log(CHANGE, "SetParent was already set to =", ls.parent) + } +} diff --git a/linuxstatus/draw.go b/linuxstatus/draw.go index b7fbbea..6680f2d 100644 --- a/linuxstatus/draw.go +++ b/linuxstatus/draw.go @@ -27,6 +27,7 @@ func draw(ls *LinuxStatus) { ls.grid.NewLabel("interfaces =") ls.Interfaces = ls.grid.NewCombobox("Interfaces") + ls.speed = gadgets.NewOneLiner(ls.grid, "refresh speed =") ls.speedActual = gadgets.NewOneLiner(ls.grid, "refresh speed =") ls.grid.Margin() diff --git a/linuxstatus/hostname.go b/linuxstatus/hostname.go index 76854f5..50a5b27 100644 --- a/linuxstatus/hostname.go +++ b/linuxstatus/hostname.go @@ -3,6 +3,8 @@ package linuxstatus import ( + "errors" + "go.wit.com/log" "go.wit.com/shell" @@ -28,7 +30,7 @@ func getHostname() { log.Error(err, "FQDN hostname error") return } - log.Warn("full hostname should be:", s) + log.Error(errors.New("full hostname should be: " + s)) dn := run("domainname") if (me.domainname.Get() != dn) { diff --git a/linuxstatus/linuxloop.go b/linuxstatus/linuxloop.go index fbbe653..1981aa2 100644 --- a/linuxstatus/linuxloop.go +++ b/linuxstatus/linuxloop.go @@ -15,7 +15,7 @@ import ( func linuxLoop() { me.changed = false duration := timeFunction(getHostname) - log.Info("getHostname() execution Time: ", duration, "me.changed =", me.changed) + log.Log(INFO, "getHostname() execution Time: ", duration, "me.changed =", me.changed) duration = timeFunction(scanInterfaces) log.Log(NET, "scanInterfaces() execution Time: ", duration) @@ -33,7 +33,7 @@ func linuxLoop() { // me.IPv6.SetText(all) user, _ := user.Current() - log.Println("os.Getuid =", user.Username, os.Getuid()) + log.Log(INFO, "os.Getuid =", user.Username, os.Getuid()) if (me.uid != nil) { me.uid.Set(user.Username + " (" + strconv.Itoa(os.Getuid()) + ")") } diff --git a/linuxstatus/net.go b/linuxstatus/net.go index 27e1a7b..d86aac5 100644 --- a/linuxstatus/net.go +++ b/linuxstatus/net.go @@ -74,7 +74,7 @@ func renameInterface(i *net.Interface) { func checkInterface(i net.Interface) { val, ok := me.ifmap[i.Index] if ! ok { - log.Info(i.Name, "is a new network interface. The linux kernel index =", i.Index) + log.Log(INFO, i.Name, "is a new network interface. The linux kernel index =", i.Index) me.ifmap[i.Index] = new(IFtype) me.ifmap[i.Index].gone = false me.ifmap[i.Index].iface = &i @@ -88,7 +88,7 @@ func checkInterface(i net.Interface) { me.ifmap[i.Index].gone = false log.Log(NET, "me.ifmap[i] does exist. Need to compare everything.", i.Index, i.Name, val.iface.Index, val.iface.Name) if (val.iface.Name != i.Name) { - log.Info(val.iface.Name, "has changed to it's name to", i.Name) + log.Log(INFO, val.iface.Name, "has changed to it's name to", i.Name) me.ifmap[i.Index].iface = &i me.changed = true if (me.Interfaces != nil) { @@ -143,14 +143,14 @@ func checkDNS() (map[string]*IPtype, map[string]*IPtype) { ipt = "IPv6" } if (t.IsReal()) { - log.Info("\tIP is Real ", ipt, i.Index, i.Name, s) + log.Log(INFO, "\tIP is Real ", ipt, i.Index, i.Name, s) if (t.ipv6) { ipv6s[s] = t } else { ipv4s[s] = t } } else { - log.Info("\tIP is not Real", ipt, i.Index, i.Name, s) + log.Log(INFO, "\tIP is not Real", ipt, i.Index, i.Name, s) } } return ipv6s, ipv4s @@ -192,14 +192,14 @@ func checkIP(ip *net.IPNet, i net.Interface) bool { } } if (IsReal(&ip.IP)) { - log.Info("\tIP is Real ", t, i.Index, i.Name, realip) + log.Log(INFO, "\tIP is Real ", t, i.Index, i.Name, realip) } else { - log.Info("\tIP is not Real", t, i.Index, i.Name, realip) + log.Log(INFO, "\tIP is not Real", t, i.Index, i.Name, realip) } log.Log(NET, "\t\tIP is IsPrivate() =", ip.IP.IsPrivate()) log.Log(NET, "\t\tIP is IsLoopback() =", ip.IP.IsLoopback()) log.Log(NET, "\t\tIP is IsLinkLocalUnicast() =", ip.IP.IsLinkLocalUnicast()) - // log.Info("HERE HERE", "realip =", realip, "me.ip[realip]=", me.ipmap[realip]) + // log.Log(INFO, "HERE HERE", "realip =", realip, "me.ip[realip]=", me.ipmap[realip]) return true } @@ -210,7 +210,7 @@ func scanInterfaces() { log.Log(NET, SPEW, ifaces) for _, i := range ifaces { addrs, _ := i.Addrs() - // log.Info("range ifaces = ", i) + // log.Log(INFO, "range ifaces = ", i) checkInterface(i) log.Log(NET, "*net.Interface.Name = ", i.Name, i.Index) log.Log(NET, SPEW, i) diff --git a/linuxstatus/new.go b/linuxstatus/new.go index 5788ee1..4ae57eb 100644 --- a/linuxstatus/new.go +++ b/linuxstatus/new.go @@ -9,29 +9,31 @@ import ( func New() *LinuxStatus { if me != nil { - log.Warn("You have done New() twice. You can only do this once") + log.Log(WARN, "You have done New() twice. You can only do this once") return me } me = &LinuxStatus { hidden: true, ready: false, } + me.ifmap = make(map[int]*IFtype) + me.ipmap = make(map[string]*IPtype) return me } func (ls *LinuxStatus) InitWindow() { if ! ls.Initialized() { - log.Warn("LinuxStatus() is not initalized yet (no parent for the window?)") + log.Log(WARN, "not initalized yet (no parent for the window?)") return } if ls.window != nil { - log.Warn("You already have a window") + log.Log(WARN, "You already have a window") ls.ready = true return } - ls.ready = true - log.Warn("Creating the Window") + log.Log(WARN, "Creating the Window") ls.window = gadgets.NewBasicWindow(ls.parent, "Linux OS Details") + ls.ready = true } diff --git a/linuxstatus/structs.go b/linuxstatus/structs.go index be541f2..10fec02 100644 --- a/linuxstatus/structs.go +++ b/linuxstatus/structs.go @@ -36,6 +36,7 @@ type LinuxStatus struct { IPv6 *gadgets.OneLiner workingIPv6 *gadgets.OneLiner Interfaces *gui.Node + speed *gadgets.OneLiner speedActual *gadgets.OneLiner } diff --git a/linuxstatus/update.go b/linuxstatus/update.go index 1ab8cee..0de7cf9 100644 --- a/linuxstatus/update.go +++ b/linuxstatus/update.go @@ -1,31 +1,40 @@ package linuxstatus import ( - "errors" "fmt" "time" + "errors" "go.wit.com/log" ) func (ls *LinuxStatus) Update() { - log.Info("linuxStatus() Update() START") - if ls == nil { - log.Error(errors.New("linuxStatus() Update() ls == nil")) + if ! ls.Ready() { + log.Warn("can't update yet. ready is false") + log.Error(errors.New("Update() is not ready yet")) return } + log.Log(INFO, "Update() START") duration := timeFunction(func () { linuxLoop() }) + ls.SetSpeed(duration) + log.Info("linuxStatus() Update() END") +} + +func (ls *LinuxStatus) SetSpeed(duration time.Duration) { s := fmt.Sprint(duration) + if ls.speedActual == nil { + log.Warn("can't actually warn") + return + } ls.speedActual.Set(s) if (duration > 500 * time.Millisecond ) { - // ls.speed, "SLOW") + ls.speed.Set("SLOW") } else if (duration > 100 * time.Millisecond ) { - // ls.speed, "OK") + ls.speed.Set("OK") } else { - // ls.speed, "FAST") + ls.speed.Set("FAST") } - log.Info("linuxStatus() Update() END") }