Logging: errorIf fatalIf print in the format [file.go:82:funcName()] (#3127)

This commit is contained in:
Krishna Srinivas
2016-10-29 15:04:16 +05:30
committed by Harshavardhana
parent 79b98b5c25
commit 0b3282ac9f
5 changed files with 15 additions and 91 deletions
+6 -40
View File
@@ -17,13 +17,9 @@
package cmd
import (
"bufio"
"bytes"
"fmt"
"path"
"path/filepath"
"runtime"
"runtime/debug"
"strings"
"github.com/Sirupsen/logrus"
@@ -46,37 +42,6 @@ type logger struct {
// Add new loggers here.
}
// Function takes input with the results from runtime.Caller(1). Depending on the boolean.
// This function can either returned a shotFile form or a longFile form.
func funcFromPC(pc uintptr, file string, line int, shortFile bool) string {
var fn, name string
if shortFile {
fn = strings.Replace(file, path.Join(filepath.ToSlash(GOPATH)+"/src/github.com/minio/minio/cmd/")+"/", "", -1)
name = strings.Replace(runtime.FuncForPC(pc).Name(), "github.com/minio/minio/cmd.", "", -1)
} else {
fn = strings.Replace(file, path.Join(filepath.ToSlash(GOPATH)+"/src/")+"/", "", -1)
name = strings.Replace(runtime.FuncForPC(pc).Name(), "github.com/minio/minio/cmd.", "", -1)
}
return fmt.Sprintf("%s [%s:%d]", name, fn, line)
}
// stackInfo returns printable stack trace.
func stackInfo() string {
// Convert stack-trace bytes to io.Reader.
rawStack := bufio.NewReader(bytes.NewBuffer(debug.Stack()))
// Skip stack trace lines until our real caller.
for i := 0; i <= 4; i++ {
rawStack.ReadLine()
}
// Read the rest of useful stack trace.
stackBuf := new(bytes.Buffer)
stackBuf.ReadFrom(rawStack)
// Strip GOPATH of the build system and return.
return strings.Replace(stackBuf.String(), filepath.ToSlash(GOPATH)+"/src/", "", -1)
}
// Get file, line, function name of the caller.
func callerLocation() string {
pc, file, line, success := runtime.Caller(2)
@@ -84,9 +49,10 @@ func callerLocation() string {
file = "<unknown>"
line = 0
}
shortFile := true // We are only interested in short file form.
callerLoc := funcFromPC(pc, file, line, shortFile)
return callerLoc
file = path.Base(file)
name := runtime.FuncForPC(pc).Name()
name = strings.TrimPrefix(name, "github.com/minio/minio/cmd.")
return fmt.Sprintf("[%s:%d:%s()]", file, line, name)
}
// errorIf synonymous with fatalIf but doesn't exit on error != nil
@@ -116,8 +82,8 @@ func fatalIf(err error, msg string, data ...interface{}) {
"location": location,
"cause": err.Error(),
}
if globalTrace {
fields["stack"] = "\n" + stackInfo()
if e, ok := err.(*Error); ok {
fields["stack"] = strings.Join(e.Trace(), " ")
}
log.WithFields(fields).Fatalf(msg, data...)
}