From 1062ec5aa83d7cef647493179bf3f38dc745018a Mon Sep 17 00:00:00 2001 From: David Moles Date: Mon, 14 Jan 2019 15:52:39 -0800 Subject: [PATCH] Add Pretty interface --- internal/logging/logger.go | 18 ++++++++++-- internal/logging/logger_test.go | 22 +++----------- internal/logging/logging_test.go | 35 +++++++++++++++++++++- internal/logging/pretty.go | 5 ++++ internal/logging/pretty_test.go | 45 +++++++++++++++++++++++++++++ internal/streaming/progress_test.go | 3 ++ 6 files changed, 107 insertions(+), 21 deletions(-) create mode 100644 internal/logging/pretty.go create mode 100644 internal/logging/pretty_test.go diff --git a/internal/logging/logger.go b/internal/logging/logger.go index a9b1d64..6637ecd 100644 --- a/internal/logging/logger.go +++ b/internal/logging/logger.go @@ -45,7 +45,8 @@ 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) } @@ -53,12 +54,25 @@ func (l infoLogger) Info(a ...interface{}) { // 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 diff --git a/internal/logging/logger_test.go b/internal/logging/logger_test.go index 0b0a4f2..0600301 100644 --- a/internal/logging/logger_test.go +++ b/internal/logging/logger_test.go @@ -1,8 +1,8 @@ package logging import ( - "strings" "fmt" + "strings" . "gopkg.in/check.v1" ) @@ -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 @@ -153,4 +137,6 @@ func (s *LoggerSuite) TestVerboseFlag(c *C) { } c.Assert(logger.String(), Equals, strExpected) } -} \ No newline at end of file +} + + diff --git a/internal/logging/logging_test.go b/internal/logging/logging_test.go index c5bd1a3..69a5b5b 100644 --- a/internal/logging/logging_test.go +++ b/internal/logging/logging_test.go @@ -1,9 +1,42 @@ package logging import ( + "fmt" "testing" . "gopkg.in/check.v1" ) -func Test(t *testing.T) { TestingT(t) } \ No newline at end of file +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{}" +} diff --git a/internal/logging/pretty.go b/internal/logging/pretty.go new file mode 100644 index 0000000..bbc6b63 --- /dev/null +++ b/internal/logging/pretty.go @@ -0,0 +1,5 @@ +package logging + +type Pretty interface { + Pretty() string +} diff --git a/internal/logging/pretty_test.go b/internal/logging/pretty_test.go new file mode 100644 index 0000000..2a70a6c --- /dev/null +++ b/internal/logging/pretty_test.go @@ -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?") +} diff --git a/internal/streaming/progress_test.go b/internal/streaming/progress_test.go index 2e08bc6..23e045c 100644 --- a/internal/streaming/progress_test.go +++ b/internal/streaming/progress_test.go @@ -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