Add functions for structured progress logging
This commit is contained in:
parent
58e452ea4e
commit
e852713ed7
|
@ -1,6 +1,7 @@
|
||||||
package run
|
package run
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"time"
|
"time"
|
||||||
|
@ -25,7 +26,11 @@ func init() {
|
||||||
log.Errorf("%s", err)
|
log.Errorf("%s", err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
group := groups.NettestGroups[*nettestGroup]
|
group, ok := groups.NettestGroups[*nettestGroup]
|
||||||
|
if !ok {
|
||||||
|
log.Errorf("No test group named %s", *nettestGroup)
|
||||||
|
return errors.New("invalid test group name")
|
||||||
|
}
|
||||||
log.Debugf("Running test group %s", group.Label)
|
log.Debugf("Running test group %s", group.Label)
|
||||||
|
|
||||||
result, err := database.CreateResult(ctx.DB, database.Result{
|
result, err := database.CreateResult(ctx.DB, database.Result{
|
||||||
|
|
|
@ -60,15 +60,25 @@ func New(w io.Writer) *Handler {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// HandleLog implements log.Handler.
|
// TypedLog is used for handling special "typed" logs to the CLI
|
||||||
func (h *Handler) HandleLog(e *log.Entry) error {
|
func (h *Handler) TypedLog(t string, e *log.Entry) error {
|
||||||
|
switch t {
|
||||||
|
case "progress":
|
||||||
|
// XXX replace this with something more fancy like https://github.com/tj/go-progress
|
||||||
|
fmt.Fprintf(h.Writer, "%.1f%% [%s]: %s", e.Fields.Get("percentage").(float64)*100, e.Fields.Get("key"), e.Message)
|
||||||
|
fmt.Fprintln(h.Writer)
|
||||||
|
return nil
|
||||||
|
default:
|
||||||
|
return h.DefaultLog(e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// DefaultLog is the default way of printing out logs
|
||||||
|
func (h *Handler) DefaultLog(e *log.Entry) error {
|
||||||
color := Colors[e.Level]
|
color := Colors[e.Level]
|
||||||
level := Strings[e.Level]
|
level := Strings[e.Level]
|
||||||
names := e.Fields.Names()
|
names := e.Fields.Names()
|
||||||
|
|
||||||
h.mu.Lock()
|
|
||||||
defer h.mu.Unlock()
|
|
||||||
|
|
||||||
color.Fprintf(h.Writer, "%s %-25s", bold.Sprintf("%*s", h.Padding+1, level), e.Message)
|
color.Fprintf(h.Writer, "%s %-25s", bold.Sprintf("%*s", h.Padding+1, level), e.Message)
|
||||||
|
|
||||||
for _, name := range names {
|
for _, name := range names {
|
||||||
|
@ -82,3 +92,16 @@ func (h *Handler) HandleLog(e *log.Entry) error {
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// HandleLog implements log.Handler.
|
||||||
|
func (h *Handler) HandleLog(e *log.Entry) error {
|
||||||
|
h.mu.Lock()
|
||||||
|
defer h.mu.Unlock()
|
||||||
|
|
||||||
|
t, isTyped := e.Fields["type"].(string)
|
||||||
|
if isTyped {
|
||||||
|
return h.TypedLog(t, e)
|
||||||
|
}
|
||||||
|
|
||||||
|
return h.DefaultLog(e)
|
||||||
|
}
|
||||||
|
|
14
internal/output/output.go
Normal file
14
internal/output/output.go
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
package output
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/apex/log"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Progress logs a progress type event
|
||||||
|
func Progress(key string, perc float64, msg string) {
|
||||||
|
log.WithFields(log.Fields{
|
||||||
|
"type": "progress",
|
||||||
|
"key": key,
|
||||||
|
"percentage": perc,
|
||||||
|
}).Info(msg)
|
||||||
|
}
|
|
@ -82,7 +82,7 @@ var NettestGroups = map[string]NettestGroup{
|
||||||
return string(summaryBytes), nil
|
return string(summaryBytes), nil
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
"middleboxes": NettestGroup{
|
"middlebox": NettestGroup{
|
||||||
Label: "Middleboxes",
|
Label: "Middleboxes",
|
||||||
Nettests: []nettests.Nettest{
|
Nettests: []nettests.Nettest{
|
||||||
middlebox.HTTPInvalidRequestLine{},
|
middlebox.HTTPInvalidRequestLine{},
|
||||||
|
|
|
@ -2,6 +2,7 @@ package nettests
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
"github.com/apex/log"
|
"github.com/apex/log"
|
||||||
"github.com/measurement-kit/go-measurement-kit"
|
"github.com/measurement-kit/go-measurement-kit"
|
||||||
|
@ -9,6 +10,7 @@ import (
|
||||||
"github.com/openobservatory/gooni/internal/cli/version"
|
"github.com/openobservatory/gooni/internal/cli/version"
|
||||||
"github.com/openobservatory/gooni/internal/colors"
|
"github.com/openobservatory/gooni/internal/colors"
|
||||||
"github.com/openobservatory/gooni/internal/database"
|
"github.com/openobservatory/gooni/internal/database"
|
||||||
|
"github.com/openobservatory/gooni/internal/output"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Nettest interface. Every Nettest should implement this.
|
// Nettest interface. Every Nettest should implement this.
|
||||||
|
@ -176,6 +178,9 @@ func (c *Controller) Init(nt *mk.Nettest) error {
|
||||||
// OnProgress should be called when a new progress event is available.
|
// OnProgress should be called when a new progress event is available.
|
||||||
func (c *Controller) OnProgress(perc float64, msg string) {
|
func (c *Controller) OnProgress(perc float64, msg string) {
|
||||||
log.Debugf("OnProgress: %f - %s", perc, msg)
|
log.Debugf("OnProgress: %f - %s", perc, msg)
|
||||||
|
|
||||||
|
key := fmt.Sprintf("%T", c.nt)
|
||||||
|
output.Progress(key, perc, msg)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Entry is an opaque measurement entry
|
// Entry is an opaque measurement entry
|
||||||
|
|
Loading…
Reference in New Issue
Block a user