2018-02-21 16:06:30 +01:00
|
|
|
|
package cli
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"io"
|
|
|
|
|
"os"
|
2018-06-22 11:53:10 +02:00
|
|
|
|
"strings"
|
2018-02-21 16:06:30 +01:00
|
|
|
|
"sync"
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
"github.com/apex/log"
|
|
|
|
|
"github.com/fatih/color"
|
|
|
|
|
colorable "github.com/mattn/go-colorable"
|
2018-06-25 16:31:44 +02:00
|
|
|
|
"github.com/ooni/probe-cli/internal/util"
|
2018-02-21 16:06:30 +01:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// Default handler outputting to stderr.
|
2018-09-18 09:55:23 +02:00
|
|
|
|
var Default = New(os.Stdout)
|
2018-02-21 16:06:30 +01:00
|
|
|
|
|
|
|
|
|
// start time.
|
|
|
|
|
var start = time.Now()
|
|
|
|
|
|
|
|
|
|
var bold = color.New(color.Bold)
|
|
|
|
|
|
|
|
|
|
// Colors mapping.
|
|
|
|
|
var Colors = [...]*color.Color{
|
|
|
|
|
log.DebugLevel: color.New(color.FgWhite),
|
|
|
|
|
log.InfoLevel: color.New(color.FgBlue),
|
|
|
|
|
log.WarnLevel: color.New(color.FgYellow),
|
|
|
|
|
log.ErrorLevel: color.New(color.FgRed),
|
|
|
|
|
log.FatalLevel: color.New(color.FgRed),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Strings mapping.
|
|
|
|
|
var Strings = [...]string{
|
|
|
|
|
log.DebugLevel: "•",
|
|
|
|
|
log.InfoLevel: "•",
|
|
|
|
|
log.WarnLevel: "•",
|
|
|
|
|
log.ErrorLevel: "⨯",
|
|
|
|
|
log.FatalLevel: "⨯",
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Handler implementation.
|
|
|
|
|
type Handler struct {
|
|
|
|
|
mu sync.Mutex
|
|
|
|
|
Writer io.Writer
|
|
|
|
|
Padding int
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// New handler.
|
|
|
|
|
func New(w io.Writer) *Handler {
|
|
|
|
|
if f, ok := w.(*os.File); ok {
|
|
|
|
|
return &Handler{
|
|
|
|
|
Writer: colorable.NewColorable(f),
|
|
|
|
|
Padding: 3,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return &Handler{
|
|
|
|
|
Writer: w,
|
|
|
|
|
Padding: 3,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-22 11:53:10 +02:00
|
|
|
|
func logSectionTitle(w io.Writer, f log.Fields) error {
|
|
|
|
|
colWidth := 24
|
|
|
|
|
|
|
|
|
|
title := f.Get("title").(string)
|
|
|
|
|
fmt.Fprintf(w, "┏"+strings.Repeat("━", colWidth+2)+"┓\n")
|
2018-06-25 16:31:44 +02:00
|
|
|
|
fmt.Fprintf(w, "┃ %s ┃\n", util.RightPad(title, colWidth))
|
2018-06-22 11:53:10 +02:00
|
|
|
|
fmt.Fprintf(w, "┗"+strings.Repeat("━", colWidth+2)+"┛\n")
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-22 14:25:30 +02:00
|
|
|
|
func logTable(w io.Writer, f log.Fields) error {
|
|
|
|
|
color := color.New(color.FgBlue)
|
|
|
|
|
|
|
|
|
|
names := f.Names()
|
|
|
|
|
|
|
|
|
|
var lines []string
|
|
|
|
|
colWidth := 0
|
|
|
|
|
for _, name := range names {
|
|
|
|
|
if name == "type" {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
line := fmt.Sprintf("%s: %s", color.Sprint(name), f.Get(name))
|
2018-06-25 16:31:44 +02:00
|
|
|
|
lineLength := util.EscapeAwareRuneCountInString(line)
|
2018-06-22 14:25:30 +02:00
|
|
|
|
lines = append(lines, line)
|
|
|
|
|
if colWidth < lineLength {
|
|
|
|
|
colWidth = lineLength
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fmt.Fprintf(w, "┏"+strings.Repeat("━", colWidth+2)+"┓\n")
|
|
|
|
|
for _, line := range lines {
|
|
|
|
|
fmt.Fprintf(w, "┃ %s ┃\n",
|
2018-06-25 16:31:44 +02:00
|
|
|
|
util.RightPad(line, colWidth),
|
2018-06-22 14:25:30 +02:00
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
fmt.Fprintf(w, "┗"+strings.Repeat("━", colWidth+2)+"┛\n")
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-22 15:22:29 +01:00
|
|
|
|
// TypedLog is used for handling special "typed" logs to the CLI
|
|
|
|
|
func (h *Handler) TypedLog(t string, e *log.Entry) error {
|
|
|
|
|
switch t {
|
Integrate further with ooni/probe-engine: episode two (#46)
* utils/geoip.go: use github.com/ooni/probe-engine
Let's start using the engine by rewriting utils/geoip.go to
be just a thin wrapper around the engine functionality.
* Ready for review
* Checkpoint: the im tests are converted
Still have some doubts with respect to the variables that
are passed to MK via probe-engine. Will double check.
* fix(i/c/r/run.go): write the correct logic
* nettests: one more comment and also fix a format string
* Tweak previous
* progress
* Fix doofus
* better comment
* XXX => actionable comment
* Add glue to simplify test keys management
Making the concept of measurement more abstract in the engine is
not feasible because, when submitting a measurement, we need to
modify it to update the report ID and the measurement ID. Therefore,
returning a serialized measurement is not a good idea. We will
keep using a model.Measurement in the engine.
Changing model.Measurement.TestKeys's type from a `interface{}`
pointing to a well defined data structure to `map[string]interface{}`
is a regression because means that we are moving from code that
has a clear and defined structure to code that is more complicated
to parse and validate. Since we're already suffering havily from
the lack of a good schema, I'm not going to make the situation
worst by worsening the engine. At least for ndt7 and psiphon, we
now have a good schema and I don't want to lose that.
However, the current code in this repository is expecting the
test keys to be a `map[string]interface{}`. This choice was
dictated by the fact that we receive a JSON from Measurement Kit
and by the fact that there's not a clear schema.
To solve this tension, in this commit I am going to write glue
adapter code that makes sure that the TestKeys of a Measurement
are converted to `map[string]interface{}`. This will be done
using a type cast where possible and JSON serialization and parsing
otherwise. In a perfect world, glue is not a good idea, but in a
real world it may actually be useful.
When all tests in the engine will have a clear Go data structure,
we'll then remove the glue and just cast to the proper data
structure from `interface{}` where required.
* nettests/performance: use probe-engine
* go.{mod,sum}: upgrade to latest probe-engine
* nettests/middlebox: use ooni/probe-engine
* Update to the latest probe-engine
* web_connectivity: rewrite to use probe-engine
* Cosmetic change suggested by @hellais
* nettests/nettests.go: remove unused code
* nettests/nettests.go: fix progress
* nettests/nettests.go: remove go-measurement-kit code
* We don't depend on go-measurement-kit anymore
* Improve non-verbose output where possible
See also: https://github.com/measurement-kit/measurement-kit/issues/1856
* Make web_connectivity output pleasant
* Update to the latest probe-engine
* nettests/nettests.go: honour sharing settings
* Update to the latest probe-engine
* Use log.WithFields for probe-engine
* Update go.mod go.sum
* Revert "Update go.mod go.sum"
This reverts commit 5ecd38d8236f4a4e9b77ddb8e8a0d1e3cdd4b818.
* Revert "Revert "Update go.mod go.sum""
This reverts commit 6114b31eca98826112032776bd0feff02d763ecd.
* Upgrade ooni/probe-engine
* Unset GOPATH before running go build commands
* Dockefile: fix linux build by using latest
* Update to the latest ooni/probe-engine
```
go get -u github.com/ooni/probe-engine
go mod tidy
```
* Repair build
2019-08-15 18:08:43 +02:00
|
|
|
|
case "engine":
|
|
|
|
|
fmt.Fprintf(h.Writer, "[engine] %s\n", e.Message)
|
|
|
|
|
return nil
|
2018-03-22 15:22:29 +01:00
|
|
|
|
case "progress":
|
2018-09-10 16:56:32 +02:00
|
|
|
|
perc := e.Fields.Get("percentage").(float64) * 100
|
2019-11-14 12:01:40 +01:00
|
|
|
|
eta := e.Fields.Get("eta").(float64)
|
|
|
|
|
s := fmt.Sprintf(" %s %-25s (%ss left)",
|
2018-09-10 16:56:32 +02:00
|
|
|
|
bold.Sprintf("%.2f%%", perc),
|
2019-11-14 12:01:40 +01:00
|
|
|
|
e.Message,
|
|
|
|
|
bold.Sprintf("%.2f", eta))
|
2018-09-10 16:56:32 +02:00
|
|
|
|
fmt.Fprint(h.Writer, s)
|
2018-07-14 17:19:06 +02:00
|
|
|
|
fmt.Fprintln(h.Writer)
|
2018-09-10 16:56:32 +02:00
|
|
|
|
return nil
|
2018-06-22 14:25:30 +02:00
|
|
|
|
case "table":
|
|
|
|
|
return logTable(h.Writer, e.Fields)
|
2018-09-13 14:54:56 +02:00
|
|
|
|
case "measurement_item":
|
|
|
|
|
return logMeasurementItem(h.Writer, e.Fields)
|
2019-10-02 18:23:14 +02:00
|
|
|
|
case "measurement_json":
|
|
|
|
|
return logMeasurementJSON(h.Writer, e.Fields)
|
2018-09-13 14:54:56 +02:00
|
|
|
|
case "measurement_summary":
|
|
|
|
|
return logMeasurementSummary(h.Writer, e.Fields)
|
2018-05-03 18:40:52 +02:00
|
|
|
|
case "result_item":
|
|
|
|
|
return logResultItem(h.Writer, e.Fields)
|
2018-06-22 11:53:10 +02:00
|
|
|
|
case "result_summary":
|
|
|
|
|
return logResultSummary(h.Writer, e.Fields)
|
|
|
|
|
case "section_title":
|
|
|
|
|
return logSectionTitle(h.Writer, e.Fields)
|
2018-03-22 15:22:29 +01:00
|
|
|
|
default:
|
|
|
|
|
return h.DefaultLog(e)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// DefaultLog is the default way of printing out logs
|
|
|
|
|
func (h *Handler) DefaultLog(e *log.Entry) error {
|
2018-02-21 16:06:30 +01:00
|
|
|
|
color := Colors[e.Level]
|
|
|
|
|
level := Strings[e.Level]
|
|
|
|
|
names := e.Fields.Names()
|
|
|
|
|
|
2018-06-22 13:56:42 +02:00
|
|
|
|
s := color.Sprintf("%s %-25s", bold.Sprintf("%*s", h.Padding+1, level), e.Message)
|
2018-02-21 16:06:30 +01:00
|
|
|
|
for _, name := range names {
|
|
|
|
|
if name == "source" {
|
|
|
|
|
continue
|
|
|
|
|
}
|
2018-09-10 18:03:32 +02:00
|
|
|
|
s += fmt.Sprintf(" %s=%v", color.Sprint(name), e.Fields.Get(name))
|
2018-02-21 16:06:30 +01:00
|
|
|
|
}
|
|
|
|
|
|
2018-09-10 18:03:32 +02:00
|
|
|
|
fmt.Fprint(h.Writer, s)
|
2018-07-14 17:19:06 +02:00
|
|
|
|
fmt.Fprintln(h.Writer)
|
2018-02-21 16:06:30 +01:00
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2018-03-22 15:22:29 +01:00
|
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
|
}
|