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.go27
1 files changed, 20 insertions, 7 deletions
diff --git a/libgo/go/log/log.go b/libgo/go/log/log.go
index 12a9e7b8ce2..216cfe03222 100644
--- a/libgo/go/log/log.go
+++ b/libgo/go/log/log.go
@@ -25,8 +25,9 @@ import (
// These flags define which text to prefix to each log entry generated by the Logger.
// Bits are 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).
+// With the exception of the Lmsgprefix flag, there is no
+// control over the order they appear (the order listed here)
+// or the format they present (as described in the comments).
// The prefix is followed by a colon only when Llongfile or Lshortfile
// is specified.
// For example, flags Ldate | Ltime (or LstdFlags) produce,
@@ -40,6 +41,7 @@ const (
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
LUTC // if Ldate or Ltime is set, use UTC rather than the local time zone
+ Lmsgprefix // move the "prefix" from the beginning of the line to before the message
LstdFlags = Ldate | Ltime // initial values for the standard logger
)
@@ -49,7 +51,7 @@ const (
// multiple goroutines; it guarantees to serialize access to the Writer.
type Logger struct {
mu sync.Mutex // ensures atomic writes; protects the following fields
- prefix string // prefix to write at beginning of each line
+ prefix string // prefix on each line to identify the logger (but see Lmsgprefix)
flag int // properties
out io.Writer // destination for output
buf []byte // for accumulating text to write
@@ -57,7 +59,8 @@ type Logger struct {
// New creates a new Logger. The out variable sets the
// destination to which log data will be written.
-// The prefix appears at the beginning of each generated log line.
+// The prefix appears at the beginning of each generated log line, or
+// after the log header if the Lmsgprefix flag is provided.
// The flag argument defines the logging properties.
func New(out io.Writer, prefix string, flag int) *Logger {
return &Logger{out: out, prefix: prefix, flag: flag}
@@ -90,11 +93,14 @@ func itoa(buf *[]byte, i int, wid int) {
}
// formatHeader writes log header to buf in following order:
-// * l.prefix (if it's not blank),
+// * l.prefix (if it's not blank and Lmsgprefix is unset),
// * date and/or time (if corresponding flags are provided),
-// * file and line number (if corresponding flags are provided).
+// * file and line number (if corresponding flags are provided),
+// * l.prefix (if it's not blank and Lmsgprefix is set).
func (l *Logger) formatHeader(buf *[]byte, t time.Time, file string, line int) {
- *buf = append(*buf, l.prefix...)
+ if l.flag&Lmsgprefix == 0 {
+ *buf = append(*buf, l.prefix...)
+ }
if l.flag&(Ldate|Ltime|Lmicroseconds) != 0 {
if l.flag&LUTC != 0 {
t = t.UTC()
@@ -138,6 +144,9 @@ func (l *Logger) formatHeader(buf *[]byte, t time.Time, file string, line int) {
itoa(buf, line, -1)
*buf = append(*buf, ": "...)
}
+ if l.flag&Lmsgprefix != 0 {
+ *buf = append(*buf, l.prefix...)
+ }
}
// Output writes the output for a logging event. The string s contains
@@ -227,6 +236,7 @@ func (l *Logger) Panicln(v ...interface{}) {
}
// Flags returns the output flags for the logger.
+// The flag bits are Ldate, Ltime, and so on.
func (l *Logger) Flags() int {
l.mu.Lock()
defer l.mu.Unlock()
@@ -234,6 +244,7 @@ func (l *Logger) Flags() int {
}
// SetFlags sets the output flags for the logger.
+// The flag bits are Ldate, Ltime, and so on.
func (l *Logger) SetFlags(flag int) {
l.mu.Lock()
defer l.mu.Unlock()
@@ -269,11 +280,13 @@ func SetOutput(w io.Writer) {
}
// Flags returns the output flags for the standard logger.
+// The flag bits are Ldate, Ltime, and so on.
func Flags() int {
return std.Flags()
}
// SetFlags sets the output flags for the standard logger.
+// The flag bits are Ldate, Ltime, and so on.
func SetFlags(flag int) {
std.SetFlags(flag)
}