Skip to content

Commit

Permalink
llog: tests and examples
Browse files Browse the repository at this point in the history
Signed-off-by: Chris Koch <[email protected]>
  • Loading branch information
hugelgupf committed Feb 7, 2024
1 parent abda872 commit 436f10e
Show file tree
Hide file tree
Showing 5 changed files with 261 additions and 5 deletions.
20 changes: 20 additions & 0 deletions llog/default2_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright 2024 the u-root Authors. All rights reserved
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package llog_test

import (
"log/slog"

"github.com/u-root/uio/llog"
)

func ExampleDefault_withtime() {
l := llog.Default()
l.Infof("An INFO level string")
l.Debugf("A DEBUG level that does not appear")

l.Level = llog.Level(slog.LevelDebug)
l.Debugf("A DEBUG level that appears")
}
36 changes: 36 additions & 0 deletions llog/default_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright 2024 the u-root Authors. All rights reserved
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package llog_test

import (
"log"
"log/slog"
"os"

"github.com/u-root/uio/llog"
)

func ExampleDefault() {
// Example is reproducible without date/time displayed.
log.SetFlags(0)
// Examples only go out to stdout.
log.SetOutput(os.Stdout)

l := llog.Default()
l.Infof("An INFO level string")
l.Debugf("A DEBUG level that does not appear")

l.Level = llog.Level(slog.LevelDebug)
l.Debugf("A DEBUG level that appears")

l.Warnf("I'm warning you")
l.Errorf("This is going to error")

// Output:
// INFO An INFO level string
// DEBUG A DEBUG level that appears
// WARN I'm warning you
// ERROR This is going to error
}
26 changes: 26 additions & 0 deletions llog/example_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright 2024 the u-root Authors. All rights reserved
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package llog_test

import (
"flag"
"log/slog"

"github.com/u-root/uio/llog"
)

func someFunc(v llog.Printf) {
v("logs at the given level %s", "foo")
}

func Example() {
l := llog.Default()
// If -v is set, l.Level becomes slog.LevelDebug.
l.Level.RegisterDebugFlag(flag.CommandLine, "v")
flag.Parse()

someFunc(l.Debugf)
someFunc(l.Printf(slog.LevelWarn))
}
10 changes: 5 additions & 5 deletions llog/levellog.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func SinkFor(p Printf) Sink {
// Prepend log level.
format = "%s " + format
args = append([]any{level}, args...)
p(format, args)
p(format, args...)
}
}

Expand All @@ -88,7 +88,7 @@ type Logger struct {
// uses l as the default log level.
//
// Logs with level >= l will be printed using p.
func New(p Printf, l Level) *Logger {
func New(l Level, p Printf) *Logger {
return &Logger{
Sink: SinkFor(p),
Level: l,
Expand All @@ -97,10 +97,10 @@ func New(p Printf, l Level) *Logger {

// Printf returns a Printf that can be passed around to log at the given level.
func (l *Logger) Printf(level slog.Level) Printf {
if l == nil || l.Sink == nil {
return func(fmt string, args ...any) {}
}
return func(fmt string, args ...any) {
if l == nil {
return
}
l.Logf(level, fmt, args...)
}
}
Expand Down
174 changes: 174 additions & 0 deletions llog/llog_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
// Copyright 2024 the u-root Authors. All rights reserved
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package llog

import (
"flag"
"fmt"
"log"
"log/slog"
"strconv"
"strings"
"testing"
)

func TestLevelFlag(t *testing.T) {
for _, tt := range []struct {
args []string
want Level
}{
{
args: []string{"-level=4"},
want: Level(slog.LevelWarn),
},
{
args: []string{},
want: Level(slog.LevelInfo),
},
} {
f := flag.NewFlagSet("", flag.ContinueOnError)

var v Level
v.RegisterLevelFlag(f, "level")
_ = f.Parse(tt.args)

if v != tt.want {
t.Errorf("Parse(%#v) = %v, want %v", tt.args, v, tt.want)
}
}

for _, tt := range []struct {
args []string
want Level
err error
}{
{
args: []string{"-v"},
want: Level(slog.LevelWarn),
},
{
args: []string{},
want: Level(slog.LevelInfo),
},
{
args: []string{"-v=true"},
want: Level(slog.LevelWarn),
},
{
args: []string{"-v=true", "-v=false"},
want: Level(slog.LevelWarn),
},
{
args: []string{"-v=foobar"},
want: Level(slog.LevelInfo),
err: strconv.ErrSyntax,
},
} {
f := flag.NewFlagSet("", flag.ContinueOnError)

var v Level
v.RegisterVerboseFlag(f, "v", slog.LevelWarn)
// Parse doesn't use %w.
if err := f.Parse(tt.args); err != tt.err && err != nil && !strings.Contains(err.Error(), tt.err.Error()) {
t.Errorf("Parse(%#v) = %v, want %v", tt.args, err, tt.err)
}
if v != tt.want {
t.Errorf("Parse(%#v) = %v, want %v", tt.args, v, tt.want)
}
}

for _, tt := range []struct {
args []string
want Level
err error
}{
{
args: []string{"-v"},
want: Level(slog.LevelDebug),
},
{
args: []string{},
want: Level(slog.LevelInfo),
},
{
args: []string{"-v=true"},
want: Level(slog.LevelDebug),
},
{
args: []string{"-v=true", "-v=false"},
want: Level(slog.LevelDebug),
},
{
args: []string{"-v=foobar"},
want: Level(slog.LevelInfo),
err: strconv.ErrSyntax,
},
} {
f := flag.NewFlagSet("", flag.ContinueOnError)

var v Level
v.RegisterDebugFlag(f, "v")
// Parse doesn't use %w.
if err := f.Parse(tt.args); err != tt.err && err != nil && !strings.Contains(err.Error(), tt.err.Error()) {
t.Errorf("Parse(%#v) = %v, want %v", tt.args, err, tt.err)
}
if v != tt.want {
t.Errorf("Parse(%#v) = %v, want %v", tt.args, v, tt.want)
}
}
}

func TestNilLogger(t *testing.T) {
for _, l := range []*Logger{nil, {}} {
// Test that none of this panics.
l.Printf(slog.LevelDebug)("nothing")
l.Debugf("nothing")
l.Infof("nothing")
l.Warnf("nothing")
l.Errorf("nothing")
l.Logf(slog.LevelDebug, "nothing")
}
}

func TestLog(t *testing.T) {
var s strings.Builder
l := New(Level(slog.LevelDebug), func(format string, args ...any) {
fmt.Fprintf(&s, format+"\n", args...)
})

l.Printf(slog.LevelDebug)("nothing")
l.Debugf("nothing")
l.Infof("nothing")
l.Warnf("nothing")
l.Errorf("nothing")
l.Logf(slog.LevelDebug, "nothing")

want := `DEBUG nothing
DEBUG nothing
INFO nothing
WARN nothing
ERROR nothing
DEBUG nothing
`
if got := s.String(); got != want {
t.Errorf("got %v, want %v", got, want)
}
}

func TestDefaults(t *testing.T) {
var s strings.Builder
log.SetOutput(&s)
log.SetFlags(0)

l := Debug()
l.Debugf("foobar")
want := "DEBUG foobar\n"
if got := s.String(); got != want {
t.Errorf("got %v, want %v", got, want)
}

l = Test(t)
l.Debugf("more foobar")
}

0 comments on commit 436f10e

Please sign in to comment.