Skip to content

Commit

Permalink
Add Pretty interface
Browse files Browse the repository at this point in the history
  • Loading branch information
David Moles committed Jan 14, 2019
1 parent 9ca2c70 commit 1062ec5
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 21 deletions.
18 changes: 16 additions & 2 deletions internal/logging/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,20 +45,34 @@ type infoLogger struct {

// Logger.Info() implementation: log to stderr
func (l infoLogger) Info(a ...interface{}) {
_, err := fmt.Fprintln(l.out, a...)
pretty := prettify(a...)
_, err := fmt.Fprintln(l.out, pretty...)
if err != nil {
logFatal(err)
}
}

// Logger.Infof() implementation: log to stderr
func (l infoLogger) Infof(format string, a ...interface{}) {
_, err := fmt.Fprintf(l.out, format, a...)
pretty := prettify(a...)
_, err := fmt.Fprintf(l.out, format, pretty...)
if err != nil {
logFatal(err)
}
}

func prettify(a ...interface{}) []interface{} {
var pretty []interface{}
for _, v := range a {
if p, ok := v.(Pretty); ok {
pretty = append(pretty, p.Pretty())
} else {
pretty = append(pretty, v)
}
}
return pretty
}

// ------------------------------
// terseLogger

Expand Down
22 changes: 4 additions & 18 deletions internal/logging/logger_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package logging

import (
"strings"
"fmt"
"strings"

. "gopkg.in/check.v1"
)
Expand Down Expand Up @@ -43,22 +43,6 @@ func (s *LoggerSuite) logFatal(v ...interface{}) {
s.fatals = append(s.fatals, fmt.Sprint(v...))
}

type StringableWriter interface {
Write(p []byte) (n int, err error)
String() string
}

type FailWriter struct {

}

func (f FailWriter) Write(p []byte) (n int, err error) {
return 0, fmt.Errorf("failed to write %v", p)
}

func (f FailWriter) String() string {
return "FailWriter{}"
}

// ------------------------------------------------------------
// Tests
Expand Down Expand Up @@ -153,4 +137,6 @@ func (s *LoggerSuite) TestVerboseFlag(c *C) {
}
c.Assert(logger.String(), Equals, strExpected)
}
}
}


35 changes: 34 additions & 1 deletion internal/logging/logging_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,42 @@
package logging

import (
"fmt"
"testing"

. "gopkg.in/check.v1"
)

func Test(t *testing.T) { TestingT(t) }
func Test(t *testing.T) { TestingT(t) }

// ------------------------------------------------------------
// Helper types

type Prettifiable struct {
Val interface{}
}

func (p Prettifiable) String() string {
return fmt.Sprintf("Prettifiable{ Val: %v }", p.Val)
}

func (p Prettifiable) Pretty() string {
return fmt.Sprintf("prettified %v", p.Val)
}

type StringableWriter interface {
Write(p []byte) (n int, err error)
String() string
}

type FailWriter struct {

}

func (f FailWriter) Write(p []byte) (n int, err error) {
return 0, fmt.Errorf("failed to write %v", p)
}

func (f FailWriter) String() string {
return "FailWriter{}"
}
5 changes: 5 additions & 0 deletions internal/logging/pretty.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package logging

type Pretty interface {
Pretty() string
}
45 changes: 45 additions & 0 deletions internal/logging/pretty_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package logging

import (
"strings"

. "gopkg.in/check.v1"
)

// ------------------------------------------------------------
// Fixture

type PrettySuite struct {
out StringableWriter
logger Logger
}

var _ = Suite(&PrettySuite{})

func (s *PrettySuite) SetUpTest(c *C) {
s.out = &strings.Builder{}
logger := NewLogger(true).(verboseLogger)
logger.out = s.out
s.logger = logger
}

func (s *PrettySuite) TearDownTest(c *C) {
s.out = nil
}

// ------------------------------------------------------------
// Tests

func (s *PrettySuite) TestPrettyInfo(c *C) {
p := Prettifiable{"msg"}

s.logger.Info(p)
c.Assert(s.out.String(), Equals, p.Pretty() + "\n")
}

func (s *PrettySuite) TestPrettyInfof(c *C) {
p := Prettifiable{"msg"}

s.logger.Infof("Is %v pretty?", p)
c.Assert(s.out.String(), Equals, "Is " + p.Pretty() + " pretty?")
}
3 changes: 3 additions & 0 deletions internal/streaming/progress_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ func (s *ProgressSuite) TestInfoTo(c *C) {
c.Assert(s.logger.Infos[0], Equals, expected)
}

// ------------------------------------------------------------
// Helper types

type CapturingLogger struct {
Infos []string
Details []string
Expand Down

0 comments on commit 1062ec5

Please sign in to comment.