mirror of
https://github.com/XTLS/Xray-core.git
synced 2025-04-30 09:18:34 +00:00
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:
parent
8320732743
commit
079d0bd8a9
291 changed files with 1837 additions and 2368 deletions
|
@ -37,7 +37,7 @@ func (m *ClientManager) Dispatch(ctx context.Context, link *transport.Link) erro
|
|||
}
|
||||
}
|
||||
|
||||
return newError("unable to find an available mux client").AtWarning()
|
||||
return errors.New("unable to find an available mux client").AtWarning()
|
||||
}
|
||||
|
||||
type WorkerPicker interface {
|
||||
|
@ -57,7 +57,7 @@ func (p *IncrementalWorkerPicker) cleanupFunc() error {
|
|||
defer p.access.Unlock()
|
||||
|
||||
if len(p.workers) == 0 {
|
||||
return newError("no worker")
|
||||
return errors.New("no worker")
|
||||
}
|
||||
|
||||
p.cleanup()
|
||||
|
@ -155,7 +155,7 @@ func (f *DialingWorkerFactory) Create() (*ClientWorker, error) {
|
|||
ctx, cancel := context.WithCancel(ctx)
|
||||
|
||||
if err := p.Process(ctx, &transport.Link{Reader: uplinkReader, Writer: downlinkWriter}, d); err != nil {
|
||||
errors.New("failed to handler mux client connection").Base(err).WriteToLog()
|
||||
errors.LogInfoInner(ctx, err, "failed to handler mux client connection")
|
||||
}
|
||||
common.Must(c.Close())
|
||||
cancel()
|
||||
|
@ -244,7 +244,7 @@ func writeFirstPayload(reader buf.Reader, writer *Writer) error {
|
|||
|
||||
func fetchInput(ctx context.Context, s *Session, output buf.Writer) {
|
||||
outbounds := session.OutboundsFromContext(ctx)
|
||||
ob := outbounds[len(outbounds) - 1]
|
||||
ob := outbounds[len(outbounds)-1]
|
||||
transferType := protocol.TransferTypeStream
|
||||
if ob.Target.Network == net.Network_UDP {
|
||||
transferType = protocol.TransferTypePacket
|
||||
|
@ -254,15 +254,15 @@ func fetchInput(ctx context.Context, s *Session, output buf.Writer) {
|
|||
defer s.Close(false)
|
||||
defer writer.Close()
|
||||
|
||||
newError("dispatching request to ", ob.Target).WriteToLog(session.ExportIDToError(ctx))
|
||||
errors.LogInfo(ctx, "dispatching request to ", ob.Target)
|
||||
if err := writeFirstPayload(s.input, writer); err != nil {
|
||||
newError("failed to write first payload").Base(err).WriteToLog(session.ExportIDToError(ctx))
|
||||
errors.LogInfoInner(ctx, err, "failed to write first payload")
|
||||
writer.hasError = true
|
||||
return
|
||||
}
|
||||
|
||||
if err := buf.Copy(s.input, writer); err != nil {
|
||||
newError("failed to fetch all input").Base(err).WriteToLog(session.ExportIDToError(ctx))
|
||||
errors.LogInfoInner(ctx, err, "failed to fetch all input")
|
||||
writer.hasError = true
|
||||
return
|
||||
}
|
||||
|
@ -335,7 +335,7 @@ func (m *ClientWorker) handleStatusKeep(meta *FrameMetadata, reader *buf.Buffere
|
|||
rr := s.NewReader(reader, &meta.Target)
|
||||
err := buf.Copy(rr, s.output)
|
||||
if err != nil && buf.IsWriteError(err) {
|
||||
newError("failed to write to downstream. closing session ", s.ID).Base(err).WriteToLog()
|
||||
errors.LogInfoInner(context.Background(), err, "failed to write to downstream. closing session ", s.ID)
|
||||
s.Close(false)
|
||||
return buf.Copy(rr, buf.Discard)
|
||||
}
|
||||
|
@ -365,7 +365,7 @@ func (m *ClientWorker) fetchOutput() {
|
|||
err := meta.Unmarshal(reader)
|
||||
if err != nil {
|
||||
if errors.Cause(err) != io.EOF {
|
||||
newError("failed to read metadata").Base(err).WriteToLog()
|
||||
errors.LogInfoInner(context.Background(), err, "failed to read metadata")
|
||||
}
|
||||
break
|
||||
}
|
||||
|
@ -381,12 +381,12 @@ func (m *ClientWorker) fetchOutput() {
|
|||
err = m.handleStatusKeep(&meta, reader)
|
||||
default:
|
||||
status := meta.SessionStatus
|
||||
newError("unknown status: ", status).AtError().WriteToLog()
|
||||
errors.LogError(context.Background(), "unknown status: ", status)
|
||||
return
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
newError("failed to process data").Base(err).WriteToLog()
|
||||
errors.LogInfoInner(context.Background(), err, "failed to process data")
|
||||
return
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,9 +0,0 @@
|
|||
package mux
|
||||
|
||||
import "github.com/xtls/xray-core/common/errors"
|
||||
|
||||
type errPathObjHolder struct{}
|
||||
|
||||
func newError(values ...interface{}) *errors.Error {
|
||||
return errors.New(values...).WithPathObj(errPathObjHolder{})
|
||||
}
|
|
@ -7,6 +7,7 @@ import (
|
|||
"github.com/xtls/xray-core/common"
|
||||
"github.com/xtls/xray-core/common/bitmask"
|
||||
"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/protocol"
|
||||
"github.com/xtls/xray-core/common/serial"
|
||||
|
@ -102,7 +103,7 @@ func (f *FrameMetadata) Unmarshal(reader io.Reader) error {
|
|||
return err
|
||||
}
|
||||
if metaLen > 512 {
|
||||
return newError("invalid metalen ", metaLen).AtError()
|
||||
return errors.New("invalid metalen ", metaLen).AtError()
|
||||
}
|
||||
|
||||
b := buf.New()
|
||||
|
@ -118,7 +119,7 @@ func (f *FrameMetadata) Unmarshal(reader io.Reader) error {
|
|||
// Visible for testing only.
|
||||
func (f *FrameMetadata) UnmarshalFromBuffer(b *buf.Buffer) error {
|
||||
if b.Len() < 4 {
|
||||
return newError("insufficient buffer: ", b.Len())
|
||||
return errors.New("insufficient buffer: ", b.Len())
|
||||
}
|
||||
|
||||
f.SessionID = binary.BigEndian.Uint16(b.BytesTo(2))
|
||||
|
@ -129,14 +130,14 @@ func (f *FrameMetadata) UnmarshalFromBuffer(b *buf.Buffer) error {
|
|||
if f.SessionStatus == SessionStatusNew || (f.SessionStatus == SessionStatusKeep && b.Len() > 4 &&
|
||||
TargetNetwork(b.Byte(4)) == TargetNetworkUDP) { // MUST check the flag first
|
||||
if b.Len() < 8 {
|
||||
return newError("insufficient buffer: ", b.Len())
|
||||
return errors.New("insufficient buffer: ", b.Len())
|
||||
}
|
||||
network := TargetNetwork(b.Byte(4))
|
||||
b.Advance(5)
|
||||
|
||||
addr, port, err := addrParser.ReadAddressPort(nil, b)
|
||||
if err != nil {
|
||||
return newError("failed to parse address and port").Base(err)
|
||||
return errors.New("failed to parse address and port").Base(err)
|
||||
}
|
||||
|
||||
switch network {
|
||||
|
@ -145,7 +146,7 @@ func (f *FrameMetadata) UnmarshalFromBuffer(b *buf.Buffer) error {
|
|||
case TargetNetworkUDP:
|
||||
f.Target = net.UDPDestination(addr, port)
|
||||
default:
|
||||
return newError("unknown network type: ", network)
|
||||
return errors.New("unknown network type: ", network)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -5,6 +5,7 @@ import (
|
|||
|
||||
"github.com/xtls/xray-core/common/buf"
|
||||
"github.com/xtls/xray-core/common/crypto"
|
||||
"github.com/xtls/xray-core/common/errors"
|
||||
"github.com/xtls/xray-core/common/net"
|
||||
"github.com/xtls/xray-core/common/serial"
|
||||
)
|
||||
|
@ -37,7 +38,7 @@ func (r *PacketReader) ReadMultiBuffer() (buf.MultiBuffer, error) {
|
|||
}
|
||||
|
||||
if size > buf.Size {
|
||||
return nil, newError("packet size too large: ", size)
|
||||
return nil, errors.New("packet size too large: ", size)
|
||||
}
|
||||
|
||||
b := buf.New()
|
||||
|
|
|
@ -94,7 +94,7 @@ func NewServerWorker(ctx context.Context, d routing.Dispatcher, link *transport.
|
|||
func handle(ctx context.Context, s *Session, output buf.Writer) {
|
||||
writer := NewResponseWriter(s.ID, output, s.transferType)
|
||||
if err := buf.Copy(s.input, writer); err != nil {
|
||||
newError("session ", s.ID, " ends.").Base(err).WriteToLog(session.ExportIDToError(ctx))
|
||||
errors.LogInfoInner(ctx, err, "session ", s.ID, " ends.")
|
||||
writer.hasError = true
|
||||
}
|
||||
|
||||
|
@ -118,7 +118,7 @@ func (w *ServerWorker) handleStatusKeepAlive(meta *FrameMetadata, reader *buf.Bu
|
|||
}
|
||||
|
||||
func (w *ServerWorker) handleStatusNew(ctx context.Context, meta *FrameMetadata, reader *buf.BufferedReader) error {
|
||||
newError("received request for ", meta.Target).WriteToLog(session.ExportIDToError(ctx))
|
||||
errors.LogInfo(ctx, "received request for ", meta.Target)
|
||||
{
|
||||
msg := &log.AccessMessage{
|
||||
To: meta.Target,
|
||||
|
@ -134,7 +134,7 @@ func (w *ServerWorker) handleStatusNew(ctx context.Context, meta *FrameMetadata,
|
|||
|
||||
if network := session.AllowedNetworkFromContext(ctx); network != net.Network_Unknown {
|
||||
if meta.Target.Network != network {
|
||||
return newError("unexpected network ", meta.Target.Network) // it will break the whole Mux connection
|
||||
return errors.New("unexpected network ", meta.Target.Network) // it will break the whole Mux connection
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -152,7 +152,7 @@ func (w *ServerWorker) handleStatusNew(ctx context.Context, meta *FrameMetadata,
|
|||
} else {
|
||||
if x.Status == Initializing { // nearly impossible
|
||||
XUDPManager.Unlock()
|
||||
newError("XUDP hit ", meta.GlobalID).Base(errors.New("conflict")).AtWarning().WriteToLog(session.ExportIDToError(ctx))
|
||||
errors.LogWarningInner(ctx, errors.New("conflict"), "XUDP hit ", meta.GlobalID)
|
||||
// It's not a good idea to return an err here, so just let client wait.
|
||||
// Client will receive an End frame after sending a Keep frame.
|
||||
return nil
|
||||
|
@ -170,7 +170,7 @@ func (w *ServerWorker) handleStatusNew(ctx context.Context, meta *FrameMetadata,
|
|||
b.Release()
|
||||
mb = nil
|
||||
}
|
||||
newError("XUDP hit ", meta.GlobalID).Base(err).WriteToLog(session.ExportIDToError(ctx))
|
||||
errors.LogInfoInner(ctx, err,"XUDP hit ", meta.GlobalID)
|
||||
}
|
||||
if mb != nil {
|
||||
ctx = session.ContextWithTimeoutOnly(ctx, true)
|
||||
|
@ -180,7 +180,7 @@ func (w *ServerWorker) handleStatusNew(ctx context.Context, meta *FrameMetadata,
|
|||
XUDPManager.Lock()
|
||||
delete(XUDPManager.Map, x.GlobalID)
|
||||
XUDPManager.Unlock()
|
||||
err = newError("XUDP new ", meta.GlobalID).Base(errors.New("failed to dispatch request to ", meta.Target).Base(err))
|
||||
err = errors.New("XUDP new ", meta.GlobalID).Base(errors.New("failed to dispatch request to ", meta.Target).Base(err))
|
||||
return err // it will break the whole Mux connection
|
||||
}
|
||||
link.Writer.WriteMultiBuffer(mb) // it's meaningless to test a new pipe
|
||||
|
@ -188,7 +188,7 @@ func (w *ServerWorker) handleStatusNew(ctx context.Context, meta *FrameMetadata,
|
|||
input: link.Reader,
|
||||
output: link.Writer,
|
||||
}
|
||||
newError("XUDP new ", meta.GlobalID).Base(err).WriteToLog(session.ExportIDToError(ctx))
|
||||
errors.LogInfoInner(ctx, err, "XUDP new ", meta.GlobalID)
|
||||
}
|
||||
x.Mux = &Session{
|
||||
input: x.Mux.input,
|
||||
|
@ -211,7 +211,7 @@ func (w *ServerWorker) handleStatusNew(ctx context.Context, meta *FrameMetadata,
|
|||
if meta.Option.Has(OptionData) {
|
||||
buf.Copy(NewStreamReader(reader), buf.Discard)
|
||||
}
|
||||
return newError("failed to dispatch request.").Base(err)
|
||||
return errors.New("failed to dispatch request.").Base(err)
|
||||
}
|
||||
s := &Session{
|
||||
input: link.Reader,
|
||||
|
@ -255,7 +255,7 @@ func (w *ServerWorker) handleStatusKeep(meta *FrameMetadata, reader *buf.Buffere
|
|||
err := buf.Copy(rr, s.output)
|
||||
|
||||
if err != nil && buf.IsWriteError(err) {
|
||||
newError("failed to write to downstream writer. closing session ", s.ID).Base(err).WriteToLog()
|
||||
errors.LogInfoInner(context.Background(), err, "failed to write to downstream writer. closing session ", s.ID)
|
||||
s.Close(false)
|
||||
return buf.Copy(rr, buf.Discard)
|
||||
}
|
||||
|
@ -277,7 +277,7 @@ func (w *ServerWorker) handleFrame(ctx context.Context, reader *buf.BufferedRead
|
|||
var meta FrameMetadata
|
||||
err := meta.Unmarshal(reader)
|
||||
if err != nil {
|
||||
return newError("failed to read metadata").Base(err)
|
||||
return errors.New("failed to read metadata").Base(err)
|
||||
}
|
||||
|
||||
switch meta.SessionStatus {
|
||||
|
@ -291,11 +291,11 @@ func (w *ServerWorker) handleFrame(ctx context.Context, reader *buf.BufferedRead
|
|||
err = w.handleStatusKeep(&meta, reader)
|
||||
default:
|
||||
status := meta.SessionStatus
|
||||
return newError("unknown status: ", status).AtError()
|
||||
return errors.New("unknown status: ", status).AtError()
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return newError("failed to process data").Base(err)
|
||||
return errors.New("failed to process data").Base(err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
@ -314,7 +314,7 @@ func (w *ServerWorker) run(ctx context.Context) {
|
|||
err := w.handleFrame(ctx, reader)
|
||||
if err != nil {
|
||||
if errors.Cause(err) != io.EOF {
|
||||
newError("unexpected EOF").Base(err).WriteToLog(session.ExportIDToError(ctx))
|
||||
errors.LogInfoInner(ctx, err, "unexpected EOF")
|
||||
common.Interrupt(input)
|
||||
}
|
||||
return
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package mux
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
"runtime"
|
||||
"sync"
|
||||
|
@ -8,6 +9,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/common/protocol"
|
||||
"github.com/xtls/xray-core/transport/pipe"
|
||||
|
@ -180,7 +182,7 @@ func (s *Session) Close(locked bool) error {
|
|||
if s.XUDP.Status == Active {
|
||||
s.XUDP.Expire = time.Now().Add(time.Minute)
|
||||
s.XUDP.Status = Expiring
|
||||
newError("XUDP put ", s.XUDP.GlobalID).AtDebug().WriteToLog()
|
||||
errors.LogDebug(context.Background(), "XUDP put ", s.XUDP.GlobalID)
|
||||
}
|
||||
XUDPManager.Unlock()
|
||||
}
|
||||
|
@ -230,7 +232,7 @@ func init() {
|
|||
if x.Status == Expiring && now.After(x.Expire) {
|
||||
x.Interrupt()
|
||||
delete(XUDPManager.Map, id)
|
||||
newError("XUDP del ", id).AtDebug().WriteToLog()
|
||||
errors.LogDebug(context.Background(), "XUDP del ", id)
|
||||
}
|
||||
}
|
||||
XUDPManager.Unlock()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue