diff options
author | Rob Pike <r@golang.org> | 2009-01-26 09:28:07 -0800 |
---|---|---|
committer | Rob Pike <r@golang.org> | 2009-01-26 09:28:07 -0800 |
commit | 1abefacb5fbddbab7077b10d9e3c5cceb51ae858 (patch) | |
tree | f61ed63b3ab8aabca4cc0fb31c00a4fc5df1f2f8 /src/lib/log.go | |
parent | dc2889e3a3bc71b21b217139be6915ed67f6172a (diff) | |
download | golang-1abefacb5fbddbab7077b10d9e3c5cceb51ae858.tar.gz |
add start to a logging package.
R=rsc
DELTA=205 (205 added, 0 deleted, 0 changed)
OCL=23448
CL=23478
Diffstat (limited to 'src/lib/log.go')
-rw-r--r-- | src/lib/log.go | 135 |
1 files changed, 135 insertions, 0 deletions
diff --git a/src/lib/log.go b/src/lib/log.go new file mode 100644 index 000000000..a54ba9f98 --- /dev/null +++ b/src/lib/log.go @@ -0,0 +1,135 @@ +// 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], +// Stderr[f], Exit[f], and Crash[f]. +// Exit exits when written to. +// Crash causes a crash when written to. + +package log + +import ( + "fmt"; + "io"; + "os"; + "time"; +) + +// Lshortname can be or'd in to cause only the last element of the file name to be printed. +const ( + Lok = iota; + Lexit; // terminate execution when written + Lcrash; // crash (panic) when written + Lshortname = 1 << 5; +) + +type Logger struct { + out0 io.Write; + out1 io.Write; + flag int; +} + +func NewLogger(out0, out1 io.Write, flag int) *Logger { + return &Logger{out0, out1, flag} +} + +var ( + stdout = NewLogger(os.Stdout, nil, Lok); + stderr = NewLogger(os.Stderr, nil, Lok); + exit = NewLogger(os.Stderr, nil, Lexit); + crash = NewLogger(os.Stderr, nil, Lcrash); +) + +func timestamp(ns int64) string { + t := time.SecondsToLocalTime(ns/1e9); + // why are time fields private? + s := t.RFC1123(); + return s[5:12] + s[17:25]; // TODO(r): placeholder. this gives "24 Jan 15:50:18" +} + +var shortnames = make(map[string] string) // cache of short names to avoid allocation. + +// The calldepth is provided for generality, although at the moment on all paths it will be 2. +func (l *Logger) output(calldepth int, s string) { + now := time.Nanoseconds(); // get this early. + newline := "\n"; + if len(s) > 0 && s[len(s)-1] == '\n' { + newline = "" + } + pc, file, line, ok := sys.Caller(calldepth); + if ok { + if l.flag & Lshortname == Lshortname { + short, ok := shortnames[file]; + if !ok { + short = file; + for i := len(file) - 1; i > 0; i-- { + if file[i] == '/' { + short = file[i+1:len(file)]; + shortnames[file] = short; + break; + } + } + } + file = short; + } + } else { + file = "???"; + line = 0; + } + s = fmt.Sprintf("%s %s:%d: %s%s", timestamp(now), file, line, s, newline); + io.WriteString(l.out0, s); + if l.out1 != nil { + io.WriteString(l.out1, s); + } + switch l.flag & ^Lshortname { + case Lcrash: + panic("log: fatal error"); + case Lexit: + sys.Exit(1); + } +} + +// Basic methods on Logger, analogous to Printf and Print +func (l *Logger) Logf(format string, v ...) { + l.output(2, fmt.Sprintf(format, v)) +} + +func (l *Logger) Log(v ...) { + l.output(2, fmt.Sprintln(v)) +} + +// Helper functions for lightweight simple logging to predefined Loggers. +func Stdout(v ...) { + stdout.output(2, fmt.Sprint(v)) +} + +func Stderr(v ...) { + stdout.output(2, fmt.Sprintln(v)) +} + +func Stdoutf(format string, v ...) { + stdout.output(2, fmt.Sprintf(format, v)) +} + +func Stderrf(format string, v ...) { + stderr.output(2, fmt.Sprintf(format, v)) +} + +func Exit(v ...) { + exit.output(2, fmt.Sprintln(v)) +} + +func Exitf(format string, v ...) { + exit.output(2, fmt.Sprintf(format, v)) +} + +func Crash(v ...) { + crash.output(2, fmt.Sprintln(v)) +} + +func Crashf(format string, v ...) { + crash.output(2, fmt.Sprintf(format, v)) +} |