Extract Recording Code

Signed-off-by: Jean-Laurent de Morlhon <jeanlaurent@morlhon.net>
This commit is contained in:
Jean-Laurent de Morlhon 2015-12-16 16:35:19 +01:00
parent fca8c187b1
commit 7281142868
3 changed files with 74 additions and 34 deletions

View File

@ -4,25 +4,22 @@ import (
"fmt"
"io"
"os"
"sync"
)
type FmtMachineLogger struct {
out io.Writer
err io.Writer
debug bool
historyLock *sync.Mutex
history []string
out io.Writer
err io.Writer
debug bool
history *HistoryRecorder
}
// NewFmtMachineLogger creates a MachineLogger implementation used by the drivers
func NewFmtMachineLogger() MachineLogger {
return &FmtMachineLogger{
out: os.Stdout,
err: os.Stderr,
debug: false,
historyLock: &sync.Mutex{},
history: []string{},
out: os.Stdout,
err: os.Stderr,
debug: false,
history: NewHistoryRecorder(),
}
}
@ -39,73 +36,61 @@ func (ml *FmtMachineLogger) SetErr(err io.Writer) {
}
func (ml *FmtMachineLogger) Debug(args ...interface{}) {
ml.record(args...)
ml.history.Record(args...)
if ml.debug {
fmt.Fprintln(ml.err, args...)
}
}
func (ml *FmtMachineLogger) Debugf(fmtString string, args ...interface{}) {
ml.recordf(fmtString, args...)
ml.history.Recordf(fmtString, args...)
if ml.debug {
fmt.Fprintf(ml.err, fmtString+"\n", args...)
}
}
func (ml *FmtMachineLogger) Error(args ...interface{}) {
ml.record(args...)
ml.history.Record(args...)
fmt.Fprintln(ml.err, args...)
}
func (ml *FmtMachineLogger) Errorf(fmtString string, args ...interface{}) {
ml.recordf(fmtString, args...)
ml.history.Recordf(fmtString, args...)
fmt.Fprintf(ml.err, fmtString+"\n", args...)
}
func (ml *FmtMachineLogger) Info(args ...interface{}) {
ml.record(args...)
ml.history.Record(args...)
fmt.Fprintln(ml.out, args...)
}
func (ml *FmtMachineLogger) Infof(fmtString string, args ...interface{}) {
ml.recordf(fmtString, args...)
ml.history.Recordf(fmtString, args...)
fmt.Fprintf(ml.out, fmtString+"\n", args...)
}
func (ml *FmtMachineLogger) Fatal(args ...interface{}) {
ml.record(args...)
ml.history.Record(args...)
fmt.Fprintln(ml.err, args...)
os.Exit(1)
}
func (ml *FmtMachineLogger) Fatalf(fmtString string, args ...interface{}) {
ml.recordf(fmtString, args...)
ml.history.Recordf(fmtString, args...)
fmt.Fprintf(ml.err, fmtString+"\n", args...)
os.Exit(1)
}
func (ml *FmtMachineLogger) Warn(args ...interface{}) {
ml.record(args...)
ml.history.Record(args...)
fmt.Fprintln(ml.out, args...)
}
func (ml *FmtMachineLogger) Warnf(fmtString string, args ...interface{}) {
ml.recordf(fmtString, args...)
ml.history.Recordf(fmtString, args...)
fmt.Fprintf(ml.out, fmtString+"\n", args...)
}
func (ml *FmtMachineLogger) History() []string {
return ml.history
}
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...))
return ml.history.records
}

View File

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

View File

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