From e852713ed7122e7ab9106a437d44d738031c1337 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arturo=20Filast=C3=B2?= Date: Thu, 22 Mar 2018 15:22:29 +0100 Subject: [PATCH] Add functions for structured progress logging --- internal/cli/run/run.go | 7 ++++++- internal/log/handlers/cli/cli.go | 33 +++++++++++++++++++++++++++----- internal/output/output.go | 14 ++++++++++++++ nettests/groups/groups.go | 2 +- nettests/nettests.go | 5 +++++ 5 files changed, 54 insertions(+), 7 deletions(-) create mode 100644 internal/output/output.go diff --git a/internal/cli/run/run.go b/internal/cli/run/run.go index 10f71e3..c5368ba 100644 --- a/internal/cli/run/run.go +++ b/internal/cli/run/run.go @@ -1,6 +1,7 @@ package run import ( + "errors" "fmt" "path/filepath" "time" @@ -25,7 +26,11 @@ func init() { log.Errorf("%s", 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) result, err := database.CreateResult(ctx.DB, database.Result{ diff --git a/internal/log/handlers/cli/cli.go b/internal/log/handlers/cli/cli.go index 10d7fba..7e137ba 100644 --- a/internal/log/handlers/cli/cli.go +++ b/internal/log/handlers/cli/cli.go @@ -60,15 +60,25 @@ func New(w io.Writer) *Handler { } } -// HandleLog implements log.Handler. -func (h *Handler) HandleLog(e *log.Entry) error { +// TypedLog is used for handling special "typed" logs to the CLI +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] level := Strings[e.Level] 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) for _, name := range names { @@ -82,3 +92,16 @@ func (h *Handler) HandleLog(e *log.Entry) error { 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) +} diff --git a/internal/output/output.go b/internal/output/output.go new file mode 100644 index 0000000..bf76672 --- /dev/null +++ b/internal/output/output.go @@ -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) +} diff --git a/nettests/groups/groups.go b/nettests/groups/groups.go index 9a1df41..2eb0440 100644 --- a/nettests/groups/groups.go +++ b/nettests/groups/groups.go @@ -82,7 +82,7 @@ var NettestGroups = map[string]NettestGroup{ return string(summaryBytes), nil }, }, - "middleboxes": NettestGroup{ + "middlebox": NettestGroup{ Label: "Middleboxes", Nettests: []nettests.Nettest{ middlebox.HTTPInvalidRequestLine{}, diff --git a/nettests/nettests.go b/nettests/nettests.go index ad189b2..bc1cae1 100644 --- a/nettests/nettests.go +++ b/nettests/nettests.go @@ -2,6 +2,7 @@ package nettests import ( "encoding/json" + "fmt" "github.com/apex/log" "github.com/measurement-kit/go-measurement-kit" @@ -9,6 +10,7 @@ import ( "github.com/openobservatory/gooni/internal/cli/version" "github.com/openobservatory/gooni/internal/colors" "github.com/openobservatory/gooni/internal/database" + "github.com/openobservatory/gooni/internal/output" ) // 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. func (c *Controller) OnProgress(perc float64, msg string) { log.Debugf("OnProgress: %f - %s", perc, msg) + + key := fmt.Sprintf("%T", c.nt) + output.Progress(key, perc, msg) } // Entry is an opaque measurement entry