mirror of https://github.com/docker/docs.git
Extract Recording Code
Signed-off-by: Jean-Laurent de Morlhon <jeanlaurent@morlhon.net>
This commit is contained in:
parent
fca8c187b1
commit
7281142868
|
|
@ -4,15 +4,13 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"sync"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type FmtMachineLogger struct {
|
type FmtMachineLogger struct {
|
||||||
out io.Writer
|
out io.Writer
|
||||||
err io.Writer
|
err io.Writer
|
||||||
debug bool
|
debug bool
|
||||||
historyLock *sync.Mutex
|
history *HistoryRecorder
|
||||||
history []string
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewFmtMachineLogger creates a MachineLogger implementation used by the drivers
|
// NewFmtMachineLogger creates a MachineLogger implementation used by the drivers
|
||||||
|
|
@ -21,8 +19,7 @@ func NewFmtMachineLogger() MachineLogger {
|
||||||
out: os.Stdout,
|
out: os.Stdout,
|
||||||
err: os.Stderr,
|
err: os.Stderr,
|
||||||
debug: false,
|
debug: false,
|
||||||
historyLock: &sync.Mutex{},
|
history: NewHistoryRecorder(),
|
||||||
history: []string{},
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -39,73 +36,61 @@ func (ml *FmtMachineLogger) SetErr(err io.Writer) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ml *FmtMachineLogger) Debug(args ...interface{}) {
|
func (ml *FmtMachineLogger) Debug(args ...interface{}) {
|
||||||
ml.record(args...)
|
ml.history.Record(args...)
|
||||||
if ml.debug {
|
if ml.debug {
|
||||||
fmt.Fprintln(ml.err, args...)
|
fmt.Fprintln(ml.err, args...)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ml *FmtMachineLogger) Debugf(fmtString string, args ...interface{}) {
|
func (ml *FmtMachineLogger) Debugf(fmtString string, args ...interface{}) {
|
||||||
ml.recordf(fmtString, args...)
|
ml.history.Recordf(fmtString, args...)
|
||||||
if ml.debug {
|
if ml.debug {
|
||||||
fmt.Fprintf(ml.err, fmtString+"\n", args...)
|
fmt.Fprintf(ml.err, fmtString+"\n", args...)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ml *FmtMachineLogger) Error(args ...interface{}) {
|
func (ml *FmtMachineLogger) Error(args ...interface{}) {
|
||||||
ml.record(args...)
|
ml.history.Record(args...)
|
||||||
fmt.Fprintln(ml.err, args...)
|
fmt.Fprintln(ml.err, args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ml *FmtMachineLogger) Errorf(fmtString string, args ...interface{}) {
|
func (ml *FmtMachineLogger) Errorf(fmtString string, args ...interface{}) {
|
||||||
ml.recordf(fmtString, args...)
|
ml.history.Recordf(fmtString, args...)
|
||||||
fmt.Fprintf(ml.err, fmtString+"\n", args...)
|
fmt.Fprintf(ml.err, fmtString+"\n", args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ml *FmtMachineLogger) Info(args ...interface{}) {
|
func (ml *FmtMachineLogger) Info(args ...interface{}) {
|
||||||
ml.record(args...)
|
ml.history.Record(args...)
|
||||||
fmt.Fprintln(ml.out, args...)
|
fmt.Fprintln(ml.out, args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ml *FmtMachineLogger) Infof(fmtString string, args ...interface{}) {
|
func (ml *FmtMachineLogger) Infof(fmtString string, args ...interface{}) {
|
||||||
ml.recordf(fmtString, args...)
|
ml.history.Recordf(fmtString, args...)
|
||||||
fmt.Fprintf(ml.out, fmtString+"\n", args...)
|
fmt.Fprintf(ml.out, fmtString+"\n", args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ml *FmtMachineLogger) Fatal(args ...interface{}) {
|
func (ml *FmtMachineLogger) Fatal(args ...interface{}) {
|
||||||
ml.record(args...)
|
ml.history.Record(args...)
|
||||||
fmt.Fprintln(ml.err, args...)
|
fmt.Fprintln(ml.err, args...)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ml *FmtMachineLogger) Fatalf(fmtString string, args ...interface{}) {
|
func (ml *FmtMachineLogger) Fatalf(fmtString string, args ...interface{}) {
|
||||||
ml.recordf(fmtString, args...)
|
ml.history.Recordf(fmtString, args...)
|
||||||
fmt.Fprintf(ml.err, fmtString+"\n", args...)
|
fmt.Fprintf(ml.err, fmtString+"\n", args...)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ml *FmtMachineLogger) Warn(args ...interface{}) {
|
func (ml *FmtMachineLogger) Warn(args ...interface{}) {
|
||||||
ml.record(args...)
|
ml.history.Record(args...)
|
||||||
fmt.Fprintln(ml.out, args...)
|
fmt.Fprintln(ml.out, args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ml *FmtMachineLogger) Warnf(fmtString string, args ...interface{}) {
|
func (ml *FmtMachineLogger) Warnf(fmtString string, args ...interface{}) {
|
||||||
ml.recordf(fmtString, args...)
|
ml.history.Recordf(fmtString, args...)
|
||||||
fmt.Fprintf(ml.out, fmtString+"\n", args...)
|
fmt.Fprintf(ml.out, fmtString+"\n", args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ml *FmtMachineLogger) History() []string {
|
func (ml *FmtMachineLogger) History() []string {
|
||||||
return ml.history
|
return ml.history.records
|
||||||
}
|
|
||||||
|
|
||||||
func (ml *FmtMachineLogger) record(args ...interface{}) {
|
|
||||||
ml.historyLock.Lock()
|
|
||||||
defer ml.historyLock.Unlock()
|
|
||||||
ml.history = append(ml.history, fmt.Sprint(args...))
|
|
||||||
}
|
|
||||||
|
|
||||||
func (ml *FmtMachineLogger) recordf(fmtString string, args ...interface{}) {
|
|
||||||
ml.historyLock.Lock()
|
|
||||||
defer ml.historyLock.Unlock()
|
|
||||||
ml.history = append(ml.history, fmt.Sprintf(fmtString, args...))
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,34 @@
|
||||||
|
package log
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"sync"
|
||||||
|
)
|
||||||
|
|
||||||
|
type HistoryRecorder struct {
|
||||||
|
lock *sync.Mutex
|
||||||
|
records []string
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewHistoryRecorder() *HistoryRecorder {
|
||||||
|
return &HistoryRecorder{
|
||||||
|
lock: &sync.Mutex{},
|
||||||
|
records: []string{},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ml *HistoryRecorder) History() []string {
|
||||||
|
return ml.records
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ml *HistoryRecorder) Record(args ...interface{}) {
|
||||||
|
ml.lock.Lock()
|
||||||
|
defer ml.lock.Unlock()
|
||||||
|
ml.records = append(ml.records, fmt.Sprint(args...))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ml *HistoryRecorder) Recordf(fmtString string, args ...interface{}) {
|
||||||
|
ml.lock.Lock()
|
||||||
|
defer ml.lock.Unlock()
|
||||||
|
ml.records = append(ml.records, fmt.Sprintf(fmtString, args...))
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,21 @@
|
||||||
|
package log
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestRecording(t *testing.T) {
|
||||||
|
recorder := NewHistoryRecorder()
|
||||||
|
recorder.Record("foo")
|
||||||
|
recorder.Record("bar")
|
||||||
|
recorder.Record("qix")
|
||||||
|
assert.Equal(t, recorder.History(), []string{"foo", "bar", "qix"})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestFormattedRecording(t *testing.T) {
|
||||||
|
recorder := NewHistoryRecorder()
|
||||||
|
recorder.Recordf("%s, %s and %s", "foo", "bar", "qix")
|
||||||
|
assert.Equal(t, recorder.History()[0], "foo, bar and qix")
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue