From 1b665d1d3684dec918c0362f29601a0285917ea4 Mon Sep 17 00:00:00 2001 From: Kslr Date: Fri, 2 Apr 2021 20:44:33 +0800 Subject: [PATCH] remove package path from logs (#840) --- common/common.go | 33 --------------------------------- common/errors/errors.go | 6 +++++- common/errors/errors_test.go | 15 ++------------- 3 files changed, 7 insertions(+), 47 deletions(-) diff --git a/common/common.go b/common/common.go index 6ef3531bb..00cc1d833 100644 --- a/common/common.go +++ b/common/common.go @@ -123,36 +123,3 @@ func GetGOPATH() string { } return GOPATH } - -// GetModuleName returns the value of module in `go.mod` file. -func GetModuleName(pathToProjectRoot string) (string, error) { - var moduleName string - loopPath := pathToProjectRoot - for { - if idx := strings.LastIndex(loopPath, string(filepath.Separator)); idx >= 0 { - gomodPath := filepath.Join(loopPath, "go.mod") - gomodBytes, err := ioutil.ReadFile(gomodPath) - if err != nil { - loopPath = loopPath[:idx] - continue - } - - gomodContent := string(gomodBytes) - moduleIdx := strings.Index(gomodContent, "module ") - newLineIdx := strings.Index(gomodContent, "\n") - - if moduleIdx >= 0 { - if newLineIdx >= 0 { - moduleName = strings.TrimSpace(gomodContent[moduleIdx+6 : newLineIdx]) - moduleName = strings.TrimSuffix(moduleName, "\r") - } else { - moduleName = strings.TrimSpace(gomodContent[moduleIdx+6:]) - } - return moduleName, nil - } - return "", fmt.Errorf("can not get module path in `%s`", gomodPath) - } - break - } - return moduleName, fmt.Errorf("no `go.mod` file in every parent directory of `%s`", pathToProjectRoot) -} diff --git a/common/errors/errors.go b/common/errors/errors.go index 15483ea9a..a40bdef94 100644 --- a/common/errors/errors.go +++ b/common/errors/errors.go @@ -37,7 +37,11 @@ func (err *Error) pkgPath() string { if err.pathObj == nil { return "" } - return reflect.TypeOf(err.pathObj).PkgPath() + path := reflect.TypeOf(err.pathObj).PkgPath() + if strings.HasPrefix(path, "github.com/v2fly/v2ray-core/v4/") { + return path[31:] + } + return path } // Error implements error.Error(). diff --git a/common/errors/errors_test.go b/common/errors/errors_test.go index e51e6aa82..6b1cffcdc 100644 --- a/common/errors/errors_test.go +++ b/common/errors/errors_test.go @@ -2,13 +2,11 @@ package errors_test import ( "io" - "os" "strings" "testing" "github.com/google/go-cmp/cmp" - "github.com/v2fly/v2ray-core/v4/common" . "github.com/v2fly/v2ray-core/v4/common/errors" "github.com/v2fly/v2ray-core/v4/common/log" ) @@ -42,26 +40,17 @@ func TestError(t *testing.T) { type e struct{} func TestErrorMessage(t *testing.T) { - pwd, err := os.Getwd() - if err != nil { - t.Error(err) - } - moduleName, gmnErr := common.GetModuleName(pwd) - if gmnErr != nil { - t.Error(gmnErr) - } - data := []struct { err error msg string }{ { err: New("a").Base(New("b")).WithPathObj(e{}), - msg: moduleName + "/common/errors_test: a > b", + msg: "common/errors_test: a > b", }, { err: New("a").Base(New("b").WithPathObj(e{})), - msg: "a > " + moduleName + "/common/errors_test: b", + msg: "a > common/errors_test: b", }, }