blob: 88233713e5d1b2d646d289d33065d56072ffb39e [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 (
"fmt"
"strings"
)
// Log emits a log record with the current context and supplied message to the active log handler.
func (l Logger) Log(msg string) {
if l.Active() {
GetHandler(l.Context)(l.Context.Record(msg))
}
}
// Logf emits a log record with the current context and supplied message to the active log handler.
func (l Logger) Logf(format string, args ...interface{}) {
if l.Active() {
GetHandler(l.Context)(l.Context.Record(logf{format, args}))
}
}
// Print is shorthand for ctx.At(InfoLevel).Log(msg)
// Useful for hidden by default simple progress messages
func (ctx logContext) Print(msg string) {
ctx.At(InfoLevel).Log(msg)
}
// Print is shorthand for ctx.At(InfoLevel).Logf(format, args...)
// This is useful for Printf debugging, should generally not be left in the code.
func (ctx logContext) Printf(format string, args ...interface{}) {
ctx.At(InfoLevel).Logf(format, args...)
}
// Fatal is shorthand for ctx.At(EmergencyLevel).Log(msg)
// Useful in applications where emergency level logging also causes a panic.
func (ctx logContext) Fatal(msg string) {
ctx.At(EmergencyLevel).Log(msg)
}
// Fatalf is shorthand for ctx.At(EmergencyLevel).Logf(format, args...)
// Useful in applications where emergency level logging also causes a panic.
func (ctx logContext) Fatalf(format string, args ...interface{}) {
ctx.At(EmergencyLevel).Logf(format, args...)
}
// Fatal is shorthand for ctx.At(EmergencyLevel).Cause(err).Log(msg)
// Useful in applications where emergency level logging also causes a panic.
func (ctx logContext) FatalError(err error, msg string) {
ctx.At(EmergencyLevel).Cause(err).Log(msg)
}
// Parse parses a logging message back into a Record
func Parse(ctx Context, s string) Record {
// TODO: handle more than just the simple brief format
i := strings.IndexRune(s, ':')
if i >= 0 {
severity := Severity(Severity_value[s[0:i]])
s = s[i+1:]
ctx = ctx.Severity(severity)
}
return ctx.Record(s)
}
// logf is the private type for delayed printf formatting.
type logf struct {
format string
args []interface{}
}
func (l logf) Format(f fmt.State, c rune) {
fmt.Fprintf(f, l.format, l.args...)
}