Skip to content

Commit 97761c4

Browse files
committed
Refactor new tcpip package
1 parent 7c9b4d6 commit 97761c4

24 files changed

+1299
-53
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
/.idea/
22
/vendor/
3+
.DS_Store

common/log/context.go

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package log
2+
3+
import (
4+
"context"
5+
"math/rand"
6+
"time"
7+
8+
"github.com/sagernet/sing/common/random"
9+
)
10+
11+
func init() {
12+
random.InitializeSeed()
13+
}
14+
15+
type contextIDKey struct{}
16+
17+
type ContextID struct {
18+
ID uint32
19+
CreatedAt time.Time
20+
}
21+
22+
func ContextWithNewID(ctx context.Context) context.Context {
23+
return context.WithValue(ctx, (*contextIDKey)(nil), ContextID{
24+
ID: rand.Uint32(),
25+
CreatedAt: time.Now(),
26+
})
27+
}
28+
29+
func IDFromContext(ctx context.Context) (ContextID, bool) {
30+
id, loaded := ctx.Value((*contextIDKey)(nil)).(ContextID)
31+
return id, loaded
32+
}

common/log/default.go

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package log
2+
3+
import (
4+
"io"
5+
"time"
6+
7+
"github.com/sagernet/sing/common/log/internal/default"
8+
)
9+
10+
var _ = default_logger.Options(DefaultOptions{})
11+
12+
type DefaultOptions struct {
13+
Level Level
14+
Writer io.Writer
15+
BaseTime time.Time
16+
PlatformWriter io.Writer
17+
DisableColor bool
18+
Timestamp bool
19+
}
20+
21+
func NewDefault(options DefaultOptions) Logger {
22+
return default_logger.New(default_logger.Options(options))
23+
}

common/log/export.go

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package log
2+
3+
import (
4+
"context"
5+
"os"
6+
"time"
7+
)
8+
9+
var defaultLogger Logger
10+
11+
func init() {
12+
defaultLogger = NewDefault(DefaultOptions{
13+
Level: LevelDebug,
14+
Writer: os.Stderr,
15+
BaseTime: time.Now(),
16+
})
17+
}
18+
19+
func Default() Logger {
20+
return defaultLogger
21+
}
22+
23+
func SetDefault(logger Logger) {
24+
defaultLogger = logger
25+
}
26+
27+
func Trace(args ...any) {
28+
defaultLogger.Trace(args...)
29+
}
30+
31+
func Debug(args ...any) {
32+
defaultLogger.Debug(args...)
33+
}
34+
35+
func Info(args ...any) {
36+
defaultLogger.Info(args...)
37+
}
38+
39+
func Warn(args ...any) {
40+
defaultLogger.Warn(args...)
41+
}
42+
43+
func Error(args ...any) {
44+
defaultLogger.Error(args...)
45+
}
46+
47+
func Fatal(args ...any) {
48+
defaultLogger.Fatal(args...)
49+
}
50+
51+
func Panic(args ...any) {
52+
defaultLogger.Panic(args...)
53+
}
54+
55+
func TraceContext(ctx context.Context, args ...any) {
56+
defaultLogger.TraceContext(ctx, args...)
57+
}
58+
59+
func DebugContext(ctx context.Context, args ...any) {
60+
defaultLogger.DebugContext(ctx, args...)
61+
}
62+
63+
func InfoContext(ctx context.Context, args ...any) {
64+
defaultLogger.InfoContext(ctx, args...)
65+
}
66+
67+
func WarnContext(ctx context.Context, args ...any) {
68+
defaultLogger.WarnContext(ctx, args...)
69+
}
70+
71+
func ErrorContext(ctx context.Context, args ...any) {
72+
defaultLogger.ErrorContext(ctx, args...)
73+
}
74+
75+
func FatalContext(ctx context.Context, args ...any) {
76+
defaultLogger.FatalContext(ctx, args...)
77+
}
78+
79+
func PanicContext(ctx context.Context, args ...any) {
80+
defaultLogger.PanicContext(ctx, args...)
81+
}

0 commit comments

Comments
 (0)