From f884ff1f845bfb6652e9f3ce9dff64b8756f929a Mon Sep 17 00:00:00 2001 From: Ayman Bagabas Date: Tue, 8 Jul 2025 15:49:54 -0400 Subject: [PATCH 1/3] feat(log): add atomic check for initialization --- internal/log/log.go | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/internal/log/log.go b/internal/log/log.go index 31c183e9d7274d35ad2d70c7c1f5418586bc5a2d..8d2583cdabb0a6d4184137e25e36bf253d86d858 100644 --- a/internal/log/log.go +++ b/internal/log/log.go @@ -6,12 +6,16 @@ import ( "os" "runtime/debug" "sync" + "sync/atomic" "time" "gopkg.in/natefinch/lumberjack.v2" ) -var initOnce sync.Once +var ( + initOnce sync.Once + initialized atomic.Bool +) func Init(logFile string, debug bool) { initOnce.Do(func() { @@ -34,9 +38,14 @@ func Init(logFile string, debug bool) { }) slog.SetDefault(slog.New(logger)) + initialized.Store(true) }) } +func IsInitialized() bool { + return initialized.Load() +} + func RecoverPanic(name string, cleanup func()) { if r := recover(); r != nil { // Create a timestamped panic log file From edd4996a4f89ba56cec018a23ca74aed2ac96d35 Mon Sep 17 00:00:00 2001 From: Ayman Bagabas Date: Tue, 8 Jul 2025 15:50:14 -0400 Subject: [PATCH 2/3] fix(fsext): only log warnings if log is initialized --- internal/fsext/fileutil.go | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/internal/fsext/fileutil.go b/internal/fsext/fileutil.go index e350b61f6c4fd02c0088cc1f2f5c35c0bbc45259..677613ffc047919c7cf888a888da8ccbce7ea585 100644 --- a/internal/fsext/fileutil.go +++ b/internal/fsext/fileutil.go @@ -12,6 +12,7 @@ import ( "github.com/bmatcuk/doublestar/v4" "github.com/charlievieth/fastwalk" + "github.com/charmbracelet/crush/internal/log" ignore "github.com/sabhiram/go-gitignore" ) @@ -25,11 +26,15 @@ func init() { var err error rgPath, err = exec.LookPath("rg") if err != nil { - slog.Warn("Ripgrep (rg) not found in $PATH. Some features might be limited or slower.") + if log.IsInitialized() { + slog.Warn("Ripgrep (rg) not found in $PATH. Some features might be limited or slower.") + } } fzfPath, err = exec.LookPath("fzf") if err != nil { - slog.Warn("FZF not found in $PATH. Some features might be limited or slower.") + if log.IsInitialized() { + slog.Warn("FZF not found in $PATH. Some features might be limited or slower.") + } } } From c2d0d64c7d70582466e309bbc3bb36fce86fe26c Mon Sep 17 00:00:00 2001 From: Ayman Bagabas Date: Tue, 8 Jul 2025 15:55:36 -0400 Subject: [PATCH 3/3] refactor(log): rename log initialization function for clarity --- internal/config/load.go | 8 ++------ internal/fsext/fileutil.go | 4 ++-- internal/log/log.go | 4 ++-- 3 files changed, 6 insertions(+), 10 deletions(-) diff --git a/internal/config/load.go b/internal/config/load.go index d472c2a9632dd704a998bb7d427974b5b338cd76..84585d05d56dddbac36d2d147cc5c4cada781c7e 100644 --- a/internal/config/load.go +++ b/internal/config/load.go @@ -52,16 +52,12 @@ func Load(workingDir string, debug bool) (*Config, error) { cfg.Options.Debug = true } - // Init logs - log.Init( + // Setup logs + log.Setup( filepath.Join(cfg.Options.DataDirectory, "logs", fmt.Sprintf("%s.log", appName)), cfg.Options.Debug, ) - if err != nil { - return nil, fmt.Errorf("failed to load config: %w", err) - } - // Load known providers, this loads the config from fur providers, err := LoadProviders(client.New()) if err != nil || len(providers) == 0 { diff --git a/internal/fsext/fileutil.go b/internal/fsext/fileutil.go index 677613ffc047919c7cf888a888da8ccbce7ea585..c3678041de4239cf66247ebbd9cb084cb8eb6b8a 100644 --- a/internal/fsext/fileutil.go +++ b/internal/fsext/fileutil.go @@ -26,13 +26,13 @@ func init() { var err error rgPath, err = exec.LookPath("rg") if err != nil { - if log.IsInitialized() { + if log.Initialized() { slog.Warn("Ripgrep (rg) not found in $PATH. Some features might be limited or slower.") } } fzfPath, err = exec.LookPath("fzf") if err != nil { - if log.IsInitialized() { + if log.Initialized() { slog.Warn("FZF not found in $PATH. Some features might be limited or slower.") } } diff --git a/internal/log/log.go b/internal/log/log.go index 8d2583cdabb0a6d4184137e25e36bf253d86d858..bf99fe60fa9a5015029af171adfd6b3f9bf5596b 100644 --- a/internal/log/log.go +++ b/internal/log/log.go @@ -17,7 +17,7 @@ var ( initialized atomic.Bool ) -func Init(logFile string, debug bool) { +func Setup(logFile string, debug bool) { initOnce.Do(func() { logRotator := &lumberjack.Logger{ Filename: logFile, @@ -42,7 +42,7 @@ func Init(logFile string, debug bool) { }) } -func IsInitialized() bool { +func Initialized() bool { return initialized.Load() }