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

@ -14,8 +14,8 @@ 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/common/session"
"github.com/xtls/xray-core/common/signal/done"
"github.com/xtls/xray-core/common/signal/semaphore"
"github.com/xtls/xray-core/common/uuid"
@ -136,7 +136,7 @@ func init() {
}
func Dial(ctx context.Context, dest net.Destination, streamSettings *internet.MemoryStreamConfig) (stat.Connection, error) {
newError("dialing splithttp to ", dest).WriteToLog(session.ExportIDToError(ctx))
errors.LogInfo(ctx, "dialing splithttp to ", dest)
var requestURL url.URL
@ -191,7 +191,7 @@ func Dial(ctx context.Context, dest net.Destination, streamSettings *internet.Me
nil,
)
if err != nil {
newError("failed to construct download http request").Base(err).WriteToLog()
errors.LogInfoInner(ctx, err, "failed to construct download http request")
gotDownResponse.Close()
return
}
@ -201,14 +201,14 @@ func Dial(ctx context.Context, dest net.Destination, streamSettings *internet.Me
response, err := httpClient.download.Do(req)
gotConn.Close()
if err != nil {
newError("failed to send download http request").Base(err).WriteToLog()
errors.LogInfoInner(ctx, err, "failed to send download http request")
gotDownResponse.Close()
return
}
if response.StatusCode != 200 {
response.Body.Close()
newError("invalid status code on download:", response.Status).WriteToLog()
errors.LogInfo(ctx, "invalid status code on download:", response.Status)
gotDownResponse.Close()
return
}
@ -219,7 +219,7 @@ func Dial(ctx context.Context, dest net.Destination, streamSettings *internet.Me
_, err = io.ReadFull(response.Body, trashHeader)
if err != nil {
response.Body.Close()
newError("failed to read initial response").Base(err).WriteToLog()
errors.LogInfoInner(ctx, err, "failed to read initial response")
gotDownResponse.Close()
return
}
@ -258,7 +258,7 @@ func Dial(ctx context.Context, dest net.Destination, streamSettings *internet.Me
defer requestsLimiter.Signal()
req, err := http.NewRequest("POST", url, &buf.MultiBufferContainer{MultiBuffer: chunk})
if err != nil {
newError("failed to send upload").Base(err).WriteToLog()
errors.LogInfoInner(ctx, err, "failed to send upload")
uploadPipeReader.Interrupt()
return
}
@ -268,14 +268,14 @@ func Dial(ctx context.Context, dest net.Destination, streamSettings *internet.Me
if httpClient.isH2 {
resp, err := httpClient.upload.Do(req)
if err != nil {
newError("failed to send upload").Base(err).WriteToLog()
errors.LogInfoInner(ctx, err, "failed to send upload")
uploadPipeReader.Interrupt()
return
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
newError("failed to send upload, bad status code:", resp.Status).WriteToLog()
errors.LogInfo(ctx, "failed to send upload, bad status code:", resp.Status)
uploadPipeReader.Interrupt()
return
}
@ -287,7 +287,7 @@ func Dial(ctx context.Context, dest net.Destination, streamSettings *internet.Me
if uploadConn == nil {
uploadConn, err = httpClient.dialUploadConn(context.WithoutCancel(ctx))
if err != nil {
newError("failed to connect upload").Base(err).WriteToLog()
errors.LogInfoInner(ctx, err, "failed to connect upload")
uploadPipeReader.Interrupt()
return
}
@ -300,7 +300,7 @@ func Dial(ctx context.Context, dest net.Destination, streamSettings *internet.Me
}
if err != nil {
newError("failed to send upload").Base(err).WriteToLog()
errors.LogInfoInner(ctx, err, "failed to send upload")
uploadPipeReader.Interrupt()
return
}
@ -324,7 +324,7 @@ func Dial(ctx context.Context, dest net.Destination, streamSettings *internet.Me
CreateReader: func() (io.ReadCloser, error) {
<-gotDownResponse.Wait()
if downResponse == nil {
return nil, newError("downResponse failed")
return nil, errors.New("downResponse failed")
}
return downResponse, nil
},

View file

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

@ -12,9 +12,9 @@ import (
"time"
"github.com/xtls/xray-core/common"
"github.com/xtls/xray-core/common/errors"
"github.com/xtls/xray-core/common/net"
http_proto "github.com/xtls/xray-core/common/protocol/http"
"github.com/xtls/xray-core/common/session"
"github.com/xtls/xray-core/common/signal/done"
"github.com/xtls/xray-core/transport/internet"
"github.com/xtls/xray-core/transport/internet/stat"
@ -73,13 +73,13 @@ func (h *requestHandler) upsertSession(sessionId string) *httpSession {
func (h *requestHandler) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
if len(h.host) > 0 && request.Host != h.host {
newError("failed to validate host, request:", request.Host, ", config:", h.host).WriteToLog()
errors.LogInfo(context.Background(), "failed to validate host, request:", request.Host, ", config:", h.host)
writer.WriteHeader(http.StatusNotFound)
return
}
if !strings.HasPrefix(request.URL.Path, h.path) {
newError("failed to validate path, request:", request.URL.Path, ", config:", h.path).WriteToLog()
errors.LogInfo(context.Background(), "failed to validate path, request:", request.URL.Path, ", config:", h.path)
writer.WriteHeader(http.StatusNotFound)
return
}
@ -91,7 +91,7 @@ func (h *requestHandler) ServeHTTP(writer http.ResponseWriter, request *http.Req
}
if sessionId == "" {
newError("no sessionid on request:", request.URL.Path).WriteToLog()
errors.LogInfo(context.Background(), "no sessionid on request:", request.URL.Path)
writer.WriteHeader(http.StatusBadRequest)
return
}
@ -117,21 +117,21 @@ func (h *requestHandler) ServeHTTP(writer http.ResponseWriter, request *http.Req
}
if seq == "" {
newError("no seq on request:", request.URL.Path).WriteToLog()
errors.LogInfo(context.Background(), "no seq on request:", request.URL.Path)
writer.WriteHeader(http.StatusBadRequest)
return
}
payload, err := io.ReadAll(request.Body)
if err != nil {
newError("failed to upload").Base(err).WriteToLog()
errors.LogInfoInner(context.Background(), err, "failed to upload")
writer.WriteHeader(http.StatusInternalServerError)
return
}
seqInt, err := strconv.ParseUint(seq, 10, 64)
if err != nil {
newError("failed to upload").Base(err).WriteToLog()
errors.LogInfoInner(context.Background(), err, "failed to upload")
writer.WriteHeader(http.StatusInternalServerError)
return
}
@ -142,7 +142,7 @@ func (h *requestHandler) ServeHTTP(writer http.ResponseWriter, request *http.Req
})
if err != nil {
newError("failed to upload").Base(err).WriteToLog()
errors.LogInfoInner(context.Background(), err, "failed to upload")
writer.WriteHeader(http.StatusInternalServerError)
return
}
@ -249,9 +249,9 @@ func ListenSH(ctx context.Context, address net.Address, port net.Port, streamSet
Net: "unix",
}, streamSettings.SocketSettings)
if err != nil {
return nil, newError("failed to listen unix domain socket(for SH) on ", address).Base(err)
return nil, errors.New("failed to listen unix domain socket(for SH) on ", address).Base(err)
}
newError("listening unix domain socket(for SH) on ", address).WriteToLog(session.ExportIDToError(ctx))
errors.LogInfo(ctx, "listening unix domain socket(for SH) on ", address)
} else { // tcp
localAddr = gonet.TCPAddr{
IP: address.IP(),
@ -262,9 +262,9 @@ func ListenSH(ctx context.Context, address net.Address, port net.Port, streamSet
Port: int(port),
}, streamSettings.SocketSettings)
if err != nil {
return nil, newError("failed to listen TCP(for SH) on ", address, ":", port).Base(err)
return nil, errors.New("failed to listen TCP(for SH) on ", address, ":", port).Base(err)
}
newError("listening TCP(for SH) on ", address, ":", port).WriteToLog(session.ExportIDToError(ctx))
errors.LogInfo(ctx, "listening TCP(for SH) on ", address, ":", port)
}
if config := v2tls.ConfigFromStreamSettings(streamSettings); config != nil {
@ -294,7 +294,7 @@ func ListenSH(ctx context.Context, address net.Address, port net.Port, streamSet
go func() {
if err := l.server.Serve(l.listener); err != nil {
newError("failed to serve http for splithttp").Base(err).AtWarning().WriteToLog(session.ExportIDToError(ctx))
errors.LogWarningInner(ctx, err, "failed to serve http for splithttp")
}
}()

View file

@ -3,6 +3,8 @@ package splithttp
import (
"io"
"sync"
"github.com/xtls/xray-core/common/errors"
)
type LazyReader struct {
@ -50,7 +52,7 @@ func (r *LazyReader) Close() error {
if r.reader != nil {
err = r.reader.Close()
r.reader = nil
r.readerError = newError("closed reader")
r.readerError = errors.New("closed reader")
}
return err

View file

@ -4,6 +4,7 @@ import (
"context"
"github.com/xtls/xray-core/common"
"github.com/xtls/xray-core/common/errors"
)
//go:generate go run github.com/xtls/xray-core/common/errors/errorgen
@ -12,6 +13,6 @@ const protocolName = "splithttp"
func init() {
common.Must(common.RegisterConfig((*Config)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
return nil, newError("splithttp is a transport protocol.")
return nil, errors.New("splithttp is a transport protocol.")
}))
}

View file

@ -6,6 +6,8 @@ package splithttp
import (
"container/heap"
"io"
"github.com/xtls/xray-core/common/errors"
)
type Packet struct {
@ -33,7 +35,7 @@ func NewUploadQueue(maxPackets int) *UploadQueue {
func (h *UploadQueue) Push(p Packet) error {
if h.closed {
return newError("splithttp packet queue closed")
return errors.New("splithttp packet queue closed")
}
h.pushedPackets <- p
@ -84,7 +86,7 @@ func (h *UploadQueue) Read(b []byte) (int, error) {
// the "reassembly buffer" is too large, and we want to
// constrain memory usage somehow. let's tear down the
// connection, and hope the application retries.
return 0, newError("packet queue is too large")
return 0, errors.New("packet queue is too large")
}
heap.Push(&h.heap, packet)
packet2, more := <-h.pushedPackets