Refactor log (#3446)

* Refactor log

* Add new log methods

* Fix logger test

* Change all logging code

* Clean up pathObj

* Rebase to latest main

* Remove invoking method name after the dot
This commit is contained in:
yuhan6665 2024-06-29 14:32:57 -04:00 committed by GitHub
parent 8320732743
commit 079d0bd8a9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
291 changed files with 1837 additions and 2368 deletions

View file

@ -2,6 +2,7 @@ package domainsocket
import (
"github.com/xtls/xray-core/common"
"github.com/xtls/xray-core/common/errors"
"github.com/xtls/xray-core/common/net"
"github.com/xtls/xray-core/transport/internet"
)
@ -14,7 +15,7 @@ const (
func (c *Config) GetUnixAddr() (*net.UnixAddr, error) {
path := c.Path
if path == "" {
return nil, newError("empty domain socket path")
return nil, errors.New("empty domain socket path")
}
if c.Abstract && path[0] != '@' {
path = "@" + path

View file

@ -7,6 +7,7 @@ import (
"context"
"github.com/xtls/xray-core/common"
"github.com/xtls/xray-core/common/errors"
"github.com/xtls/xray-core/common/net"
"github.com/xtls/xray-core/transport/internet"
"github.com/xtls/xray-core/transport/internet/reality"
@ -23,7 +24,7 @@ func Dial(ctx context.Context, dest net.Destination, streamSettings *internet.Me
conn, err := net.DialUnix("unix", nil, addr)
if err != nil {
return nil, newError("failed to dial unix: ", settings.Path).Base(err).AtWarning()
return nil, errors.New("failed to dial unix: ", settings.Path).Base(err).AtWarning()
}
if config := tls.ConfigFromStreamSettings(streamSettings); config != nil {

View file

@ -1,9 +0,0 @@
package domainsocket
import "github.com/xtls/xray-core/common/errors"
type errPathObjHolder struct{}
func newError(values ...interface{}) *errors.Error {
return errors.New(values...).WithPathObj(errPathObjHolder{})
}

View file

@ -11,6 +11,7 @@ import (
goreality "github.com/xtls/reality"
"github.com/xtls/xray-core/common"
"github.com/xtls/xray-core/common/errors"
"github.com/xtls/xray-core/common/net"
"github.com/xtls/xray-core/transport/internet"
"github.com/xtls/xray-core/transport/internet/reality"
@ -38,7 +39,7 @@ func Listen(ctx context.Context, address net.Address, port net.Port, streamSetti
unixListener, err := net.ListenUnix("unix", addr)
if err != nil {
return nil, newError("failed to listen domain socket").Base(err).AtWarning()
return nil, errors.New("failed to listen domain socket").Base(err).AtWarning()
}
ln := &Listener{
@ -88,7 +89,7 @@ func (ln *Listener) run() {
if strings.Contains(err.Error(), "closed") {
break
}
newError("failed to accepted raw connections").Base(err).AtWarning().WriteToLog()
errors.LogWarningInner(context.Background(), err, "failed to accepted raw connections")
continue
}
go func() {
@ -96,7 +97,7 @@ func (ln *Listener) run() {
conn = tls.Server(conn, ln.tlsConfig)
} else if ln.realityConfig != nil {
if conn, err = reality.Server(conn, ln.realityConfig); err != nil {
newError(err).AtInfo().WriteToLog()
errors.LogInfo(context.Background(), err.Error())
return
}
}
@ -117,7 +118,7 @@ func (fl *fileLocker) Acquire() error {
}
if err := unix.Flock(int(f.Fd()), unix.LOCK_EX); err != nil {
f.Close()
return newError("failed to lock file: ", fl.path).Base(err)
return errors.New("failed to lock file: ", fl.path).Base(err)
}
fl.file = f
return nil
@ -125,13 +126,13 @@ func (fl *fileLocker) Acquire() error {
func (fl *fileLocker) Release() {
if err := unix.Flock(int(fl.file.Fd()), unix.LOCK_UN); err != nil {
newError("failed to unlock file: ", fl.path).Base(err).WriteToLog()
errors.LogInfoInner(context.Background(), err, "failed to unlock file: ", fl.path)
}
if err := fl.file.Close(); err != nil {
newError("failed to close file: ", fl.path).Base(err).WriteToLog()
errors.LogInfoInner(context.Background(), err, "failed to close file: ", fl.path)
}
if err := os.Remove(fl.path); err != nil {
newError("failed to remove file: ", fl.path).Base(err).WriteToLog()
errors.LogInfoInner(context.Background(), err, "failed to remove file: ", fl.path)
}
}