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

@ -7,6 +7,7 @@ import (
"github.com/xtls/xray-core/common"
"github.com/xtls/xray-core/common/buf"
"github.com/xtls/xray-core/common/cmdarg"
"github.com/xtls/xray-core/common/errors"
"github.com/xtls/xray-core/main/confloader"
"google.golang.org/protobuf/proto"
)
@ -38,14 +39,14 @@ var (
func RegisterConfigLoader(format *ConfigFormat) error {
name := strings.ToLower(format.Name)
if _, found := configLoaderByName[name]; found {
return newError(format.Name, " already registered.")
return errors.New(format.Name, " already registered.")
}
configLoaderByName[name] = format
for _, ext := range format.Extension {
lext := strings.ToLower(ext)
if f, found := configLoaderByExt[lext]; found {
return newError(ext, " already registered to ", f.Name)
return errors.New(ext, " already registered to ", f.Name)
}
configLoaderByExt[lext] = format
}
@ -116,7 +117,7 @@ func LoadConfig(formatName string, input interface{}) (*Config, error) {
}
if f == "" {
return nil, newError("Failed to get format of ", file).AtWarning()
return nil, errors.New("Failed to get format of ", file).AtWarning()
}
if f == "protobuf" {
@ -130,7 +131,7 @@ func LoadConfig(formatName string, input interface{}) (*Config, error) {
if len(v) == 1 {
return configLoaderByName["protobuf"].Loader(v)
} else {
return nil, newError("Only one protobuf config file is allowed").AtWarning()
return nil, errors.New("Only one protobuf config file is allowed").AtWarning()
}
}
@ -141,11 +142,11 @@ func LoadConfig(formatName string, input interface{}) (*Config, error) {
if f, found := configLoaderByName[formatName]; found {
return f.Loader(v)
} else {
return nil, newError("Unable to load config in", formatName).AtWarning()
return nil, errors.New("Unable to load config in", formatName).AtWarning()
}
}
return nil, newError("Unable to load config").AtWarning()
return nil, errors.New("Unable to load config").AtWarning()
}
func loadProtobufConfig(data []byte) (*Config, error) {
@ -173,7 +174,7 @@ func init() {
common.Must(err)
return loadProtobufConfig(data)
default:
return nil, newError("unknow type")
return nil, errors.New("unknow type")
}
},
}))

View file

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

@ -5,6 +5,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/common/net/cnc"
"github.com/xtls/xray-core/features/routing"
@ -50,7 +51,7 @@ func Dial(ctx context.Context, v *Instance, dest net.Destination) (net.Conn, err
dispatcher := v.GetFeature(routing.DispatcherType())
if dispatcher == nil {
return nil, newError("routing.Dispatcher is not registered in Xray core")
return nil, errors.New("routing.Dispatcher is not registered in Xray core")
}
r, err := dispatcher.(routing.Dispatcher).Dispatch(ctx, dest)
@ -77,7 +78,7 @@ func DialUDP(ctx context.Context, v *Instance) (net.PacketConn, error) {
dispatcher := v.GetFeature(routing.DispatcherType())
if dispatcher == nil {
return nil, newError("routing.Dispatcher is not registered in Xray core")
return nil, errors.New("routing.Dispatcher is not registered in Xray core")
}
return udp.DialDispatcher(ctx, dispatcher.(routing.Dispatcher))
}

View file

@ -6,6 +6,7 @@ import (
"sync"
"github.com/xtls/xray-core/common"
"github.com/xtls/xray-core/common/errors"
"github.com/xtls/xray-core/common/platform"
"github.com/xtls/xray-core/common/serial"
"github.com/xtls/xray-core/features"
@ -104,7 +105,7 @@ func AddInboundHandler(server *Instance, config *InboundHandlerConfig) error {
}
handler, ok := rawHandler.(inbound.Handler)
if !ok {
return newError("not an InboundHandler")
return errors.New("not an InboundHandler")
}
if err := inboundManager.AddHandler(server.ctx, handler); err != nil {
return err
@ -130,7 +131,7 @@ func AddOutboundHandler(server *Instance, config *OutboundHandlerConfig) error {
}
handler, ok := rawHandler.(outbound.Handler)
if !ok {
return newError("not an OutboundHandler")
return errors.New("not an OutboundHandler")
}
if err := outboundManager.AddHandler(server.ctx, handler); err != nil {
return err
@ -181,7 +182,7 @@ func NewWithContext(ctx context.Context, config *Config) (*Instance, error) {
}
func initInstanceWithConfig(config *Config, server *Instance) (bool, error) {
server.ctx = context.WithValue(server.ctx, "cone",
server.ctx = context.WithValue(server.ctx, "cone",
platform.NewEnvFlag(platform.UseCone).GetValue(func() string { return "" }) != "true")
if config.Transport != nil {
@ -234,7 +235,7 @@ func initInstanceWithConfig(config *Config, server *Instance) (bool, error) {
)
if server.featureResolutions != nil {
return true, newError("not all dependency are resolved.")
return true, errors.New("not all dependency are resolved.")
}
if err := addInboundHandlers(server, config.Inbound); err != nil {
@ -259,14 +260,14 @@ func (s *Instance) Close() error {
s.running = false
var errors []interface{}
var errs []interface{}
for _, f := range s.features {
if err := f.Close(); err != nil {
errors = append(errors, err)
errs = append(errs, err)
}
}
if len(errors) > 0 {
return newError("failed to close all features").Base(newError(serial.Concat(errors...)))
if len(errs) > 0 {
return errors.New("failed to close all features").Base(errors.New(serial.Concat(errs...)))
}
return nil
@ -302,7 +303,7 @@ func (s *Instance) AddFeature(feature features.Feature) error {
if s.running {
if err := feature.Start(); err != nil {
newError("failed to start feature").Base(err).WriteToLog()
errors.LogInfoInner(s.ctx, err, "failed to start feature")
}
return nil
}
@ -350,7 +351,7 @@ func (s *Instance) Start() error {
}
}
newError("Xray ", Version(), " started").AtWarning().WriteToLog()
errors.LogWarning(s.ctx, "Xray ", Version(), " started")
return nil
}