From d6eec0fa528f8ae13aca8deda3ac698f17e1cce6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E7=A5=A5=E8=B6=85?= Date: Mon, 18 Apr 2022 16:31:48 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8E=BB=E9=99=A4=E9=94=99=E8=AF=AF=E6=8F=90?= =?UTF-8?q?=E7=A4=BA=E4=B8=AD=E7=9A=84=E7=A8=8B=E5=BA=8F=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E5=90=8D=E4=B8=AD=E7=9A=84Workspace=E7=9B=AE=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/apps/log_writer.go | 3 ++- internal/errors/error.go | 7 ++++--- internal/utils/workspace.go | 15 +++++++++++++++ 3 files changed, 21 insertions(+), 4 deletions(-) create mode 100644 internal/utils/workspace.go diff --git a/internal/apps/log_writer.go b/internal/apps/log_writer.go index 23bdedd..a09bf00 100644 --- a/internal/apps/log_writer.go +++ b/internal/apps/log_writer.go @@ -2,6 +2,7 @@ package apps import ( "github.com/TeaOSLab/EdgeNode/internal/goman" + "github.com/TeaOSLab/EdgeNode/internal/utils" "github.com/TeaOSLab/EdgeNode/internal/utils/sizes" "github.com/iwind/TeaGo/Tea" "github.com/iwind/TeaGo/files" @@ -77,7 +78,7 @@ func (this *LogWriter) Write(message string) { var ok bool _, file, line, ok = runtime.Caller(callDepth) if ok { - file = this.packagePath(file) + file = utils.RemoveWorkspace(this.packagePath(file)) } } diff --git a/internal/errors/error.go b/internal/errors/error.go index 74faa7d..dbea041 100644 --- a/internal/errors/error.go +++ b/internal/errors/error.go @@ -2,6 +2,7 @@ package errors import ( "errors" + "github.com/TeaOSLab/EdgeNode/internal/utils" "path/filepath" "runtime" "strconv" @@ -15,7 +16,7 @@ type errorObj struct { } func (this *errorObj) Error() string { - s := this.err.Error() + "\n " + this.file + s := this.err.Error() + "\n " + utils.RemoveWorkspace(this.file) if len(this.funcName) > 0 { s += ":" + this.funcName + "()" } @@ -23,7 +24,7 @@ func (this *errorObj) Error() string { return s } -// 新错误 +// New 新错误 func New(errText string) error { ptr, file, line, ok := runtime.Caller(1) funcName := "" @@ -39,7 +40,7 @@ func New(errText string) error { } } -// 包装已有错误 +// Wrap 包装已有错误 func Wrap(err error) error { if err == nil { return nil diff --git a/internal/utils/workspace.go b/internal/utils/workspace.go new file mode 100644 index 0000000..eeea7bf --- /dev/null +++ b/internal/utils/workspace.go @@ -0,0 +1,15 @@ +// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved. + +package utils + +import "regexp" + +var workspaceReg = regexp.MustCompile(`/Edge[A-Z]\w+/`) + +func RemoveWorkspace(path string) string { + var indexes = workspaceReg.FindAllStringIndex(path, -1) + if len(indexes) > 0 { + return path[indexes[len(indexes)-1][0]:] + } + return path +}