diff options
author | Péter Szilágyi <peterke@gmail.com> | 2017-02-23 17:44:16 +0800 |
---|---|---|
committer | Péter Szilágyi <peterke@gmail.com> | 2017-02-23 18:16:47 +0800 |
commit | f89dd627760b43bd405cb3db1e5efdb100835db5 (patch) | |
tree | edaf8a63eb7f7434cd9b9cbd764b3c4c3d76191e /log/format.go | |
parent | 1ca20a2697e5df840a0541b61f274379b43fddc7 (diff) | |
download | dexon-f89dd627760b43bd405cb3db1e5efdb100835db5.tar dexon-f89dd627760b43bd405cb3db1e5efdb100835db5.tar.gz dexon-f89dd627760b43bd405cb3db1e5efdb100835db5.tar.bz2 dexon-f89dd627760b43bd405cb3db1e5efdb100835db5.tar.lz dexon-f89dd627760b43bd405cb3db1e5efdb100835db5.tar.xz dexon-f89dd627760b43bd405cb3db1e5efdb100835db5.tar.zst dexon-f89dd627760b43bd405cb3db1e5efdb100835db5.zip |
internal, log: support debug log prints, displaying log origins
Diffstat (limited to 'log/format.go')
-rw-r--r-- | log/format.go | 54 |
1 files changed, 50 insertions, 4 deletions
diff --git a/log/format.go b/log/format.go index 9dcfc8d13..2a3790501 100644 --- a/log/format.go +++ b/log/format.go @@ -8,6 +8,7 @@ import ( "strconv" "strings" "sync" + "sync/atomic" "time" ) @@ -18,6 +19,30 @@ const ( termMsgJust = 40 ) +// locationTrims are trimmed for display to avoid unwieldy log lines. +var locationTrims = []string{ + "github.com/ethereum/go-ethereum/", + "github.com/ethereum/ethash/", +} + +// PrintOrigins sets or unsets log location (file:line) printing for terminal +// format output. +func PrintOrigins(print bool) { + if print { + atomic.StoreUint32(&locationEnabled, 1) + } else { + atomic.StoreUint32(&locationEnabled, 0) + } +} + +// locationEnabled is an atomic flag controlling whether the terminal formatter +// should append the log locations too when printing entries. +var locationEnabled uint32 + +// locationLength is the maxmimum path length encountered, which all logs are +// padded to to aid in alignment. +var locationLength uint32 + type Format interface { Format(r *Record) []byte } @@ -64,12 +89,33 @@ func TerminalFormat() Format { b := &bytes.Buffer{} lvl := strings.ToUpper(r.Lvl.String()) - if color > 0 { - fmt.Fprintf(b, "\x1b[%dm%s\x1b[0m[%s] %s ", color, lvl, r.Time.Format(termTimeFormat), r.Msg) + if atomic.LoadUint32(&locationEnabled) != 0 { + // Log origin printing was requested, format the location path and line number + location := fmt.Sprintf("%+v", r.Call) + for _, prefix := range locationTrims { + location = strings.TrimPrefix(location, prefix) + } + // Maintain the maximum location length for fancyer alignment + align := int(atomic.LoadUint32(&locationLength)) + if align < len(location) { + align = len(location) + atomic.StoreUint32(&locationLength, uint32(align)) + } + padding := strings.Repeat(" ", align-len(location)) + + // Assemble and print the log heading + if color > 0 { + fmt.Fprintf(b, "\x1b[%dm%s\x1b[0m[%s|%s]%s %s ", color, lvl, r.Time.Format(termTimeFormat), location, padding, r.Msg) + } else { + fmt.Fprintf(b, "[%s] [%s|%s]%s %s ", lvl, r.Time.Format(termTimeFormat), location, padding, r.Msg) + } } else { - fmt.Fprintf(b, "[%s] [%s] %s ", lvl, r.Time.Format(termTimeFormat), r.Msg) + if color > 0 { + fmt.Fprintf(b, "\x1b[%dm%s\x1b[0m[%s] %s ", color, lvl, r.Time.Format(termTimeFormat), r.Msg) + } else { + fmt.Fprintf(b, "[%s] [%s] %s ", lvl, r.Time.Format(termTimeFormat), r.Msg) + } } - // try to justify the log output for short messages if len(r.Ctx) > 0 && len(r.Msg) < termMsgJust { b.Write(bytes.Repeat([]byte{' '}, termMsgJust-len(r.Msg))) |