blob: a65defeb250828318ad5f15df61a8568bfdb4f29 [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"
"android.googlesource.com/platform/tools/gpu/framework/log"
)
var greetingError = errors.New("greeting")
func TestHelpers(t *testing.T) {
buf := &bytes.Buffer{}
ctx := log.Background().PreFilter(log.NoLimit).Filter(log.Pass).Handler(log.Writer(log.Normal, buf)).
Recorder(log.StacktraceOnError).StackFilter(LogTestStackFilter)
expect := func(s string) {
got := buf.String()
buf.Reset()
if s != got {
t.Errorf("Got %v expected %v", got, s)
}
}
ctx.Emergency().Log("Hello")
expect("Emergency:log_test.go:38:Hello\n")
ctx.Alert().Log("Hello")
expect("Alert:log_test.go:40:Hello\n")
ctx.Critical().Log("Hello")
expect("Critical:log_test.go:42:Hello\n")
ctx.Error().Log("Hello")
expect("Error:log_test.go:44:Hello\n")
ctx.Fail(greetingError, "Hello")
expect("Error:log_test.go:46:Failed:Hello<greeting>\n")
ctx.Cause(greetingError).Critical().Log("Goodbye")
expect("Critical:log_test.go:48:Failed:Goodbye<greeting>\n")
ctx.Warning().Log("Hello")
expect("Warning:Hello\n")
ctx.Notice().Log("Hello")
expect("Notice:Hello\n")
ctx.Print("Hello")
expect("Info:Hello\n")
ctx.Debug().Log("Hello")
expect("Debug:Hello\n")
ctx.Info().S("string", "one").I("int", 1).F("float", 1.0).Log("Hello")
expect("Info:Hello:string=one,int=1,float=1\n")
ctx.S("string", "one").I("int", 1).F("float", 1.0).Info().Log("Hello")
expect("Info:Hello:string=one,int=1,float=1\n")
v := errors.New("test")
ctx.Info().V("value", v).T("type", v).Log("Hello")
expect("Info:Hello:value=test,type=*errors.errorString\n")
ctx.V("value", v).T("type", v).Info().Log("Hello")
expect("Info:Hello:value=test,type=*errors.errorString\n")
ctx.Info().Tag("a_tag").Log("Hello")
expect("Info:a_tag:Hello\n")
ctx.Tag("b_tag").Info().Log("Hello")
expect("Info:b_tag:Hello\n")
}
func LogTestStackFilter(entry *log.StackEntry) bool {
return entry.Package != "android.googlesource.com/platform/tools/gpu/framework/log_test"
}