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

@ -1,9 +0,0 @@
package shadowsocks_2022
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

@ -13,6 +13,7 @@ import (
N "github.com/sagernet/sing/common/network"
"github.com/xtls/xray-core/common"
"github.com/xtls/xray-core/common/buf"
"github.com/xtls/xray-core/common/errors"
"github.com/xtls/xray-core/common/log"
"github.com/xtls/xray-core/common/net"
"github.com/xtls/xray-core/common/protocol"
@ -49,11 +50,11 @@ func NewServer(ctx context.Context, config *ServerConfig) (*Inbound, error) {
level: int(config.Level),
}
if !C.Contains(shadowaead_2022.List, config.Method) {
return nil, newError("unsupported method ", config.Method)
return nil, errors.New("unsupported method ", config.Method)
}
service, err := shadowaead_2022.NewServiceWithPassword(config.Method, config.Key, 500, inbound, nil)
if err != nil {
return nil, newError("create service").Base(err)
return nil, errors.New("create service").Base(err)
}
inbound.service = service
return inbound, nil
@ -112,7 +113,7 @@ func (i *Inbound) NewConnection(ctx context.Context, conn net.Conn, metadata M.M
Status: log.AccessAccepted,
Email: i.email,
})
newError("tunnelling request to tcp:", metadata.Destination).WriteToLog(session.ExportIDToError(ctx))
errors.LogInfo(ctx, "tunnelling request to tcp:", metadata.Destination)
dispatcher := session.DispatcherFromContext(ctx)
link, err := dispatcher.Dispatch(ctx, singbridge.ToDestination(metadata.Destination, net.Network_TCP))
if err != nil {
@ -133,7 +134,7 @@ func (i *Inbound) NewPacketConnection(ctx context.Context, conn N.PacketConn, me
Status: log.AccessAccepted,
Email: i.email,
})
newError("tunnelling request to udp:", metadata.Destination).WriteToLog(session.ExportIDToError(ctx))
errors.LogInfo(ctx, "tunnelling request to udp:", metadata.Destination)
dispatcher := session.DispatcherFromContext(ctx)
destination := singbridge.ToDestination(metadata.Destination, net.Network_UDP)
link, err := dispatcher.Dispatch(ctx, destination)
@ -152,7 +153,7 @@ func (i *Inbound) NewError(ctx context.Context, err error) {
if E.IsClosed(err) {
return
}
newError(err).AtWarning().WriteToLog()
errors.LogWarning(ctx, err.Error())
}
type natPacketConn struct {

View file

@ -17,6 +17,7 @@ import (
N "github.com/sagernet/sing/common/network"
"github.com/xtls/xray-core/common"
"github.com/xtls/xray-core/common/buf"
"github.com/xtls/xray-core/common/errors"
"github.com/xtls/xray-core/common/log"
"github.com/xtls/xray-core/common/net"
"github.com/xtls/xray-core/common/protocol"
@ -53,15 +54,15 @@ func NewMultiServer(ctx context.Context, config *MultiUserServerConfig) (*MultiU
users: config.Users,
}
if config.Key == "" {
return nil, newError("missing key")
return nil, errors.New("missing key")
}
psk, err := base64.StdEncoding.DecodeString(config.Key)
if err != nil {
return nil, newError("parse config").Base(err)
return nil, errors.New("parse config").Base(err)
}
service, err := shadowaead_2022.NewMultiService[int](config.Method, psk, 500, inbound, nil)
if err != nil {
return nil, newError("create service").Base(err)
return nil, errors.New("create service").Base(err)
}
for i, user := range config.Users {
@ -75,7 +76,7 @@ func NewMultiServer(ctx context.Context, config *MultiUserServerConfig) (*MultiU
C.Map(config.Users, func(it *User) string { return it.Key }),
)
if err != nil {
return nil, newError("create service").Base(err)
return nil, errors.New("create service").Base(err)
}
inbound.service = service
@ -91,7 +92,7 @@ func (i *MultiUserInbound) AddUser(ctx context.Context, u *protocol.MemoryUser)
if account.Email != "" {
for idx := range i.users {
if i.users[idx].Email == account.Email {
return newError("User ", account.Email, " already exists.")
return errors.New("User ", account.Email, " already exists.")
}
}
}
@ -114,7 +115,7 @@ func (i *MultiUserInbound) AddUser(ctx context.Context, u *protocol.MemoryUser)
// RemoveUser implements proxy.UserManager.RemoveUser().
func (i *MultiUserInbound) RemoveUser(ctx context.Context, email string) error {
if email == "" {
return newError("Email must not be empty.")
return errors.New("Email must not be empty.")
}
i.Lock()
@ -129,7 +130,7 @@ func (i *MultiUserInbound) RemoveUser(ctx context.Context, email string) error {
}
if idx == -1 {
return newError("User ", email, " not found.")
return errors.New("User ", email, " not found.")
}
ulen := len(i.users)
@ -203,11 +204,11 @@ func (i *MultiUserInbound) NewConnection(ctx context.Context, conn net.Conn, met
Status: log.AccessAccepted,
Email: user.Email,
})
newError("tunnelling request to tcp:", metadata.Destination).WriteToLog(session.ExportIDToError(ctx))
errors.LogInfo(ctx, "tunnelling request to tcp:", metadata.Destination)
dispatcher := session.DispatcherFromContext(ctx)
destination := singbridge.ToDestination(metadata.Destination, net.Network_TCP)
if !destination.IsValid() {
return newError("invalid destination")
return errors.New("invalid destination")
}
link, err := dispatcher.Dispatch(ctx, destination)
@ -231,7 +232,7 @@ func (i *MultiUserInbound) NewPacketConnection(ctx context.Context, conn N.Packe
Status: log.AccessAccepted,
Email: user.Email,
})
newError("tunnelling request to udp:", metadata.Destination).WriteToLog(session.ExportIDToError(ctx))
errors.LogInfo(ctx, "tunnelling request to udp:", metadata.Destination)
dispatcher := session.DispatcherFromContext(ctx)
destination := singbridge.ToDestination(metadata.Destination, net.Network_UDP)
link, err := dispatcher.Dispatch(ctx, destination)
@ -250,5 +251,5 @@ func (i *MultiUserInbound) NewError(ctx context.Context, err error) {
if E.IsClosed(err) {
return
}
newError(err).AtWarning().WriteToLog()
errors.LogWarning(ctx, err.Error())
}

View file

@ -15,6 +15,7 @@ import (
N "github.com/sagernet/sing/common/network"
"github.com/xtls/xray-core/common"
"github.com/xtls/xray-core/common/buf"
"github.com/xtls/xray-core/common/errors"
"github.com/xtls/xray-core/common/log"
"github.com/xtls/xray-core/common/net"
"github.com/xtls/xray-core/common/protocol"
@ -50,11 +51,11 @@ func NewRelayServer(ctx context.Context, config *RelayServerConfig) (*RelayInbou
destinations: config.Destinations,
}
if !C.Contains(shadowaead_2022.List, config.Method) || !strings.Contains(config.Method, "aes") {
return nil, newError("unsupported method ", config.Method)
return nil, errors.New("unsupported method ", config.Method)
}
service, err := shadowaead_2022.NewRelayServiceWithPassword[int](config.Method, config.Key, 500, inbound)
if err != nil {
return nil, newError("create service").Base(err)
return nil, errors.New("create service").Base(err)
}
for i, destination := range config.Destinations {
@ -74,7 +75,7 @@ func NewRelayServer(ctx context.Context, config *RelayServerConfig) (*RelayInbou
}),
)
if err != nil {
return nil, newError("create service").Base(err)
return nil, errors.New("create service").Base(err)
}
inbound.service = service
return inbound, nil
@ -135,7 +136,7 @@ func (i *RelayInbound) NewConnection(ctx context.Context, conn net.Conn, metadat
Status: log.AccessAccepted,
Email: user.Email,
})
newError("tunnelling request to tcp:", metadata.Destination).WriteToLog(session.ExportIDToError(ctx))
errors.LogInfo(ctx, "tunnelling request to tcp:", metadata.Destination)
dispatcher := session.DispatcherFromContext(ctx)
link, err := dispatcher.Dispatch(ctx, singbridge.ToDestination(metadata.Destination, net.Network_TCP))
if err != nil {
@ -158,7 +159,7 @@ func (i *RelayInbound) NewPacketConnection(ctx context.Context, conn N.PacketCon
Status: log.AccessAccepted,
Email: user.Email,
})
newError("tunnelling request to udp:", metadata.Destination).WriteToLog(session.ExportIDToError(ctx))
errors.LogInfo(ctx, "tunnelling request to udp:", metadata.Destination)
dispatcher := session.DispatcherFromContext(ctx)
destination := singbridge.ToDestination(metadata.Destination, net.Network_UDP)
link, err := dispatcher.Dispatch(ctx, destination)
@ -177,5 +178,5 @@ func (i *RelayInbound) NewError(ctx context.Context, err error) {
if E.IsClosed(err) {
return
}
newError(err).AtWarning().WriteToLog()
errors.LogWarning(ctx, err.Error())
}

View file

@ -13,6 +13,7 @@ import (
"github.com/sagernet/sing/common/uot"
"github.com/xtls/xray-core/common"
"github.com/xtls/xray-core/common/buf"
"github.com/xtls/xray-core/common/errors"
"github.com/xtls/xray-core/common/net"
"github.com/xtls/xray-core/common/session"
"github.com/xtls/xray-core/common/singbridge"
@ -44,15 +45,15 @@ func NewClient(ctx context.Context, config *ClientConfig) (*Outbound, error) {
}
if C.Contains(shadowaead_2022.List, config.Method) {
if config.Key == "" {
return nil, newError("missing psk")
return nil, errors.New("missing psk")
}
method, err := shadowaead_2022.NewWithPassword(config.Method, config.Key, nil)
if err != nil {
return nil, newError("create method").Base(err)
return nil, errors.New("create method").Base(err)
}
o.method = method
} else {
return nil, newError("unknown method ", config.Method)
return nil, errors.New("unknown method ", config.Method)
}
if config.UdpOverTcp {
o.uotClient = &uot.Client{Version: uint8(config.UdpOverTcpVersion)}
@ -68,16 +69,16 @@ func (o *Outbound) Process(ctx context.Context, link *transport.Link, dialer int
}
outbounds := session.OutboundsFromContext(ctx)
ob := outbounds[len(outbounds) - 1]
ob := outbounds[len(outbounds)-1]
if !ob.Target.IsValid() {
return newError("target not specified")
return errors.New("target not specified")
}
ob.Name = "shadowsocks-2022"
ob.CanSpliceCopy = 3
destination := ob.Target
network := destination.Network
newError("tunneling request to ", destination, " via ", o.server.NetAddr()).WriteToLog(session.ExportIDToError(ctx))
errors.LogInfo(ctx, "tunneling request to ", destination, " via ", o.server.NetAddr())
serverDestination := o.server
if o.uotClient != nil {
@ -87,7 +88,7 @@ func (o *Outbound) Process(ctx context.Context, link *transport.Link, dialer int
}
connection, err := dialer.Dial(ctx, serverDestination)
if err != nil {
return newError("failed to connect to server").Base(err)
return errors.New("failed to connect to server").Base(err)
}
if session.TimeoutOnlyFromContext(ctx) {
@ -100,7 +101,7 @@ func (o *Outbound) Process(ctx context.Context, link *transport.Link, dialer int
if timeoutReader, isTimeoutReader := link.Reader.(buf.TimeoutReader); isTimeoutReader {
mb, err := timeoutReader.ReadMultiBufferTimeout(time.Millisecond * 100)
if err != nil && err != buf.ErrNotTimeoutReader && err != buf.ErrReadTimeout {
return newError("read payload").Base(err)
return errors.New("read payload").Base(err)
}
payload := B.New()
for {
@ -111,7 +112,7 @@ func (o *Outbound) Process(ctx context.Context, link *transport.Link, dialer int
_, err = serverConn.Write(payload.Bytes())
if err != nil {
payload.Release()
return newError("write payload").Base(err)
return errors.New("write payload").Base(err)
}
handshake = true
}
@ -125,7 +126,7 @@ func (o *Outbound) Process(ctx context.Context, link *transport.Link, dialer int
if !handshake {
_, err = serverConn.Write(nil)
if err != nil {
return newError("client handshake").Base(err)
return errors.New("client handshake").Base(err)
}
}
return singbridge.CopyConn(ctx, inboundConn, link, serverConn)