diff options
Diffstat (limited to 'libgo/go/log')
-rw-r--r-- | libgo/go/log/log.go | 20 | ||||
-rw-r--r-- | libgo/go/log/log_test.go | 10 | ||||
-rw-r--r-- | libgo/go/log/syslog/syslog.go | 15 |
3 files changed, 32 insertions, 13 deletions
diff --git a/libgo/go/log/log.go b/libgo/go/log/log.go index 58b8788..587904b 100644 --- a/libgo/go/log/log.go +++ b/libgo/go/log/log.go @@ -72,7 +72,7 @@ func (l *Logger) SetOutput(w io.Writer) { var std = New(os.Stderr, "", LstdFlags) -// Cheap integer to fixed-width decimal ASCII. Give a negative width to avoid zero-padding. +// Cheap integer to fixed-width decimal ASCII. Give a negative width to avoid zero-padding. func itoa(buf *[]byte, i int, wid int) { // Assemble decimal in reverse order. var b [20]byte @@ -89,12 +89,16 @@ func itoa(buf *[]byte, i int, wid int) { *buf = append(*buf, b[bp:]...) } +// formatHeader writes log header to buf in following order: +// * l.prefix (if it's not blank), +// * date and/or time (if corresponding flags are provided), +// * file and line number (if corresponding flags are provided). func (l *Logger) formatHeader(buf *[]byte, t time.Time, file string, line int) { *buf = append(*buf, l.prefix...) - if l.flag&LUTC != 0 { - t = t.UTC() - } if l.flag&(Ldate|Ltime|Lmicroseconds) != 0 { + if l.flag&LUTC != 0 { + t = t.UTC() + } if l.flag&Ldate != 0 { year, month, day := t.Date() itoa(buf, year, 4) @@ -143,13 +147,17 @@ func (l *Logger) formatHeader(buf *[]byte, t time.Time, file string, line int) { // provided for generality, although at the moment on all pre-defined // paths it will be 2. func (l *Logger) Output(calldepth int, s string) error { - now := time.Now() // get this early. + // Get time early if we need it. + var now time.Time + if l.flag&(Ldate|Ltime|Lmicroseconds) != 0 { + now = time.Now() + } var file string var line int l.mu.Lock() defer l.mu.Unlock() if l.flag&(Lshortfile|Llongfile) != 0 { - // release lock while getting caller info - it's expensive. + // Release lock while getting caller info - it's expensive. l.mu.Unlock() var ok bool _, file, line, ok = runtime.Caller(calldepth) diff --git a/libgo/go/log/log_test.go b/libgo/go/log/log_test.go index dd16c9d..966fdf3 100644 --- a/libgo/go/log/log_test.go +++ b/libgo/go/log/log_test.go @@ -182,3 +182,13 @@ func BenchmarkPrintln(b *testing.B) { l.Println(testString) } } + +func BenchmarkPrintlnNoFlags(b *testing.B) { + const testString = "test" + var buf bytes.Buffer + l := New(&buf, "", 0) + for i := 0; i < b.N; i++ { + buf.Reset() + l.Println(testString) + } +} diff --git a/libgo/go/log/syslog/syslog.go b/libgo/go/log/syslog/syslog.go index df9ffb8..dfd0028 100644 --- a/libgo/go/log/syslog/syslog.go +++ b/libgo/go/log/syslog/syslog.go @@ -102,15 +102,16 @@ type netConn struct { // New establishes a new connection to the system log daemon. Each // write to the returned writer sends a log message with the given -// priority and prefix. +// priority (a combination of the syslog facility and severity) and +// prefix tag. If tag is empty, the os.Args[0] is used. func New(priority Priority, tag string) (*Writer, error) { return Dial("", "", priority, tag) } // Dial establishes a connection to a log daemon by connecting to // address raddr on the specified network. Each write to the returned -// writer sends a log message with the given facility, severity and -// tag. +// writer sends a log message with the facility and severity +// (from priority) and tag. If tag is empty, the os.Args[0] is used. // If network is empty, Dial will connect to the local syslog server. // Otherwise, see the documentation for net.Dial for valid values // of network and raddr. @@ -301,10 +302,10 @@ func (n *netConn) close() error { return n.conn.Close() } -// NewLogger creates a log.Logger whose output is written to -// the system log service with the specified priority. The logFlag -// argument is the flag set passed through to log.New to create -// the Logger. +// NewLogger creates a log.Logger whose output is written to the +// system log service with the specified priority, a combination of +// the syslog facility and severity. The logFlag argument is the flag +// set passed through to log.New to create the Logger. func NewLogger(p Priority, logFlag int) (*log.Logger, error) { s, err := New(p, "") if err != nil { |