1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2025-01-02 15:36:41 -05:00

remove package path from logs (#840)

This commit is contained in:
Kslr 2021-04-02 20:44:33 +08:00 committed by GitHub
parent 44151f3420
commit 1b665d1d36
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 7 additions and 47 deletions

View File

@ -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)
}

View File

@ -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().

View File

@ -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",
},
}