diff options
Diffstat (limited to 'libgo/go/log/log.go')
-rw-r--r-- | libgo/go/log/log.go | 57 |
1 files changed, 25 insertions, 32 deletions
diff --git a/libgo/go/log/log.go b/libgo/go/log/log.go index 02a407ebc60..1d7f209d1da 100644 --- a/libgo/go/log/log.go +++ b/libgo/go/log/log.go @@ -13,8 +13,6 @@ package log import ( - "bytes" - _ "debug/elf" "fmt" "io" "os" @@ -29,7 +27,7 @@ const ( // 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 - Ldate = 1 << iota // the date: 2009/0123 + Ldate = 1 << iota // the date: 2009/01/23 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 @@ -42,11 +40,11 @@ const ( // 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 { - mu sync.Mutex // ensures atomic writes; protects the following fields - prefix string // prefix to write at beginning of each line - flag int // properties - out io.Writer // destination for output - buf bytes.Buffer // for accumulating text to write + mu sync.Mutex // ensures atomic writes; protects the following fields + prefix string // prefix to write at beginning of each line + flag int // properties + out io.Writer // destination for output + buf []byte // for accumulating text to write } // New creates a new Logger. The out variable sets the @@ -61,10 +59,10 @@ var std = New(os.Stderr, "", LstdFlags) // Cheap integer to fixed-width decimal ASCII. Give a negative width to avoid zero-padding. // Knows the buffer has capacity. -func itoa(buf *bytes.Buffer, i int, wid int) { +func itoa(buf *[]byte, i int, wid int) { var u uint = uint(i) if u == 0 && wid <= 1 { - buf.WriteByte('0') + *buf = append(*buf, '0') return } @@ -76,38 +74,33 @@ func itoa(buf *bytes.Buffer, i int, wid int) { wid-- b[bp] = byte(u%10) + '0' } - - // avoid slicing b to avoid an allocation. - for bp < len(b) { - buf.WriteByte(b[bp]) - bp++ - } + *buf = append(*buf, b[bp:]...) } -func (l *Logger) formatHeader(buf *bytes.Buffer, t time.Time, file string, line int) { - buf.WriteString(l.prefix) +func (l *Logger) formatHeader(buf *[]byte, t time.Time, file string, line int) { + *buf = append(*buf, l.prefix...) if l.flag&(Ldate|Ltime|Lmicroseconds) != 0 { if l.flag&Ldate != 0 { year, month, day := t.Date() itoa(buf, year, 4) - buf.WriteByte('/') + *buf = append(*buf, '/') itoa(buf, int(month), 2) - buf.WriteByte('/') + *buf = append(*buf, '/') itoa(buf, day, 2) - buf.WriteByte(' ') + *buf = append(*buf, ' ') } if l.flag&(Ltime|Lmicroseconds) != 0 { hour, min, sec := t.Clock() itoa(buf, hour, 2) - buf.WriteByte(':') + *buf = append(*buf, ':') itoa(buf, min, 2) - buf.WriteByte(':') + *buf = append(*buf, ':') itoa(buf, sec, 2) if l.flag&Lmicroseconds != 0 { - buf.WriteByte('.') + *buf = append(*buf, '.') itoa(buf, t.Nanosecond()/1e3, 6) } - buf.WriteByte(' ') + *buf = append(*buf, ' ') } } if l.flag&(Lshortfile|Llongfile) != 0 { @@ -121,10 +114,10 @@ func (l *Logger) formatHeader(buf *bytes.Buffer, t time.Time, file string, line } file = short } - buf.WriteString(file) - buf.WriteByte(':') + *buf = append(*buf, file...) + *buf = append(*buf, ':') itoa(buf, line, -1) - buf.WriteString(": ") + *buf = append(*buf, ": "...) } } @@ -151,13 +144,13 @@ func (l *Logger) Output(calldepth int, s string) error { } l.mu.Lock() } - l.buf.Reset() + l.buf = l.buf[:0] l.formatHeader(&l.buf, now, file, line) - l.buf.WriteString(s) + l.buf = append(l.buf, s...) if len(s) > 0 && s[len(s)-1] != '\n' { - l.buf.WriteByte('\n') + l.buf = append(l.buf, '\n') } - _, err := l.out.Write(l.buf.Bytes()) + _, err := l.out.Write(l.buf) return err } |