blob: 02165117ad2c12bacd7160d31f65b9213d1146d6 [file] [log] [blame]
// Copyright (C) 2016 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package log_test
import (
"bytes"
"errors"
"testing"
"time"
"strings"
"android.googlesource.com/platform/tools/gpu/framework/log"
)
var testError = errors.New("an error")
var testTime time.Time
func init() {
testTime, _ = time.Parse(time.Stamp, "Dec 4 14:19:11.691")
}
func TestStyles(t *testing.T) {
buf := &bytes.Buffer{}
ctx := log.Background().PreFilter(log.NoLimit).Filter(log.Pass).StackFilter(StacksForTest())
ctx = ctx.Recorder(func(ctx log.Context, r *log.Record) { r.TimeStamp = testTime })
chainErr := ctx.V("on", "something").AsError("chained error")
ctx = ctx.Enter("a").Enter("b").Enter("c").Tag("styles").S("astring", "test")
for _, test := range []struct {
style log.Style
stacktrace log.StackTrace
expect string
}{
{log.Brief, stacktrace_3, `
Info:A message
Error:Failed:A failure<an error>
Raw output
output:Channel output
Error:Failed:A chained failure<<Error:chained error>>
`},
{log.Normal, stacktrace_3, `
Info:stack_test.go:31:styles:a->b->c:A message:astring=test
Error:stack_test.go:31:styles:a->b->c:Failed:A failure:astring=test<an error>
Raw output
output:Channel output
Error:stack_test.go:31:styles:a->b->c:A chained failure:astring=test<<Error:chained error:on=something>>
`},
{log.Detailed, stacktrace_3, `
Dec 4 14:19:11:Info:styles:a->b->c:A message
astring=test
• stack_test.go:31 stack_func_3
• stack_test.go:37 StacksForTest
Dec 4 14:19:11:Error:styles:a->b->c:Failed:A failure
astring=test
<an error>
• stack_test.go:31 stack_func_3
• stack_test.go:37 StacksForTest
Raw output
output:Channel output
Dec 4 14:19:11:Error:styles:a->b->c:A chained failure
astring=test
• stack_test.go:31 stack_func_3
• stack_test.go:37 StacksForTest
<<
Dec 4 14:19:11:Error:chained error
on=something
>>
`},
{log.Structured, stacktrace_3, `
Info:A message
Time=Dec 4 14:19:11.691
Tag=styles
Trace=a->b->c
• stack_test.go:31 android.googlesource.com/platform/tools/gpu/framework/log_test stack_func_3
• stack_test.go:37 android.googlesource.com/platform/tools/gpu/framework/log_test StacksForTest
astring=test
Error:A failure
Time=Dec 4 14:19:11.691
Tag=styles
Trace=a->b->c
Cause=an error
• stack_test.go:31 android.googlesource.com/platform/tools/gpu/framework/log_test stack_func_3
• stack_test.go:37 android.googlesource.com/platform/tools/gpu/framework/log_test StacksForTest
astring=test
Raw output
output:Channel output
Error:A chained failure
Time=Dec 4 14:19:11.691
Tag=styles
Trace=a->b->c
• stack_test.go:31 android.googlesource.com/platform/tools/gpu/framework/log_test stack_func_3
• stack_test.go:37 android.googlesource.com/platform/tools/gpu/framework/log_test StacksForTest
astring=test
<<
Error:chained error
Time=Dec 4 14:19:11.691
on=something
>>
`},
} {
expect := strings.TrimSpace(test.expect)
ctx := ctx.Handler(log.Writer(test.style, buf)).Recorder(func(ctx log.Context, r *log.Record) {
r.StackTrace = test.stacktrace
r.TimeStamp = testTime
})
ctx.Print("A message")
ctx.Fail(testError, "A failure")
ctx.Raw("").Log("Raw output")
ctx.Raw("output").Log("Channel output")
ctx.Fail(chainErr, "A chained failure")
got := strings.TrimSpace(buf.String())
buf.Reset()
if expect != got {
t.Errorf("Got\n%s\nexpected\n%s", got, expect)
}
}
}