summaryrefslogtreecommitdiff
path: root/libgo/go/log/log.go
diff options
context:
space:
mode:
Diffstat (limited to 'libgo/go/log/log.go')
-rw-r--r--libgo/go/log/log.go56
1 files changed, 51 insertions, 5 deletions
diff --git a/libgo/go/log/log.go b/libgo/go/log/log.go
index ac24b4deaa7..d34af9e5e45 100644
--- a/libgo/go/log/log.go
+++ b/libgo/go/log/log.go
@@ -19,6 +19,7 @@ import (
"runtime"
"os"
"time"
+ "sync"
)
// These flags define which text to prefix to each log entry generated by the Logger.
@@ -34,11 +35,15 @@ const (
Lshortfile // final file name element and line number: d.go:23. overrides Llongfile
)
-// Logger represents an active logging object.
+// A Logger represents an active logging object that generates lines of
+// output to an io.Writer. Each logging operation makes a single call to
+// the Writer's Write method. A Logger can be used simultaneously from
+// multiple goroutines; it guarantees to serialize access to the Writer.
type Logger struct {
- out io.Writer // destination for output
- prefix string // prefix to write at beginning of each line
- flag int // properties
+ mu sync.Mutex // ensures atomic writes
+ out io.Writer // destination for output
+ prefix string // prefix to write at beginning of each line
+ flag int // properties
}
// New creates a new Logger. The out variable sets the
@@ -46,7 +51,7 @@ type Logger struct {
// The prefix appears at the beginning of each generated log line.
// The flag argument defines the logging properties.
func New(out io.Writer, prefix string, flag int) *Logger {
- return &Logger{out, prefix, flag}
+ return &Logger{out: out, prefix: prefix, flag: flag}
}
var std = New(os.Stderr, "", Ldate|Ltime)
@@ -139,6 +144,8 @@ func (l *Logger) Output(calldepth int, s string) os.Error {
if len(s) > 0 && s[len(s)-1] != '\n' {
buf.WriteByte('\n')
}
+ l.mu.Lock()
+ defer l.mu.Unlock()
_, err := l.out.Write(buf.Bytes())
return err
}
@@ -157,6 +164,45 @@ func (l *Logger) Print(v ...interface{}) { l.Output(2, fmt.Sprint(v...)) }
// Arguments are handled in the manner of fmt.Println.
func (l *Logger) Println(v ...interface{}) { l.Output(2, fmt.Sprintln(v...)) }
+// Exit is equivalent to l.Print() followed by a call to os.Exit(1).
+func (l *Logger) Exit(v ...interface{}) {
+ l.Output(2, fmt.Sprint(v...))
+ os.Exit(1)
+}
+
+// Exitf is equivalent to l.Printf() followed by a call to os.Exit(1).
+func (l *Logger) Exitf(format string, v ...interface{}) {
+ l.Output(2, fmt.Sprintf(format, v...))
+ os.Exit(1)
+}
+
+// Exitln is equivalent to l.Println() followed by a call to os.Exit(1).
+func (l *Logger) Exitln(v ...interface{}) {
+ l.Output(2, fmt.Sprintln(v...))
+ os.Exit(1)
+}
+
+// Panic is equivalent to l.Print() followed by a call to panic().
+func (l *Logger) Panic(v ...interface{}) {
+ s := fmt.Sprint(v...)
+ l.Output(2, s)
+ panic(s)
+}
+
+// Panicf is equivalent to l.Printf() followed by a call to panic().
+func (l *Logger) Panicf(format string, v ...interface{}) {
+ s := fmt.Sprintf(format, v...)
+ l.Output(2, s)
+ panic(s)
+}
+
+// Panicln is equivalent to l.Println() followed by a call to panic().
+func (l *Logger) Panicln(v ...interface{}) {
+ s := fmt.Sprintln(v...)
+ l.Output(2, s)
+ panic(s)
+}
+
// SetOutput sets the output destination for the standard logger.
func SetOutput(w io.Writer) {
std.out = w