From cfb4e7e874e16491683b48f5ec7e56093a0680bd Mon Sep 17 00:00:00 2001 From: Martin Polden Date: Sat, 11 Sep 2021 14:04:11 +0200 Subject: cmd: Print only unique log lines --- cmd/unp/main.go | 11 +++++++++++ go.mod | 4 ++-- go.sum | 5 ++++- logutil/logutil.go | 40 ++++++++++++++++++++++++++++++++++++++++ logutil/logutil_test.go | 25 +++++++++++++++++++++++++ 5 files changed, 82 insertions(+), 3 deletions(-) create mode 100644 logutil/logutil.go create mode 100644 logutil/logutil_test.go diff --git a/cmd/unp/main.go b/cmd/unp/main.go index 0b08820..f868a8a 100644 --- a/cmd/unp/main.go +++ b/cmd/unp/main.go @@ -3,14 +3,25 @@ package main import ( "flag" "fmt" + "io" "log" + "os" + "github.com/mattn/go-isatty" + "github.com/mpolden/unp/logutil" "github.com/mpolden/unp/watcher" ) func init() { + var out io.Writer + if stderr := os.Stderr; isatty.IsTerminal(stderr.Fd()) { + out = logutil.NewUniqueWriter(stderr) + } else { + out = stderr + } log.SetPrefix("unp: ") log.SetFlags(0) + log.SetOutput(out) } func main() { diff --git a/go.mod b/go.mod index 05b96fd..0ba9196 100644 --- a/go.mod +++ b/go.mod @@ -3,9 +3,9 @@ module github.com/mpolden/unp go 1.17 require ( + github.com/mattn/go-isatty v0.0.14 github.com/mpolden/sfv v0.9.0 github.com/nwaples/rardecode v1.1.0 github.com/rjeczalik/notify v0.9.2 + golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c // indirect ) - -require golang.org/x/sys v0.0.0-20180926160741-c2ed4eda69e7 // indirect diff --git a/go.sum b/go.sum index b558f35..456e6e5 100644 --- a/go.sum +++ b/go.sum @@ -1,8 +1,11 @@ +github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y= +github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94= github.com/mpolden/sfv v0.9.0 h1:POHC8Js30xxOgMvgNLUEkJZh2fhOtx5NwK1pj7g9VvQ= github.com/mpolden/sfv v0.9.0/go.mod h1:EymWriacbRB9ZKQ21Vj+ahcIV8aq8G0FNluX6UNCcVk= github.com/nwaples/rardecode v1.1.0 h1:vSxaY8vQhOcVr4mm5e8XllHWTiM4JF507A0Katqw7MQ= github.com/nwaples/rardecode v1.1.0/go.mod h1:5DzqNKiOdpKKBH87u8VlvAnPZMXcGRhxWkRpHbbfGS0= github.com/rjeczalik/notify v0.9.2 h1:MiTWrPj55mNDHEiIX5YUSKefw/+lCQVoAFmD6oQm5w8= github.com/rjeczalik/notify v0.9.2/go.mod h1:aErll2f0sUX9PXZnVNyeiObbmTlk5jnMoCa4QEjJeqM= -golang.org/x/sys v0.0.0-20180926160741-c2ed4eda69e7 h1:bit1t3mgdR35yN0cX0G8orgLtOuyL9Wqxa1mccLB0ig= golang.org/x/sys v0.0.0-20180926160741-c2ed4eda69e7/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c h1:F1jZWGFhYfh0Ci55sIpILtKKK8p3i2/krTr0H1rg74I= +golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= diff --git a/logutil/logutil.go b/logutil/logutil.go new file mode 100644 index 0000000..467e57d --- /dev/null +++ b/logutil/logutil.go @@ -0,0 +1,40 @@ +package logutil + +import ( + "bytes" + "fmt" + "io" +) + +const ( + cursorUp = "\033[1A" + eraseEOL = "\033[K" +) + +type UniqueWriter struct { + w io.Writer + prev []byte + count int64 +} + +func NewUniqueWriter(w io.Writer) *UniqueWriter { return &UniqueWriter{w: w} } + +func (uw *UniqueWriter) Write(p []byte) (n int, err error) { + if len(p) == 0 { + return 0, nil + } + if bytes.Equal(uw.prev, p) { + uw.count++ + if p[len(p)-1] == '\n' { + p = p[:len(p)-1] + } + space := "" + if len(p) > 0 { + space = " " + } + return fmt.Fprintf(uw.w, "%s%s%s%s[repeated %d times]\n", cursorUp, eraseEOL, p, space, uw.count) + } + uw.prev = p + uw.count = 1 + return uw.w.Write(p) +} diff --git a/logutil/logutil_test.go b/logutil/logutil_test.go new file mode 100644 index 0000000..29f8849 --- /dev/null +++ b/logutil/logutil_test.go @@ -0,0 +1,25 @@ +package logutil + +import ( + "strings" + "testing" +) + +func TestWrite(t *testing.T) { + var sb strings.Builder + w := NewUniqueWriter(&sb) + + w.Write([]byte("l1\n")) + w.Write([]byte("l1\n")) + w.Write([]byte("l2\n")) + w.Write([]byte("\n")) + w.Write([]byte("\n")) + + want := "l1\n" + + "\033[1A\033[Kl1 [repeated 2 times]\n" + + "l2\n" + + "\n\033[1A\033[K[repeated 2 times]\n" + if got := sb.String(); got != want { + t.Errorf("got %q, want %q", got, want) + } +} -- cgit v1.2.3