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

@ -4,6 +4,7 @@ import (
"crypto/aes"
"crypto/cipher"
"crypto/sha256"
"errors"
"github.com/xtls/xray-core/common"
"github.com/xtls/xray-core/common/protocol"
@ -30,7 +31,7 @@ func getAuth(config *Config) (cipher.AEAD, error) {
return chacha20poly1305.New(key[:])
}
return nil, newError("unsupported security type")
return nil, errors.New("unsupported security type")
}
func getHeader(config *Config) (internet.PacketHeader, error) {

View file

@ -9,6 +9,7 @@ import (
"github.com/quic-go/quic-go/logging"
"github.com/quic-go/quic-go/qlog"
"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/task"
"github.com/xtls/xray-core/transport/internet"
@ -21,7 +22,7 @@ type connectionContext struct {
conn quic.Connection
}
var errConnectionClosed = newError("connection closed")
var errConnectionClosed = errors.New("connection closed")
func (c *connectionContext) openStream(destAddr net.Addr) (*interConn, error) {
if !isActive(c.conn) {
@ -65,17 +66,17 @@ func removeInactiveConnections(conns []*connectionContext) []*connectionContext
continue
}
newError("closing quic connection at index: ", i).WriteToLog()
errors.LogInfo(context.Background(), "closing quic connection at index: ", i)
if err := s.conn.CloseWithError(0, ""); err != nil {
newError("failed to close connection").Base(err).WriteToLog()
errors.LogInfoInner(context.Background(), err, "failed to close connection")
}
if err := s.rawConn.Close(); err != nil {
newError("failed to close raw connection").Base(err).WriteToLog()
errors.LogInfoInner(context.Background(), err, "failed to close raw connection")
}
}
if len(activeConnections) < len(conns) {
newError("active quic connection reduced from ", len(conns), " to ", len(activeConnections)).WriteToLog()
errors.LogInfo(context.Background(), "active quic connection reduced from ", len(conns), " to ", len(activeConnections))
return activeConnections
}
@ -125,17 +126,17 @@ func (s *clientConnections) openConnection(ctx context.Context, destAddr net.Add
if err == nil {
return conn, nil
}
newError("failed to openStream: ").Base(err).WriteToLog()
errors.LogInfoInner(ctx, err, "failed to openStream: ")
} else {
newError("current quic connection is not active!").WriteToLog()
errors.LogInfo(ctx, "current quic connection is not active!")
}
}
conns = removeInactiveConnections(conns)
newError("dialing quic to ", dest).WriteToLog()
errors.LogInfo(ctx, "dialing quic to ", dest)
rawConn, err := internet.DialSystem(ctx, dest, sockopt)
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)
}
quicConfig := &quic.Config{
@ -156,7 +157,7 @@ func (s *clientConnections) openConnection(ctx context.Context, destAddr net.Add
default:
// TODO: Support sockopt for QUIC
rawConn.Close()
return nil, newError("QUIC with sockopt is unsupported").AtWarning()
return nil, errors.New("QUIC with sockopt is unsupported").AtWarning()
}
sysConn, err := wrapSysConn(udpConn, config)
@ -208,14 +209,14 @@ func Dial(ctx context.Context, dest net.Destination, streamSettings *internet.Me
IP: dest.Address.IP(),
Port: int(dest.Port),
}
} else {
} else {
dialerIp := internet.DestIpAddress()
if dialerIp != nil {
destAddr = &net.UDPAddr{
IP: dialerIp,
Port: int(dest.Port),
}
newError("quic Dial use dialer dest addr: ", destAddr).WriteToLog()
errors.LogInfo(ctx, "quic Dial use dialer dest addr: ", destAddr)
} else {
addr, err := net.ResolveUDPAddr("udp", dest.NetAddr())
if err != nil {

View file

@ -1,9 +0,0 @@
package quic
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/quic-go/quic-go/logging"
"github.com/quic-go/quic-go/qlog"
"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/protocol/tls/cert"
"github.com/xtls/xray-core/common/signal/done"
@ -27,13 +28,13 @@ func (l *Listener) acceptStreams(conn quic.Connection) {
for {
stream, err := conn.AcceptStream(context.Background())
if err != nil {
newError("failed to accept stream").Base(err).WriteToLog()
errors.LogInfoInner(context.Background(), err, "failed to accept stream")
select {
case <-conn.Context().Done():
return
case <-l.done.Wait():
if err := conn.CloseWithError(0, ""); err != nil {
newError("failed to close connection").Base(err).WriteToLog()
errors.LogInfoInner(context.Background(), err, "failed to close connection")
}
return
default:
@ -56,7 +57,7 @@ func (l *Listener) keepAccepting() {
for {
conn, err := l.listener.Accept(context.Background())
if err != nil {
newError("failed to accept QUIC connection").Base(err).WriteToLog()
errors.LogInfoInner(context.Background(), err, "failed to accept QUIC connection")
if l.done.Done() {
break
}
@ -83,7 +84,7 @@ func (l *Listener) Close() error {
// Listen creates a new Listener based on configurations.
func Listen(ctx context.Context, address net.Address, port net.Port, streamSettings *internet.MemoryStreamConfig, handler internet.ConnHandler) (internet.Listener, error) {
if address.Family().IsDomain() {
return nil, newError("domain address is not allows for listening quic")
return nil, errors.New("domain address is not allows for listening quic")
}
tlsConfig := tls.ConfigFromStreamSettings(streamSettings)