2025-09-20 00:35:50 -07:00
|
|
|
|
// Package logger provides logging functionality for the 3x-ui panel with
|
2025-10-09 08:39:29 -07:00
|
|
|
|
// dual-backend logging (console/syslog and file) and buffered log storage for web UI.
|
2023-02-09 11:18:06 -08:00
|
|
|
|
package logger
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
2023-07-31 09:41:47 -07:00
|
|
|
|
"fmt"
|
2023-02-09 11:18:06 -08:00
|
|
|
|
"os"
|
2025-10-09 08:39:29 -07:00
|
|
|
|
"path/filepath"
|
|
|
|
|
|
"runtime"
|
Test-quality audit: fix 2 prod bugs, strengthen weak tests, add mutation/fuzz/CI tooling (#5345)
* test(audit): add gremlins/rapid/coverage tooling + AUDIT.md scaffold
* test(audit): hygiene sweep (race-clean except logger global; Finding #2) + smell inventory
* test(audit): cover untested error/edge branches (TLS proxy+pin, migration tag cleanup=Finding #1)
* test(audit): strengthen internal/sub link tests (dedup key, TLS/Reality mapping, clash well-formedness)
* test(audit): property (rapid) + fuzz tests for joinHostPort/userinfo/pin/ParseLink
* test(audit): tighten frontend subSortIndex rejection assertions + wire coverage
* ci(audit): add shuffle gate + non-blocking race job (Finding #2) + fuzz-smoke; document mutation policy
* chore(audit): gitignore frontend coverage output
* test(audit): exhaustive whole-repo pass — strengthen 5 weak/fake tests (netproxy, CSP, modal per-protocol loops, schema coercions)
* docs(contributing): add Testing section (conventions, race/shuffle, fuzz, mutation policy); drop AUDIT.md ledger
* fix(logger,migration): guard logBuffer with mutex; execute legacy tag cleanup (tx.Exec); make CI race gate blocking
* ci(mutation): add nightly scoped gremlins workflow (informational artifacts)
* test(audit): strengthen runtime tests — baseURL scheme/port bounds, isNonEmptySlice, trafficReset
* test(audit): strengthen clash tests — reality field mapping + tcp-header validation
* test(audit): runtime — egress-proxy + content-type tests; drop redundant bp=='' branch
* test(audit): strengthen link parser/helper tests (defaultPort, splitComma, base64, canonicalQuery, tls/reality/transport mapping)
* test(audit): strengthen sub/xray/common/netsafe/mtproto/config/middleware tests (kill surviving mutants)
* test(audit): raise timeout on protocol-iteration modal tests (heavy re-renders, slow on CI)
* fix(logger): GetLogs returns at most c entries (off-by-one fix; addresses PR review)
* perf(logger): snapshot logBuffer under lock so GetLogs doesn't block logging; clarify fuzz-seed docs (addresses PR review)
2026-06-15 06:17:03 -07:00
|
|
|
|
"sync"
|
2023-07-31 09:41:47 -07:00
|
|
|
|
"time"
|
2023-04-27 13:45:06 -07:00
|
|
|
|
|
|
|
|
|
|
"github.com/op/go-logging"
|
2026-05-13 08:03:56 -07:00
|
|
|
|
|
style: adopt golangci-lint v2 and resolve all findings
Add .golangci.yml (v2): the standard linters plus bodyclose, errorlint, noctx, misspell, rowserrcheck, sqlclosecheck, unconvert, usestdlibvars, with gofumpt + goimports formatters. Enable the std-error-handling exclusion preset for idiomatic Close/Remove/Setenv ignores; scope-exclude SA1019 (parser.ParseDir in tools/openapigen) and ST1005 (intentional capitalized user-facing error copy that tests assert verbatim). No inline nolint directives were introduced.
Resolve all 217 findings behavior-preserving: gofumpt/goimports formatting, explicit blank assignment on intentionally ignored errors, errors.Is/errors.As and %w wrapping, context-aware stdlib calls (CommandContext/QueryContext/NewRequestWithContext/Dialer), staticcheck simplifications, removed redundant conversions, http.StatusOK and http.MethodGet, inlined the go:fix intPtr helper, and deferred sql rows Close. Add a golangci CI job mirroring the existing Go jobs.
2026-06-27 06:42:22 -07:00
|
|
|
|
"github.com/mhsanaei/3x-ui/v3/internal/config"
|
|
|
|
|
|
|
2026-05-13 08:03:56 -07:00
|
|
|
|
"gopkg.in/natefinch/lumberjack.v2"
|
2023-02-09 11:18:06 -08:00
|
|
|
|
)
|
|
|
|
|
|
|
2025-10-09 08:39:29 -07:00
|
|
|
|
const (
|
|
|
|
|
|
maxLogBufferSize = 10240 // Maximum log entries kept in memory
|
|
|
|
|
|
logFileName = "3xui.log" // Log file name
|
|
|
|
|
|
timeFormat = "2006/01/02 15:04:05" // Log timestamp format
|
2026-05-13 08:03:56 -07:00
|
|
|
|
|
|
|
|
|
|
// On-disk rotation limits — single file capped, old segments pruned automatically.
|
|
|
|
|
|
maxLogFileMB = 10 // rotate active log when larger than this
|
|
|
|
|
|
maxLogBackups = 5 // rotated files retained (beyond current segment)
|
|
|
|
|
|
maxLogAgeDays = 7 // remove rotated backups older than this (0 disables time-based pruning)
|
|
|
|
|
|
compressRotated = true
|
2025-10-09 08:39:29 -07:00
|
|
|
|
)
|
2025-09-20 00:35:50 -07:00
|
|
|
|
|
2025-10-09 08:39:29 -07:00
|
|
|
|
var (
|
2026-06-26 02:40:13 -07:00
|
|
|
|
// Initialized to a usable default so logging never nil-derefs before InitLogger
|
|
|
|
|
|
// runs — the "migrate" and "setting" CLI subcommands log without calling it.
|
|
|
|
|
|
logger = logging.MustGetLogger("x-ui")
|
2026-05-13 08:03:56 -07:00
|
|
|
|
fileRotate *lumberjack.Logger // nil when file backend disabled
|
2025-10-09 08:39:29 -07:00
|
|
|
|
|
Test-quality audit: fix 2 prod bugs, strengthen weak tests, add mutation/fuzz/CI tooling (#5345)
* test(audit): add gremlins/rapid/coverage tooling + AUDIT.md scaffold
* test(audit): hygiene sweep (race-clean except logger global; Finding #2) + smell inventory
* test(audit): cover untested error/edge branches (TLS proxy+pin, migration tag cleanup=Finding #1)
* test(audit): strengthen internal/sub link tests (dedup key, TLS/Reality mapping, clash well-formedness)
* test(audit): property (rapid) + fuzz tests for joinHostPort/userinfo/pin/ParseLink
* test(audit): tighten frontend subSortIndex rejection assertions + wire coverage
* ci(audit): add shuffle gate + non-blocking race job (Finding #2) + fuzz-smoke; document mutation policy
* chore(audit): gitignore frontend coverage output
* test(audit): exhaustive whole-repo pass — strengthen 5 weak/fake tests (netproxy, CSP, modal per-protocol loops, schema coercions)
* docs(contributing): add Testing section (conventions, race/shuffle, fuzz, mutation policy); drop AUDIT.md ledger
* fix(logger,migration): guard logBuffer with mutex; execute legacy tag cleanup (tx.Exec); make CI race gate blocking
* ci(mutation): add nightly scoped gremlins workflow (informational artifacts)
* test(audit): strengthen runtime tests — baseURL scheme/port bounds, isNonEmptySlice, trafficReset
* test(audit): strengthen clash tests — reality field mapping + tcp-header validation
* test(audit): runtime — egress-proxy + content-type tests; drop redundant bp=='' branch
* test(audit): strengthen link parser/helper tests (defaultPort, splitComma, base64, canonicalQuery, tls/reality/transport mapping)
* test(audit): strengthen sub/xray/common/netsafe/mtproto/config/middleware tests (kill surviving mutants)
* test(audit): raise timeout on protocol-iteration modal tests (heavy re-renders, slow on CI)
* fix(logger): GetLogs returns at most c entries (off-by-one fix; addresses PR review)
* perf(logger): snapshot logBuffer under lock so GetLogs doesn't block logging; clarify fuzz-seed docs (addresses PR review)
2026-06-15 06:17:03 -07:00
|
|
|
|
// logBuffer maintains recent log entries in memory for web UI retrieval;
|
|
|
|
|
|
// logBufferMu guards it — written from many goroutines, read by the web UI.
|
|
|
|
|
|
logBufferMu sync.Mutex
|
|
|
|
|
|
logBuffer []struct {
|
2024-03-10 14:31:24 -07:00
|
|
|
|
time string
|
|
|
|
|
|
level logging.Level
|
|
|
|
|
|
log string
|
|
|
|
|
|
}
|
|
|
|
|
|
)
|
2023-02-09 11:18:06 -08:00
|
|
|
|
|
2025-10-09 08:39:29 -07:00
|
|
|
|
// InitLogger initializes dual logging backends: console/syslog and file.
|
|
|
|
|
|
// Console logging uses the specified level, file logging always uses DEBUG level.
|
2023-02-09 11:18:06 -08:00
|
|
|
|
func InitLogger(level logging.Level) {
|
2023-07-31 09:41:47 -07:00
|
|
|
|
newLogger := logging.MustGetLogger("x-ui")
|
2025-10-09 08:39:29 -07:00
|
|
|
|
backends := make([]logging.Backend, 0, 2)
|
2023-05-22 16:13:15 -07:00
|
|
|
|
|
2025-10-09 08:39:29 -07:00
|
|
|
|
// Console/syslog backend with configurable level
|
2026-07-08 13:12:28 -07:00
|
|
|
|
consoleBackend := initDefaultBackend()
|
|
|
|
|
|
leveledBackend := logging.AddModuleLevel(consoleBackend)
|
|
|
|
|
|
leveledBackend.SetLevel(level, "x-ui")
|
|
|
|
|
|
backends = append(backends, leveledBackend)
|
2023-05-22 16:13:15 -07:00
|
|
|
|
|
2025-10-09 08:39:29 -07:00
|
|
|
|
// File backend with DEBUG level for comprehensive logging
|
|
|
|
|
|
if fileBackend := initFileBackend(); fileBackend != nil {
|
|
|
|
|
|
leveledBackend := logging.AddModuleLevel(fileBackend)
|
|
|
|
|
|
leveledBackend.SetLevel(logging.DEBUG, "x-ui")
|
|
|
|
|
|
backends = append(backends, leveledBackend)
|
|
|
|
|
|
}
|
2023-02-09 11:18:06 -08:00
|
|
|
|
|
2025-10-09 08:39:29 -07:00
|
|
|
|
multiBackend := logging.MultiLogger(backends...)
|
|
|
|
|
|
newLogger.SetBackend(multiBackend)
|
2023-02-09 11:18:06 -08:00
|
|
|
|
logger = newLogger
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-09 08:39:29 -07:00
|
|
|
|
// initDefaultBackend creates the console/syslog logging backend.
|
|
|
|
|
|
// Windows: Uses stderr directly (no syslog support)
|
|
|
|
|
|
// Unix-like: Attempts syslog, falls back to stderr
|
|
|
|
|
|
func initDefaultBackend() logging.Backend {
|
|
|
|
|
|
var backend logging.Backend
|
|
|
|
|
|
includeTime := false
|
|
|
|
|
|
|
|
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
|
|
// Windows: Use stderr directly (no syslog support)
|
|
|
|
|
|
backend = logging.NewLogBackend(os.Stderr, "", 0)
|
|
|
|
|
|
includeTime = true
|
|
|
|
|
|
} else {
|
|
|
|
|
|
// Unix-like: Try syslog, fallback to stderr
|
|
|
|
|
|
if syslogBackend, err := logging.NewSyslogBackend(""); err != nil {
|
|
|
|
|
|
fmt.Fprintf(os.Stderr, "syslog backend disabled: %v\n", err)
|
|
|
|
|
|
backend = logging.NewLogBackend(os.Stderr, "", 0)
|
|
|
|
|
|
includeTime = os.Getppid() > 0
|
|
|
|
|
|
} else {
|
|
|
|
|
|
backend = syslogBackend
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return logging.NewBackendFormatter(backend, newFormatter(includeTime))
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-05-13 08:03:56 -07:00
|
|
|
|
// initFileBackend creates the file logging backend with size/age‑bounded rotation
|
|
|
|
|
|
// so log volume cannot grow without limit on disk.
|
2025-10-09 08:39:29 -07:00
|
|
|
|
func initFileBackend() logging.Backend {
|
|
|
|
|
|
logDir := config.GetLogFolder()
|
|
|
|
|
|
if err := os.MkdirAll(logDir, 0o750); err != nil {
|
|
|
|
|
|
fmt.Fprintf(os.Stderr, "failed to create log folder %s: %v\n", logDir, err)
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
logPath := filepath.Join(logDir, logFileName)
|
2026-05-13 08:03:56 -07:00
|
|
|
|
fileRotate = &lumberjack.Logger{
|
|
|
|
|
|
Filename: logPath,
|
|
|
|
|
|
MaxSize: maxLogFileMB,
|
|
|
|
|
|
MaxBackups: maxLogBackups,
|
|
|
|
|
|
MaxAge: maxLogAgeDays,
|
|
|
|
|
|
LocalTime: true,
|
|
|
|
|
|
Compress: compressRotated,
|
2025-10-09 08:39:29 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-05-13 08:03:56 -07:00
|
|
|
|
backend := logging.NewLogBackend(fileRotate, "", 0)
|
2025-10-09 08:39:29 -07:00
|
|
|
|
return logging.NewBackendFormatter(backend, newFormatter(true))
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// newFormatter creates a log formatter with optional timestamp.
|
|
|
|
|
|
func newFormatter(withTime bool) logging.Formatter {
|
|
|
|
|
|
format := `%{level} - %{message}`
|
|
|
|
|
|
if withTime {
|
|
|
|
|
|
format = `%{time:` + timeFormat + `} %{level} - %{message}`
|
|
|
|
|
|
}
|
|
|
|
|
|
return logging.MustStringFormatter(format)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-05-13 08:03:56 -07:00
|
|
|
|
// CloseLogger closes the rotating log writer and cleans up resources.
|
2025-10-09 08:39:29 -07:00
|
|
|
|
// Should be called during application shutdown.
|
|
|
|
|
|
func CloseLogger() {
|
2026-05-13 08:03:56 -07:00
|
|
|
|
if fileRotate != nil {
|
|
|
|
|
|
_ = fileRotate.Close()
|
|
|
|
|
|
fileRotate = nil
|
2025-10-09 08:39:29 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-20 00:35:50 -07:00
|
|
|
|
// Debug logs a debug message and adds it to the log buffer.
|
2025-03-12 12:13:51 -07:00
|
|
|
|
func Debug(args ...any) {
|
2023-07-31 09:41:47 -07:00
|
|
|
|
logger.Debug(args...)
|
|
|
|
|
|
addToBuffer("DEBUG", fmt.Sprint(args...))
|
2023-02-09 11:18:06 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-20 00:35:50 -07:00
|
|
|
|
// Debugf logs a formatted debug message and adds it to the log buffer.
|
2025-03-12 12:13:51 -07:00
|
|
|
|
func Debugf(format string, args ...any) {
|
2023-07-31 09:41:47 -07:00
|
|
|
|
logger.Debugf(format, args...)
|
|
|
|
|
|
addToBuffer("DEBUG", fmt.Sprintf(format, args...))
|
2023-02-09 11:18:06 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-20 00:35:50 -07:00
|
|
|
|
// Info logs an info message and adds it to the log buffer.
|
2025-03-12 12:13:51 -07:00
|
|
|
|
func Info(args ...any) {
|
2023-07-31 09:41:47 -07:00
|
|
|
|
logger.Info(args...)
|
|
|
|
|
|
addToBuffer("INFO", fmt.Sprint(args...))
|
2023-02-09 11:18:06 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-20 00:35:50 -07:00
|
|
|
|
// Infof logs a formatted info message and adds it to the log buffer.
|
2025-03-12 12:13:51 -07:00
|
|
|
|
func Infof(format string, args ...any) {
|
2023-07-31 09:41:47 -07:00
|
|
|
|
logger.Infof(format, args...)
|
|
|
|
|
|
addToBuffer("INFO", fmt.Sprintf(format, args...))
|
2023-02-09 11:18:06 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-20 00:35:50 -07:00
|
|
|
|
// Notice logs a notice message and adds it to the log buffer.
|
2025-03-12 12:13:51 -07:00
|
|
|
|
func Notice(args ...any) {
|
2024-02-03 03:54:39 -08:00
|
|
|
|
logger.Notice(args...)
|
|
|
|
|
|
addToBuffer("NOTICE", fmt.Sprint(args...))
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-20 00:35:50 -07:00
|
|
|
|
// Noticef logs a formatted notice message and adds it to the log buffer.
|
2025-03-12 12:13:51 -07:00
|
|
|
|
func Noticef(format string, args ...any) {
|
2024-02-03 03:54:39 -08:00
|
|
|
|
logger.Noticef(format, args...)
|
|
|
|
|
|
addToBuffer("NOTICE", fmt.Sprintf(format, args...))
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-20 00:35:50 -07:00
|
|
|
|
// Warning logs a warning message and adds it to the log buffer.
|
2025-03-12 12:13:51 -07:00
|
|
|
|
func Warning(args ...any) {
|
2023-07-31 09:41:47 -07:00
|
|
|
|
logger.Warning(args...)
|
|
|
|
|
|
addToBuffer("WARNING", fmt.Sprint(args...))
|
2023-02-09 11:18:06 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-20 00:35:50 -07:00
|
|
|
|
// Warningf logs a formatted warning message and adds it to the log buffer.
|
2025-03-12 12:13:51 -07:00
|
|
|
|
func Warningf(format string, args ...any) {
|
2023-07-31 09:41:47 -07:00
|
|
|
|
logger.Warningf(format, args...)
|
|
|
|
|
|
addToBuffer("WARNING", fmt.Sprintf(format, args...))
|
2023-02-09 11:18:06 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-20 00:35:50 -07:00
|
|
|
|
// Error logs an error message and adds it to the log buffer.
|
2025-03-12 12:13:51 -07:00
|
|
|
|
func Error(args ...any) {
|
2023-07-31 09:41:47 -07:00
|
|
|
|
logger.Error(args...)
|
|
|
|
|
|
addToBuffer("ERROR", fmt.Sprint(args...))
|
2023-02-09 11:18:06 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-20 00:35:50 -07:00
|
|
|
|
// Errorf logs a formatted error message and adds it to the log buffer.
|
2025-03-12 12:13:51 -07:00
|
|
|
|
func Errorf(format string, args ...any) {
|
2023-07-31 09:41:47 -07:00
|
|
|
|
logger.Errorf(format, args...)
|
|
|
|
|
|
addToBuffer("ERROR", fmt.Sprintf(format, args...))
|
2023-02-09 11:18:06 -08:00
|
|
|
|
}
|
2023-06-16 07:55:33 -07:00
|
|
|
|
|
2025-10-09 08:39:29 -07:00
|
|
|
|
// addToBuffer adds a log entry to the in-memory ring buffer for web UI retrieval.
|
2023-07-31 09:41:47 -07:00
|
|
|
|
func addToBuffer(level string, newLog string) {
|
|
|
|
|
|
t := time.Now()
|
Test-quality audit: fix 2 prod bugs, strengthen weak tests, add mutation/fuzz/CI tooling (#5345)
* test(audit): add gremlins/rapid/coverage tooling + AUDIT.md scaffold
* test(audit): hygiene sweep (race-clean except logger global; Finding #2) + smell inventory
* test(audit): cover untested error/edge branches (TLS proxy+pin, migration tag cleanup=Finding #1)
* test(audit): strengthen internal/sub link tests (dedup key, TLS/Reality mapping, clash well-formedness)
* test(audit): property (rapid) + fuzz tests for joinHostPort/userinfo/pin/ParseLink
* test(audit): tighten frontend subSortIndex rejection assertions + wire coverage
* ci(audit): add shuffle gate + non-blocking race job (Finding #2) + fuzz-smoke; document mutation policy
* chore(audit): gitignore frontend coverage output
* test(audit): exhaustive whole-repo pass — strengthen 5 weak/fake tests (netproxy, CSP, modal per-protocol loops, schema coercions)
* docs(contributing): add Testing section (conventions, race/shuffle, fuzz, mutation policy); drop AUDIT.md ledger
* fix(logger,migration): guard logBuffer with mutex; execute legacy tag cleanup (tx.Exec); make CI race gate blocking
* ci(mutation): add nightly scoped gremlins workflow (informational artifacts)
* test(audit): strengthen runtime tests — baseURL scheme/port bounds, isNonEmptySlice, trafficReset
* test(audit): strengthen clash tests — reality field mapping + tcp-header validation
* test(audit): runtime — egress-proxy + content-type tests; drop redundant bp=='' branch
* test(audit): strengthen link parser/helper tests (defaultPort, splitComma, base64, canonicalQuery, tls/reality/transport mapping)
* test(audit): strengthen sub/xray/common/netsafe/mtproto/config/middleware tests (kill surviving mutants)
* test(audit): raise timeout on protocol-iteration modal tests (heavy re-renders, slow on CI)
* fix(logger): GetLogs returns at most c entries (off-by-one fix; addresses PR review)
* perf(logger): snapshot logBuffer under lock so GetLogs doesn't block logging; clarify fuzz-seed docs (addresses PR review)
2026-06-15 06:17:03 -07:00
|
|
|
|
logBufferMu.Lock()
|
|
|
|
|
|
defer logBufferMu.Unlock()
|
2025-10-09 08:39:29 -07:00
|
|
|
|
if len(logBuffer) >= maxLogBufferSize {
|
2023-07-31 09:41:47 -07:00
|
|
|
|
logBuffer = logBuffer[1:]
|
2023-06-16 07:55:33 -07:00
|
|
|
|
}
|
2023-07-31 09:41:47 -07:00
|
|
|
|
|
|
|
|
|
|
logLevel, _ := logging.LogLevel(level)
|
|
|
|
|
|
logBuffer = append(logBuffer, struct {
|
|
|
|
|
|
time string
|
|
|
|
|
|
level logging.Level
|
|
|
|
|
|
log string
|
|
|
|
|
|
}{
|
2025-10-09 08:39:29 -07:00
|
|
|
|
time: t.Format(timeFormat),
|
2023-07-31 09:41:47 -07:00
|
|
|
|
level: logLevel,
|
|
|
|
|
|
log: newLog,
|
|
|
|
|
|
})
|
2023-06-16 07:55:33 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-20 00:35:50 -07:00
|
|
|
|
// GetLogs retrieves up to c log entries from the buffer that are at or below the specified level.
|
2023-07-31 09:41:47 -07:00
|
|
|
|
func GetLogs(c int, level string) []string {
|
|
|
|
|
|
var output []string
|
|
|
|
|
|
logLevel, _ := logging.LogLevel(level)
|
|
|
|
|
|
|
Test-quality audit: fix 2 prod bugs, strengthen weak tests, add mutation/fuzz/CI tooling (#5345)
* test(audit): add gremlins/rapid/coverage tooling + AUDIT.md scaffold
* test(audit): hygiene sweep (race-clean except logger global; Finding #2) + smell inventory
* test(audit): cover untested error/edge branches (TLS proxy+pin, migration tag cleanup=Finding #1)
* test(audit): strengthen internal/sub link tests (dedup key, TLS/Reality mapping, clash well-formedness)
* test(audit): property (rapid) + fuzz tests for joinHostPort/userinfo/pin/ParseLink
* test(audit): tighten frontend subSortIndex rejection assertions + wire coverage
* ci(audit): add shuffle gate + non-blocking race job (Finding #2) + fuzz-smoke; document mutation policy
* chore(audit): gitignore frontend coverage output
* test(audit): exhaustive whole-repo pass — strengthen 5 weak/fake tests (netproxy, CSP, modal per-protocol loops, schema coercions)
* docs(contributing): add Testing section (conventions, race/shuffle, fuzz, mutation policy); drop AUDIT.md ledger
* fix(logger,migration): guard logBuffer with mutex; execute legacy tag cleanup (tx.Exec); make CI race gate blocking
* ci(mutation): add nightly scoped gremlins workflow (informational artifacts)
* test(audit): strengthen runtime tests — baseURL scheme/port bounds, isNonEmptySlice, trafficReset
* test(audit): strengthen clash tests — reality field mapping + tcp-header validation
* test(audit): runtime — egress-proxy + content-type tests; drop redundant bp=='' branch
* test(audit): strengthen link parser/helper tests (defaultPort, splitComma, base64, canonicalQuery, tls/reality/transport mapping)
* test(audit): strengthen sub/xray/common/netsafe/mtproto/config/middleware tests (kill surviving mutants)
* test(audit): raise timeout on protocol-iteration modal tests (heavy re-renders, slow on CI)
* fix(logger): GetLogs returns at most c entries (off-by-one fix; addresses PR review)
* perf(logger): snapshot logBuffer under lock so GetLogs doesn't block logging; clarify fuzz-seed docs (addresses PR review)
2026-06-15 06:17:03 -07:00
|
|
|
|
// Snapshot (copy) under the lock, then filter/format unlocked: a UI log fetch
|
|
|
|
|
|
// must not block addToBuffer — and thus all logging — for the formatting loop.
|
|
|
|
|
|
// A copy (not a reslice) is required, since addToBuffer can append in place.
|
|
|
|
|
|
logBufferMu.Lock()
|
|
|
|
|
|
snapshot := make([]struct {
|
|
|
|
|
|
time string
|
|
|
|
|
|
level logging.Level
|
|
|
|
|
|
log string
|
|
|
|
|
|
}, len(logBuffer))
|
|
|
|
|
|
copy(snapshot, logBuffer)
|
|
|
|
|
|
logBufferMu.Unlock()
|
|
|
|
|
|
|
|
|
|
|
|
for i := len(snapshot) - 1; i >= 0 && len(output) < c; i-- {
|
|
|
|
|
|
if snapshot[i].level <= logLevel {
|
|
|
|
|
|
output = append(output, fmt.Sprintf("%s %s - %s", snapshot[i].time, snapshot[i].level, snapshot[i].log))
|
2023-07-31 09:41:47 -07:00
|
|
|
|
}
|
2023-06-16 07:55:33 -07:00
|
|
|
|
}
|
2023-07-31 09:41:47 -07:00
|
|
|
|
return output
|
2023-06-16 07:55:33 -07:00
|
|
|
|
}
|