Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

memoize formated messages in mem backends #91

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions format_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func TestFormat(t *testing.T) {
log.Debug("hello")

line := MemoryRecordN(backend, 0).Formatted(0)
if "format_test.go:24 1970-01-01T00:00:00 D 0001 module hello" != line {
if "format_test.go:22 1970-01-01T00:00:00 D 0001 module hello" != line {
t.Errorf("Unexpected format: %s", line)
}
}
Expand All @@ -37,13 +37,15 @@ func getLastLine(backend *MemoryBackend) string {
}

func realFunc(backend *MemoryBackend) string {
return logAndGetLine(backend)
MustGetLogger("foo").Debug("hello")
return getLastLine(backend)
}

type structFunc struct{}

func (structFunc) Log(backend *MemoryBackend) string {
return logAndGetLine(backend)
MustGetLogger("foo").Debug("hello")
return getLastLine(backend)
}

func TestRealFuncFormat(t *testing.T) {
Expand Down
6 changes: 6 additions & 0 deletions memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ func NewMemoryBackend(size int) *MemoryBackend {
func (b *MemoryBackend) Log(level Level, calldepth int, rec *Record) error {
var size int32

// memoize the formated message so that stack information is preserved
rec.Formatted(calldepth + 1)

n := &node{Record: rec}
np := unsafe.Pointer(n)

Expand Down Expand Up @@ -223,6 +226,9 @@ func (b *ChannelMemoryBackend) Stop() {

// Log implements the Log method required by Backend.
func (b *ChannelMemoryBackend) Log(level Level, calldepth int, rec *Record) error {
// memoize the formated message so that stack information is preserved
rec.Formatted(calldepth + 1)

b.incoming <- rec
return nil
}
Expand Down
6 changes: 6 additions & 0 deletions memory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ func TestMemoryBackend(t *testing.T) {
t.Errorf("record length: %d", backend.size)
}
record := MemoryRecordN(backend, 0)
if record.formatted == "" {
t.Error("record was not preformated")
}
if "5" != record.Formatted(0) {
t.Errorf("unexpected start: %s", record.Formatted(0))
}
Expand Down Expand Up @@ -97,6 +100,9 @@ func TestChannelMemoryBackend(t *testing.T) {
t.Errorf("record length: %d", backend.size)
}
record := ChannelMemoryRecordN(backend, 0)
if record.formatted == "" {
t.Error("record was not preformated")
}
if "5" != record.Formatted(0) {
t.Errorf("unexpected start: %s", record.Formatted(0))
}
Expand Down