Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions log/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ type SyncWriter struct {
w io.Writer
}

// ChildWriter returns the associated child io.Writer.
func (w *SyncWriter) ChildWriter() io.Writer {
return w.w
}

// NewSyncWriter returns a new SyncWriter. The returned writer is safe for
// concurrent use by multiple goroutines.
func NewSyncWriter(w io.Writer) *SyncWriter {
Expand Down
10 changes: 10 additions & 0 deletions log/sync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,13 @@ func TestSyncWriterConcurrency(t *testing.T) {
w = log.NewSyncWriter(w)
testConcurrency(t, log.NewLogfmtLogger(w), 10000)
}

func TestSyncWriterChildWriter(t *testing.T) {
want := &bytes.Buffer{}
w := log.NewSyncWriter(want)
got := w.ChildWriter()

if got != want {
t.Errorf("got %v, want %v", got, want)
}
}
6 changes: 4 additions & 2 deletions log/term/colorwriter_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,14 @@ type colorWriter struct {
// platform support for ANSI color codes. If w is not a terminal it is
// returned unmodified.
func NewColorWriter(w io.Writer) io.Writer {
if !IsTerminal(w) {
cw := resolveWriter(w)

if !IsTerminal(cw) {
return w
}

var csbi consoleScreenBufferInfo
handle := syscall.Handle(w.(fder).Fd())
handle := syscall.Handle(cw.(fder).Fd())
procGetConsoleScreenBufferInfo.Call(uintptr(handle), uintptr(unsafe.Pointer(&csbi)))

return &colorWriter{
Expand Down
14 changes: 13 additions & 1 deletion log/term/term.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,23 @@ import (
"github.com/go-kit/kit/log"
)

type parentWriter interface {
ChildWriter() io.Writer
}

func resolveWriter(w io.Writer) io.Writer {
Copy link
Author

@ereOn ereOn Mar 6, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: This method is a pass-through for all io.Writer instances that do not implement the parentWriter interface, thus not breaking the existing behavior.

if w, ok := w.(parentWriter); ok {
return resolveWriter(w.ChildWriter())
}

return w
}

// NewLogger returns a Logger that takes advantage of terminal features if
// possible. Log events are formatted by the Logger returned by newLogger. If
// w is a terminal each log event is colored according to the color function.
func NewLogger(w io.Writer, newLogger func(io.Writer) log.Logger, color func(keyvals ...interface{}) FgBgColor) log.Logger {
if !IsTerminal(w) {
if !IsTerminal(resolveWriter(w)) {
return newLogger(w)
}
return NewColorLogger(NewColorWriter(w), newLogger, color)
Expand Down