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 kcp
import (
"bytes"
"context"
"io"
"net"
"runtime"
@ -10,14 +11,15 @@ import (
"time"
"github.com/xtls/xray-core/common/buf"
"github.com/xtls/xray-core/common/errors"
"github.com/xtls/xray-core/common/signal"
"github.com/xtls/xray-core/common/signal/semaphore"
)
var (
ErrIOTimeout = newError("Read/Write timeout")
ErrClosedListener = newError("Listener closed.")
ErrClosedConnection = newError("Connection closed.")
ErrIOTimeout = errors.New("Read/Write timeout")
ErrClosedListener = errors.New("Listener closed.")
ErrClosedConnection = errors.New("Connection closed.")
)
// State of the connection
@ -203,7 +205,7 @@ type Connection struct {
// NewConnection create a new KCP connection between local and remote.
func NewConnection(meta ConnMetadata, writer PacketWriter, closer io.Closer, config *Config) *Connection {
newError("#", meta.Conversation, " creating connection to ", meta.RemoteAddr).WriteToLog()
errors.LogInfo(context.Background(), "#", meta.Conversation, " creating connection to ", meta.RemoteAddr)
conn := &Connection{
meta: meta,
@ -428,7 +430,7 @@ func (c *Connection) SetState(state State) {
current := c.Elapsed()
atomic.StoreInt32((*int32)(&c.state), int32(state))
atomic.StoreUint32(&c.stateBeginTime, current)
newError("#", c.meta.Conversation, " entering state ", state, " at ", current).AtDebug().WriteToLog()
errors.LogDebug(context.Background(), "#", c.meta.Conversation, " entering state ", state, " at ", current)
switch state {
case StateReadyToClose:
@ -472,7 +474,7 @@ func (c *Connection) Close() error {
c.SetState(StateTerminated)
}
newError("#", c.meta.Conversation, " closing connection to ", c.meta.RemoteAddr).WriteToLog()
errors.LogInfo(context.Background(), "#", c.meta.Conversation, " closing connection to ", c.meta.RemoteAddr)
return nil
}
@ -528,7 +530,7 @@ func (c *Connection) Terminate() {
if c == nil {
return
}
newError("#", c.meta.Conversation, " terminating connection to ", c.RemoteAddr()).WriteToLog()
errors.LogInfo(context.Background(), "#", c.meta.Conversation, " terminating connection to ", c.RemoteAddr())
// v.SetState(StateTerminated)
c.dataInput.Signal()
@ -616,7 +618,7 @@ func (c *Connection) flush() {
}
if c.State() == StateTerminating {
newError("#", c.meta.Conversation, " sending terminating cmd.").AtDebug().WriteToLog()
errors.LogDebug(context.Background(), "#", c.meta.Conversation, " sending terminating cmd.")
c.Ping(current, CommandTerminate)
if current-atomic.LoadUint32(&c.stateBeginTime) > 8000 {

View file

@ -6,6 +6,7 @@ import (
"hash/fnv"
"github.com/xtls/xray-core/common"
"github.com/xtls/xray-core/common/errors"
)
// SimpleAuthenticator is a legacy AEAD used for KCP encryption.
@ -64,12 +65,12 @@ func (a *SimpleAuthenticator) Open(dst, nonce, cipherText, extra []byte) ([]byte
fnvHash := fnv.New32a()
common.Must2(fnvHash.Write(dst[4:]))
if binary.BigEndian.Uint32(dst[:4]) != fnvHash.Sum32() {
return nil, newError("invalid auth")
return nil, errors.New("invalid auth")
}
length := binary.BigEndian.Uint16(dst[4:6])
if len(dst)-6 != int(length) {
return nil, newError("invalid auth")
return nil, errors.New("invalid auth")
}
return dst[6:], nil

View file

@ -8,6 +8,7 @@ import (
"github.com/xtls/xray-core/common"
"github.com/xtls/xray-core/common/buf"
"github.com/xtls/xray-core/common/dice"
"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/stat"
@ -46,22 +47,22 @@ func fetchInput(_ context.Context, input io.Reader, reader PacketReader, conn *C
// DialKCP dials a new KCP connections to the specific destination.
func DialKCP(ctx context.Context, dest net.Destination, streamSettings *internet.MemoryStreamConfig) (stat.Connection, error) {
dest.Network = net.Network_UDP
newError("dialing mKCP to ", dest).WriteToLog()
errors.LogInfo(ctx, "dialing mKCP to ", dest)
rawConn, err := internet.DialSystem(ctx, dest, streamSettings.SocketSettings)
if err != nil {
return nil, newError("failed to dial to dest: ", err).AtWarning().Base(err)
return nil, errors.New("failed to dial to dest: ", err).AtWarning().Base(err)
}
kcpSettings := streamSettings.ProtocolSettings.(*Config)
header, err := kcpSettings.GetPackerHeader()
if err != nil {
return nil, newError("failed to create packet header").Base(err)
return nil, errors.New("failed to create packet header").Base(err)
}
security, err := kcpSettings.GetSecurity()
if err != nil {
return nil, newError("failed to create security").Base(err)
return nil, errors.New("failed to create security").Base(err)
}
reader := &KCPPacketReader{
Header: header,

View file

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

@ -8,6 +8,7 @@ import (
"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/transport/internet"
"github.com/xtls/xray-core/transport/internet/stat"
@ -38,11 +39,11 @@ func NewListener(ctx context.Context, address net.Address, port net.Port, stream
kcpSettings := streamSettings.ProtocolSettings.(*Config)
header, err := kcpSettings.GetPackerHeader()
if err != nil {
return nil, newError("failed to create packet header").Base(err).AtError()
return nil, errors.New("failed to create packet header").Base(err).AtError()
}
security, err := kcpSettings.GetSecurity()
if err != nil {
return nil, newError("failed to create security").Base(err).AtError()
return nil, errors.New("failed to create security").Base(err).AtError()
}
l := &Listener{
header: header,
@ -67,7 +68,7 @@ func NewListener(ctx context.Context, address net.Address, port net.Port, stream
l.Lock()
l.hub = hub
l.Unlock()
newError("listening on ", address, ":", port).WriteToLog()
errors.LogInfo(ctx, "listening on ", address, ":", port)
go l.handlePackets()
@ -86,7 +87,7 @@ func (l *Listener) OnReceive(payload *buf.Buffer, src net.Destination) {
payload.Release()
if len(segments) == 0 {
newError("discarding invalid payload from ", src).WriteToLog()
errors.LogInfo(context.Background(), "discarding invalid payload from ", src)
return
}