Browse Source

remove package path from logs (#840)

Kslr 4 years ago
parent
commit
1b665d1d36
3 changed files with 7 additions and 47 deletions
  1. 0 33
      common/common.go
  2. 5 1
      common/errors/errors.go
  3. 2 13
      common/errors/errors_test.go

+ 0 - 33
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)
-}

+ 5 - 1
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().

+ 2 - 13
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",
 		},
 	}