From 6d4b3605f709df4fa316e9beef37f57371cb9bd5 Mon Sep 17 00:00:00 2001 From: Josiah Kiehl Date: Thu, 24 Jul 2014 13:37:44 -0700 Subject: [PATCH] Extract log utils into pkg/log Docker-DCO-1.1-Signed-off-by: Josiah Kiehl (github: capoferro) --- broadcastwriter/broadcastwriter.go | 4 +- httputils/resumablerequestreader.go | 5 +- log/log.go | 77 +++++++++++++++++++++++++++++ log/log_test.go | 37 ++++++++++++++ tarsum/tarsum.go | 7 +-- 5 files changed, 123 insertions(+), 7 deletions(-) create mode 100644 log/log.go create mode 100644 log/log_test.go diff --git a/broadcastwriter/broadcastwriter.go b/broadcastwriter/broadcastwriter.go index a16d8de..3bf1272 100644 --- a/broadcastwriter/broadcastwriter.go +++ b/broadcastwriter/broadcastwriter.go @@ -4,11 +4,11 @@ import ( "bytes" "encoding/json" "io" - "log" "sync" "time" "github.com/docker/docker/pkg/jsonlog" + "github.com/docker/docker/pkg/log" ) // BroadcastWriter accumulate multiple io.WriteCloser by stream. @@ -56,7 +56,7 @@ func (w *BroadcastWriter) Write(p []byte) (n int, err error) { } b, err := json.Marshal(jsonlog.JSONLog{Log: line, Stream: stream, Created: created}) if err != nil { - log.Printf("Error making JSON log line: %s", err) + log.Errorf("Error making JSON log line: %s", err) continue } b = append(b, '\n') diff --git a/httputils/resumablerequestreader.go b/httputils/resumablerequestreader.go index dbc9d9b..71533d3 100644 --- a/httputils/resumablerequestreader.go +++ b/httputils/resumablerequestreader.go @@ -3,9 +3,10 @@ package httputils import ( "fmt" "io" - "log" "net/http" "time" + + "github.com/docker/docker/pkg/log" ) type resumableRequestReader struct { @@ -71,7 +72,7 @@ func (r *resumableRequestReader) Read(p []byte) (n int, err error) { r.cleanUpResponse() } if err != nil && err != io.EOF { - log.Printf("encountered error during pull and clearing it before resume: %s", err) + log.Infof("encountered error during pull and clearing it before resume: %s", err) err = nil } return n, err diff --git a/log/log.go b/log/log.go new file mode 100644 index 0000000..53513a9 --- /dev/null +++ b/log/log.go @@ -0,0 +1,77 @@ +package log + +import ( + "fmt" + "io" + "os" + "runtime" + "strings" +) + +type priority int + +const ( + errorFormat = "[%s] %s:%d %s\n" + logFormat = "[%s] %s\n" + + fatal priority = iota + error + info + debug +) + +func (p priority) String() string { + switch p { + case fatal: + return "fatal" + case error: + return "error" + case info: + return "info" + case debug: + return "debug" + } + + return "" +} + +// Debug function, if the debug flag is set, then display. Do nothing otherwise +// If Docker is in damon mode, also send the debug info on the socket +func Debugf(format string, a ...interface{}) { + if os.Getenv("DEBUG") != "" { + logf(os.Stderr, debug, format, a...) + } +} + +func Infof(format string, a ...interface{}) { + logf(os.Stdout, info, format, a...) +} + +func Errorf(format string, a ...interface{}) { + logf(os.Stderr, error, format, a...) +} + +func Fatalf(format string, a ...interface{}) { + logf(os.Stderr, fatal, format, a...) + os.Exit(1) +} + +func logf(stream io.Writer, level priority, format string, a ...interface{}) { + var prefix string + + if level <= error || level == debug { + // Retrieve the stack infos + _, file, line, ok := runtime.Caller(2) + if !ok { + file = "" + line = -1 + } else { + file = file[strings.LastIndex(file, "/")+1:] + } + prefix = fmt.Sprintf(errorFormat, level.String(), file, line, format) + } else { + prefix = fmt.Sprintf(logFormat, level.String(), format) + } + + fmt.Fprintf(stream, prefix, a...) +} diff --git a/log/log_test.go b/log/log_test.go new file mode 100644 index 0000000..83ba5fd --- /dev/null +++ b/log/log_test.go @@ -0,0 +1,37 @@ +package log + +import ( + "bytes" + "regexp" + + "testing" +) + +func TestLogFatalf(t *testing.T) { + var output *bytes.Buffer + + tests := []struct { + Level priority + Format string + Values []interface{} + ExpectedPattern string + }{ + {fatal, "%d + %d = %d", []interface{}{1, 1, 2}, "\\[fatal\\] testing.go:\\d+ 1 \\+ 1 = 2"}, + {error, "%d + %d = %d", []interface{}{1, 1, 2}, "\\[error\\] testing.go:\\d+ 1 \\+ 1 = 2"}, + {info, "%d + %d = %d", []interface{}{1, 1, 2}, "\\[info\\] 1 \\+ 1 = 2"}, + {debug, "%d + %d = %d", []interface{}{1, 1, 2}, "\\[debug\\] testing.go:\\d+ 1 \\+ 1 = 2"}, + } + + for i, test := range tests { + output = &bytes.Buffer{} + logf(output, test.Level, test.Format, test.Values...) + + expected := regexp.MustCompile(test.ExpectedPattern) + if !expected.MatchString(output.String()) { + t.Errorf("[%d] Log output does not match expected pattern:\n\tExpected: %s\n\tOutput: %s", + i, + expected.String(), + output.String()) + } + } +} diff --git a/tarsum/tarsum.go b/tarsum/tarsum.go index bc017d0..32b5e42 100644 --- a/tarsum/tarsum.go +++ b/tarsum/tarsum.go @@ -7,12 +7,13 @@ import ( "encoding/hex" "hash" "io" - "log" "sort" "strconv" "strings" "github.com/docker/docker/vendor/src/code.google.com/p/go/src/pkg/archive/tar" + + "github.com/docker/docker/pkg/log" ) type TarSum struct { @@ -170,11 +171,11 @@ func (ts *TarSum) Sum(extra []byte) string { h.Write(extra) } for _, sum := range sums { - log.Printf("-->%s<--", sum) + log.Infof("-->%s<--", sum) h.Write([]byte(sum)) } checksum := "tarsum+sha256:" + hex.EncodeToString(h.Sum(nil)) - log.Printf("checksum processed: %s", checksum) + log.Infof("checksum processed: %s", checksum) return checksum }