2009-01-26 10:28:07 -07:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
// Rudimentary logging package. Defines a type, Logger, with simple
|
|
|
|
// methods for formatting output to one or two destinations. Also has
|
|
|
|
// predefined Loggers accessible through helper functions Stdout[f],
|
2009-03-04 23:46:44 -07:00
|
|
|
// Stderr[f], Exit[f], and Crash[f], which are easier to use than creating
|
|
|
|
// a Logger manually.
|
2009-01-26 10:28:07 -07:00
|
|
|
// Exit exits when written to.
|
|
|
|
// Crash causes a crash when written to.
|
|
|
|
package log
|
|
|
|
|
|
|
|
import (
|
2009-12-15 16:35:38 -07:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"runtime"
|
|
|
|
"os"
|
|
|
|
"time"
|
2009-01-26 10:28:07 -07:00
|
|
|
)
|
|
|
|
|
2009-03-04 23:46:44 -07:00
|
|
|
// These flags define the properties of the Logger and the output they produce.
|
2009-01-26 10:28:07 -07:00
|
|
|
const (
|
2009-01-26 16:34:16 -07:00
|
|
|
// Flags
|
2009-12-15 16:35:38 -07:00
|
|
|
Lok = iota
|
|
|
|
Lexit // terminate execution when written
|
|
|
|
Lcrash // crash (panic) when written
|
2009-01-26 16:34:16 -07:00
|
|
|
// Bits or'ed together to control what's printed. There is no control over the
|
|
|
|
// order they appear (the order listed here) or the format they present (as
|
|
|
|
// described in the comments). A colon appears after these items:
|
|
|
|
// 2009/0123 01:23:23.123123 /a/b/c/d.go:23: message
|
2009-12-15 16:35:38 -07:00
|
|
|
Ldate = 1 << iota // the date: 2009/0123
|
2010-03-16 17:45:54 -06:00
|
|
|
Ltime // the time: 01:23:23
|
|
|
|
Lmicroseconds // microsecond resolution: 01:23:23.123123. assumes Ltime.
|
|
|
|
Llongfile // full file name and line number: /a/b/c/d.go:23
|
|
|
|
Lshortfile // final file name element and line number: d.go:23. overrides Llongfile
|
2009-12-15 16:35:38 -07:00
|
|
|
lAllBits = Ldate | Ltime | Lmicroseconds | Llongfile | Lshortfile
|
2009-01-26 10:28:07 -07:00
|
|
|
)
|
|
|
|
|
2009-03-04 23:46:44 -07:00
|
|
|
// Logger represents an active logging object.
|
2009-01-26 10:28:07 -07:00
|
|
|
type Logger struct {
|
2009-12-15 16:35:38 -07:00
|
|
|
out0 io.Writer // first destination for output
|
|
|
|
out1 io.Writer // second destination for output; may be nil
|
|
|
|
prefix string // prefix to write at beginning of each line
|
|
|
|
flag int // properties
|
2009-01-26 10:28:07 -07:00
|
|
|
}
|
|
|
|
|
2009-05-15 18:22:30 -06:00
|
|
|
// New creates a new Logger. The out0 and out1 variables set the
|
2009-03-04 23:46:44 -07:00
|
|
|
// destinations to which log data will be written; out1 may be nil.
|
|
|
|
// The prefix appears at the beginning of each generated log line.
|
|
|
|
// The flag argument defines the logging properties.
|
2009-05-15 18:22:30 -06:00
|
|
|
func New(out0, out1 io.Writer, prefix string, flag int) *Logger {
|
2009-11-09 13:07:39 -07:00
|
|
|
return &Logger{out0, out1, prefix, flag}
|
2009-01-26 10:28:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
2009-12-15 16:35:38 -07:00
|
|
|
stdout = New(os.Stdout, nil, "", Lok|Ldate|Ltime)
|
|
|
|
stderr = New(os.Stderr, nil, "", Lok|Ldate|Ltime)
|
|
|
|
exit = New(os.Stderr, nil, "", Lexit|Ldate|Ltime)
|
|
|
|
crash = New(os.Stderr, nil, "", Lcrash|Ldate|Ltime)
|
2009-01-26 10:28:07 -07:00
|
|
|
)
|
|
|
|
|
2009-12-15 16:35:38 -07:00
|
|
|
var shortnames = make(map[string]string) // cache of short names to avoid allocation.
|
2009-01-26 16:34:16 -07:00
|
|
|
|
|
|
|
// Cheap integer to fixed-width decimal ASCII. Use a negative width to avoid zero-padding
|
|
|
|
func itoa(i int, wid int) string {
|
2009-12-15 16:35:38 -07:00
|
|
|
var u uint = uint(i)
|
2009-01-26 16:34:16 -07:00
|
|
|
if u == 0 && wid <= 1 {
|
2009-11-09 13:07:39 -07:00
|
|
|
return "0"
|
2009-01-26 16:34:16 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Assemble decimal in reverse order.
|
2009-12-15 16:35:38 -07:00
|
|
|
var b [32]byte
|
|
|
|
bp := len(b)
|
2009-01-26 16:34:16 -07:00
|
|
|
for ; u > 0 || wid > 0; u /= 10 {
|
2009-12-15 16:35:38 -07:00
|
|
|
bp--
|
|
|
|
wid--
|
|
|
|
b[bp] = byte(u%10) + '0'
|
2009-01-26 16:34:16 -07:00
|
|
|
}
|
|
|
|
|
2009-12-15 16:35:38 -07:00
|
|
|
return string(b[bp:])
|
2009-01-26 10:28:07 -07:00
|
|
|
}
|
|
|
|
|
2009-01-26 16:34:16 -07:00
|
|
|
func (l *Logger) formatHeader(ns int64, calldepth int) string {
|
2009-12-15 16:35:38 -07:00
|
|
|
h := l.prefix
|
2009-11-09 22:23:52 -07:00
|
|
|
if l.flag&(Ldate|Ltime|Lmicroseconds) != 0 {
|
2009-12-15 16:35:38 -07:00
|
|
|
t := time.SecondsToLocalTime(ns / 1e9)
|
2009-11-09 22:23:52 -07:00
|
|
|
if l.flag&(Ldate) != 0 {
|
2010-09-11 22:06:45 -06:00
|
|
|
h += itoa(int(t.Year), 4) + "/" + itoa(t.Month, 2) + "/" + itoa(t.Day, 2) + " "
|
2009-01-26 16:34:16 -07:00
|
|
|
}
|
2009-11-09 22:23:52 -07:00
|
|
|
if l.flag&(Ltime|Lmicroseconds) != 0 {
|
2009-12-15 16:35:38 -07:00
|
|
|
h += itoa(t.Hour, 2) + ":" + itoa(t.Minute, 2) + ":" + itoa(t.Second, 2)
|
2009-11-09 22:23:52 -07:00
|
|
|
if l.flag&Lmicroseconds != 0 {
|
2009-11-09 13:07:39 -07:00
|
|
|
h += "." + itoa(int(ns%1e9)/1e3, 6)
|
2009-01-26 16:34:16 -07:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
h += " "
|
2009-01-26 16:34:16 -07:00
|
|
|
}
|
|
|
|
}
|
2009-11-09 22:23:52 -07:00
|
|
|
if l.flag&(Lshortfile|Llongfile) != 0 {
|
2009-12-15 16:35:38 -07:00
|
|
|
_, file, line, ok := runtime.Caller(calldepth)
|
2009-01-26 16:34:16 -07:00
|
|
|
if ok {
|
2009-11-09 22:23:52 -07:00
|
|
|
if l.flag&Lshortfile != 0 {
|
2009-12-15 16:35:38 -07:00
|
|
|
short, ok := shortnames[file]
|
2009-01-26 16:34:16 -07:00
|
|
|
if !ok {
|
2009-12-15 16:35:38 -07:00
|
|
|
short = file
|
2009-11-09 22:23:52 -07:00
|
|
|
for i := len(file) - 1; i > 0; i-- {
|
2009-01-26 16:34:16 -07:00
|
|
|
if file[i] == '/' {
|
2009-12-15 16:35:38 -07:00
|
|
|
short = file[i+1:]
|
|
|
|
break
|
2009-01-26 16:34:16 -07:00
|
|
|
}
|
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
shortnames[file] = short
|
2009-01-26 16:34:16 -07:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
file = short
|
2009-01-26 16:34:16 -07:00
|
|
|
}
|
|
|
|
} else {
|
2009-12-15 16:35:38 -07:00
|
|
|
file = "???"
|
|
|
|
line = 0
|
2009-01-26 16:34:16 -07:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
h += file + ":" + itoa(line, -1) + ": "
|
2009-01-26 16:34:16 -07:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
return h
|
2009-01-26 16:34:16 -07:00
|
|
|
}
|
2009-01-26 10:28:07 -07:00
|
|
|
|
2009-03-04 23:46:44 -07:00
|
|
|
// Output writes the output for a logging event. The string s contains the text to print after
|
|
|
|
// the time stamp; calldepth is used to recover the PC. It is provided for generality, although
|
|
|
|
// at the moment on all pre-defined paths it will be 2.
|
2009-12-11 13:41:51 -07:00
|
|
|
func (l *Logger) Output(calldepth int, s string) os.Error {
|
2009-12-15 16:35:38 -07:00
|
|
|
now := time.Nanoseconds() // get this early.
|
|
|
|
newline := "\n"
|
2009-01-26 10:28:07 -07:00
|
|
|
if len(s) > 0 && s[len(s)-1] == '\n' {
|
2009-11-09 13:07:39 -07:00
|
|
|
newline = ""
|
2009-01-26 10:28:07 -07:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
s = l.formatHeader(now, calldepth+1) + s + newline
|
|
|
|
_, err := io.WriteString(l.out0, s)
|
2009-01-26 10:28:07 -07:00
|
|
|
if l.out1 != nil {
|
2009-12-15 16:35:38 -07:00
|
|
|
_, err1 := io.WriteString(l.out1, s)
|
2009-12-11 13:41:51 -07:00
|
|
|
if err == nil && err1 != nil {
|
|
|
|
err = err1
|
|
|
|
}
|
2009-01-26 10:28:07 -07:00
|
|
|
}
|
2009-01-26 16:34:16 -07:00
|
|
|
switch l.flag & ^lAllBits {
|
2009-01-26 10:28:07 -07:00
|
|
|
case Lcrash:
|
2009-11-09 13:07:39 -07:00
|
|
|
panic("log: fatal error")
|
2009-01-26 10:28:07 -07:00
|
|
|
case Lexit:
|
2009-11-09 13:07:39 -07:00
|
|
|
os.Exit(1)
|
2009-01-26 10:28:07 -07:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
return err
|
2009-01-26 10:28:07 -07:00
|
|
|
}
|
|
|
|
|
2009-03-04 23:46:44 -07:00
|
|
|
// Logf is analogous to Printf() for a Logger.
|
2010-02-01 16:53:37 -07:00
|
|
|
func (l *Logger) Logf(format string, v ...interface{}) {
|
2010-09-24 09:55:48 -06:00
|
|
|
l.Output(2, fmt.Sprintf(format, v...))
|
2010-02-01 16:53:37 -07:00
|
|
|
}
|
2009-01-26 10:28:07 -07:00
|
|
|
|
2009-11-06 19:43:57 -07:00
|
|
|
// Log is analogous to Print() for a Logger.
|
2010-09-24 09:55:48 -06:00
|
|
|
func (l *Logger) Log(v ...interface{}) { l.Output(2, fmt.Sprintln(v...)) }
|
2009-01-26 10:28:07 -07:00
|
|
|
|
2009-03-04 23:46:44 -07:00
|
|
|
// Stdout is a helper function for easy logging to stdout. It is analogous to Print().
|
2010-09-24 09:55:48 -06:00
|
|
|
func Stdout(v ...interface{}) { stdout.Output(2, fmt.Sprint(v...)) }
|
2009-01-26 10:28:07 -07:00
|
|
|
|
2009-03-04 23:46:44 -07:00
|
|
|
// Stderr is a helper function for easy logging to stderr. It is analogous to Fprint(os.Stderr).
|
2010-09-24 09:55:48 -06:00
|
|
|
func Stderr(v ...interface{}) { stderr.Output(2, fmt.Sprintln(v...)) }
|
2009-01-26 10:28:07 -07:00
|
|
|
|
2009-03-04 23:46:44 -07:00
|
|
|
// Stdoutf is a helper functions for easy formatted logging to stdout. It is analogous to Printf().
|
2010-09-24 09:55:48 -06:00
|
|
|
func Stdoutf(format string, v ...interface{}) { stdout.Output(2, fmt.Sprintf(format, v...)) }
|
2009-01-26 10:28:07 -07:00
|
|
|
|
2009-03-04 23:46:44 -07:00
|
|
|
// Stderrf is a helper function for easy formatted logging to stderr. It is analogous to Fprintf(os.Stderr).
|
2010-09-24 09:55:48 -06:00
|
|
|
func Stderrf(format string, v ...interface{}) { stderr.Output(2, fmt.Sprintf(format, v...)) }
|
2009-01-26 10:28:07 -07:00
|
|
|
|
2009-05-08 16:21:41 -06:00
|
|
|
// Exit is equivalent to Stderr() followed by a call to os.Exit(1).
|
2010-09-24 09:55:48 -06:00
|
|
|
func Exit(v ...interface{}) { exit.Output(2, fmt.Sprintln(v...)) }
|
2009-01-26 10:28:07 -07:00
|
|
|
|
2009-05-08 16:21:41 -06:00
|
|
|
// Exitf is equivalent to Stderrf() followed by a call to os.Exit(1).
|
2010-09-24 09:55:48 -06:00
|
|
|
func Exitf(format string, v ...interface{}) { exit.Output(2, fmt.Sprintf(format, v...)) }
|
2009-01-26 10:28:07 -07:00
|
|
|
|
2009-08-13 16:14:41 -06:00
|
|
|
// Crash is equivalent to Stderr() followed by a call to panic().
|
2010-09-24 09:55:48 -06:00
|
|
|
func Crash(v ...interface{}) { crash.Output(2, fmt.Sprintln(v...)) }
|
2009-01-26 10:28:07 -07:00
|
|
|
|
2009-03-04 23:46:44 -07:00
|
|
|
// Crashf is equivalent to Stderrf() followed by a call to panic().
|
2010-09-24 09:55:48 -06:00
|
|
|
func Crashf(format string, v ...interface{}) { crash.Output(2, fmt.Sprintf(format, v...)) }
|