refactor: make measurex depend on measurexlite (#892)
This diff makes `measurex` depend on `measurexlite` rather than the other way around. While there, add unit tests. Closes https://github.com/ooni/probe/issues/2240
This commit is contained in:
@@ -1,12 +1,6 @@
|
||||
package measurex
|
||||
|
||||
// NewFailure creates a serializable failure from an error. We
|
||||
// cannot round trip an error using JSON, so we serialize to this
|
||||
// intermediate format that is a sort of Optional<string>.
|
||||
func NewFailure(err error) *string {
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
s := err.Error()
|
||||
return &s
|
||||
}
|
||||
import "github.com/ooni/probe-cli/v3/internal/measurexlite"
|
||||
|
||||
// NewFailure is an alias for measurexlite.NewFailure
|
||||
var NewFailure = measurexlite.NewFailure
|
||||
|
||||
@@ -1,63 +1,15 @@
|
||||
package measurex
|
||||
|
||||
import "github.com/ooni/probe-cli/v3/internal/measurexlite"
|
||||
|
||||
//
|
||||
// Logger
|
||||
//
|
||||
// Code for logging
|
||||
//
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sync"
|
||||
"time"
|
||||
// NewOperationLogger is an alias for measurex.NewOperationLogger.
|
||||
var NewOperationLogger = measurexlite.NewOperationLogger
|
||||
|
||||
"github.com/ooni/probe-cli/v3/internal/model"
|
||||
)
|
||||
|
||||
// NewOperationLogger creates a new logger that logs
|
||||
// about an in-progress operation.
|
||||
func NewOperationLogger(logger model.Logger, format string, v ...interface{}) *OperationLogger {
|
||||
ol := &OperationLogger{
|
||||
sighup: make(chan interface{}),
|
||||
logger: logger,
|
||||
once: &sync.Once{},
|
||||
message: fmt.Sprintf(format, v...),
|
||||
wg: &sync.WaitGroup{},
|
||||
}
|
||||
ol.wg.Add(1)
|
||||
go ol.logloop()
|
||||
return ol
|
||||
}
|
||||
|
||||
// OperationLogger logs about an in-progress operation
|
||||
type OperationLogger struct {
|
||||
logger model.Logger
|
||||
message string
|
||||
once *sync.Once
|
||||
sighup chan interface{}
|
||||
wg *sync.WaitGroup
|
||||
}
|
||||
|
||||
func (ol *OperationLogger) logloop() {
|
||||
defer ol.wg.Done()
|
||||
timer := time.NewTimer(500 * time.Millisecond)
|
||||
defer timer.Stop()
|
||||
select {
|
||||
case <-timer.C:
|
||||
ol.logger.Infof("%s... in progress", ol.message)
|
||||
case <-ol.sighup:
|
||||
// we'll emit directly in stop
|
||||
}
|
||||
}
|
||||
|
||||
func (ol *OperationLogger) Stop(err error) {
|
||||
ol.once.Do(func() {
|
||||
close(ol.sighup)
|
||||
ol.wg.Wait()
|
||||
if err != nil {
|
||||
ol.logger.Infof("%s... %s", ol.message, err.Error())
|
||||
return
|
||||
}
|
||||
ol.logger.Infof("%s... ok", ol.message)
|
||||
})
|
||||
}
|
||||
// OperationLogger is an alias for measurex.OperationLogger.
|
||||
type OperationLogger = measurexlite.OperationLogger
|
||||
|
||||
Reference in New Issue
Block a user