-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7c9b4d6
commit 97761c4
Showing
24 changed files
with
1,299 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
/.idea/ | ||
/vendor/ | ||
.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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...) | ||
} |
Oops, something went wrong.