blob: 43c1bb97706f9a9dbfc1f0a8812261893dc8fcb2 [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
import (
"bytes"
"fmt"
)
// CauseKey is the context key used to store an error associated with the log
// line in the log context
const CauseKey = "cause"
// GetCause gets the cause stored in the given context.
func GetCause(ctx Context) error {
err, _ := ctx.Value(CauseKey).(error)
return err
}
// Cause returns a context with the cause set.
func (ctx logContext) Cause(err error) Context {
return ctx.setValue(CauseKey, err)
}
// Cause returns a logger with the cause set if the logger is active.
func (l Logger) Cause(err error) Logger {
if l.Active() {
l.updateUnkeyed(CauseKey, err)
}
return l
}
// Fail is shorthand for ctx.At(ErrorLevel).Cause(err).Log(msg)
func (ctx logContext) Fail(err error, msg string) {
ctx.At(ErrorLevel).Cause(err).Log(msg)
}
// Fail is shorthand for l.Cause(err).Log(msg)
func (l Logger) Fail(err error, msg string) {
l.Cause(err).Log(msg)
}
func (ctx logContext) errorRecord(msg interface{}) Record {
r := ctx.Record(msg)
if r.Severity == Severity(-1) {
r.Severity = ErrorLevel
}
return r
}
// AsError returns an error with the given message.
// This is allows us to use the power of the logging context and formatting
// when generating error objects, intended to replace current usage of
// fmt.Errorf.
func (ctx logContext) AsError(msg interface{}) error {
return errorRecord(ctx.errorRecord(msg))
}
// AsErrorf returns an error with the given message and arguments.
// This is allows us to use the power of the logging context and formatting
// when generating error objects, intended to replace current usage of
// fmt.Errorf.
func (ctx logContext) AsErrorf(format string, args ...interface{}) error {
return errorRecord(ctx.errorRecord(fmt.Errorf(format, args)))
}
// WrapError is shorthand for ctx.Cause(err).AsError(msg)
func (ctx logContext) WrapError(err error, msg interface{}) error {
return ctx.Cause(err).AsError(msg)
}
// LogError emits a log record with the current context and supplied message to the active log handler, and also returns
// the record as an error. It will return the record even if the logger is not active.
func (ctx logContext) LogError(msg interface{}) error {
r := ctx.errorRecord(msg)
l := ctx.At(r.Severity)
if l.Active() {
GetHandler(l.Context)(r)
}
return errorRecord(r)
}
type errorRecord Record
// Error makes errorRecord implement error
func (err errorRecord) Error() string {
buf := bytes.Buffer{}
Brief(&buf, Record(err))
return buf.String()
}