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 e350b61f6c4fd02c0088cc1f2f5c35c0bbc45259..c3678041de4239cf66247ebbd9cb084cb8eb6b8a 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.Initialized() { + 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.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 31c183e9d7274d35ad2d70c7c1f5418586bc5a2d..bf99fe60fa9a5015029af171adfd6b3f9bf5596b 100644 --- a/internal/log/log.go +++ b/internal/log/log.go @@ -6,14 +6,18 @@ 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) { +func Setup(logFile string, debug bool) { initOnce.Do(func() { logRotator := &lumberjack.Logger{ Filename: logFile, @@ -34,9 +38,14 @@ func Init(logFile string, debug bool) { }) slog.SetDefault(slog.New(logger)) + initialized.Store(true) }) } +func Initialized() bool { + return initialized.Load() +} + func RecoverPanic(name string, cleanup func()) { if r := recover(); r != nil { // Create a timestamped panic log file