Add functions for structured progress logging

This commit is contained in:
Arturo Filastò 2018-03-22 15:22:29 +01:00
parent 58e452ea4e
commit e852713ed7
5 changed files with 54 additions and 7 deletions

View File

@ -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{

View File

@ -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)
}

14
internal/output/output.go Normal file
View 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)
}

View File

@ -82,7 +82,7 @@ var NettestGroups = map[string]NettestGroup{
return string(summaryBytes), nil
},
},
"middleboxes": NettestGroup{
"middlebox": NettestGroup{
Label: "Middleboxes",
Nettests: []nettests.Nettest{
middlebox.HTTPInvalidRequestLine{},

View File

@ -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