Skip to content

Commit

Permalink
Refactor new tcpip package
Browse files Browse the repository at this point in the history
  • Loading branch information
nekohasekai committed Jul 14, 2023
1 parent 7c9b4d6 commit 97761c4
Show file tree
Hide file tree
Showing 24 changed files with 1,299 additions and 53 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/.idea/
/vendor/
.DS_Store
32 changes: 32 additions & 0 deletions common/log/context.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package log

import (
"context"
"math/rand"
"time"

"github.com/sagernet/sing/common/random"
)

func init() {
random.InitializeSeed()
}

type contextIDKey struct{}

type ContextID struct {
ID uint32
CreatedAt time.Time
}

func ContextWithNewID(ctx context.Context) context.Context {
return context.WithValue(ctx, (*contextIDKey)(nil), ContextID{
ID: rand.Uint32(),
CreatedAt: time.Now(),
})
}

func IDFromContext(ctx context.Context) (ContextID, bool) {
id, loaded := ctx.Value((*contextIDKey)(nil)).(ContextID)
return id, loaded
}
23 changes: 23 additions & 0 deletions common/log/default.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package log

import (
"io"
"time"

"github.com/sagernet/sing/common/log/internal/default"
)

var _ = default_logger.Options(DefaultOptions{})

type DefaultOptions struct {
Level Level
Writer io.Writer
BaseTime time.Time
PlatformWriter io.Writer
DisableColor bool
Timestamp bool
}

func NewDefault(options DefaultOptions) Logger {
return default_logger.New(default_logger.Options(options))
}
81 changes: 81 additions & 0 deletions common/log/export.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package log

import (
"context"
"os"
"time"
)

var defaultLogger Logger

func init() {
defaultLogger = NewDefault(DefaultOptions{
Level: LevelDebug,
Writer: os.Stderr,
BaseTime: time.Now(),
})
}

func Default() Logger {
return defaultLogger
}

func SetDefault(logger Logger) {
defaultLogger = logger
}

func Trace(args ...any) {
defaultLogger.Trace(args...)
}

func Debug(args ...any) {
defaultLogger.Debug(args...)
}

func Info(args ...any) {
defaultLogger.Info(args...)
}

func Warn(args ...any) {
defaultLogger.Warn(args...)
}

func Error(args ...any) {
defaultLogger.Error(args...)
}

func Fatal(args ...any) {
defaultLogger.Fatal(args...)
}

func Panic(args ...any) {
defaultLogger.Panic(args...)
}

func TraceContext(ctx context.Context, args ...any) {
defaultLogger.TraceContext(ctx, args...)
}

func DebugContext(ctx context.Context, args ...any) {
defaultLogger.DebugContext(ctx, args...)
}

func InfoContext(ctx context.Context, args ...any) {
defaultLogger.InfoContext(ctx, args...)
}

func WarnContext(ctx context.Context, args ...any) {
defaultLogger.WarnContext(ctx, args...)
}

func ErrorContext(ctx context.Context, args ...any) {
defaultLogger.ErrorContext(ctx, args...)
}

func FatalContext(ctx context.Context, args ...any) {
defaultLogger.FatalContext(ctx, args...)
}

func PanicContext(ctx context.Context, args ...any) {
defaultLogger.PanicContext(ctx, args...)
}
Loading

0 comments on commit 97761c4

Please sign in to comment.