on main help sign in
Merge pull request #113 from charmbracelet/log-warn
eeeb9a0Kujtim Hoxha
internal/config/load.go+2−6
@@ -52,16 +52,12 @@ func Load(workingDir string, debug bool) (*Config, error) {
52 cfg.Options.Debug = true
52 cfg.Options.Debug = true
53 }
53 }
54
54
55− // Init logs
55+ // Setup logs
56− log.Init(
56+ log.Setup(
57 filepath.Join(cfg.Options.DataDirectory, "logs", fmt.Sprintf("%s.log", appName)),
57 filepath.Join(cfg.Options.DataDirectory, "logs", fmt.Sprintf("%s.log", appName)),
58 cfg.Options.Debug,
58 cfg.Options.Debug,
59 )
59 )
60
60
61− if err != nil {
62− return nil, fmt.Errorf("failed to load config: %w", err)
63− }
64−
65 // Load known providers, this loads the config from fur
61 // Load known providers, this loads the config from fur
66 providers, err := LoadProviders(client.New())
62 providers, err := LoadProviders(client.New())
67 if err != nil || len(providers) == 0 {
63 if err != nil || len(providers) == 0 {
internal/fsext/fileutil.go+7−2
@@ -12,6 +12,7 @@ import (
12
12
13 "github.com/bmatcuk/doublestar/v4"
13 "github.com/bmatcuk/doublestar/v4"
14 "github.com/charlievieth/fastwalk"
14 "github.com/charlievieth/fastwalk"
15+ "github.com/charmbracelet/crush/internal/log"
15
16
16 ignore "github.com/sabhiram/go-gitignore"
17 ignore "github.com/sabhiram/go-gitignore"
17)
18)
@@ -25,11 +26,15 @@ func init() {
25 var err error
26 var err error
26 rgPath, err = exec.LookPath("rg")
27 rgPath, err = exec.LookPath("rg")
27 if err != nil {
28 if err != nil {
29+ if log.Initialized() {
28− slog.Warn("Ripgrep (rg) not found in $PATH. Some features might be limited or slower.")
30+ slog.Warn("Ripgrep (rg) not found in $PATH. Some features might be limited or slower.")
31+ }
29 }
32 }
30 fzfPath, err = exec.LookPath("fzf")
33 fzfPath, err = exec.LookPath("fzf")
31 if err != nil {
34 if err != nil {
35+ if log.Initialized() {
32− slog.Warn("FZF not found in $PATH. Some features might be limited or slower.")
36+ slog.Warn("FZF not found in $PATH. Some features might be limited or slower.")
37+ }
33 }
38 }
34}
39}
35
40
internal/log/log.go+11−2
@@ -6,14 +6,18 @@ import (
6 "os"
6 "os"
7 "runtime/debug"
7 "runtime/debug"
8 "sync"
8 "sync"
9+ "sync/atomic"
9 "time"
10 "time"
10
11
11 "gopkg.in/natefinch/lumberjack.v2"
12 "gopkg.in/natefinch/lumberjack.v2"
12)
13)
13
14
15+var (
14−var initOnce sync.Once
16+ initOnce sync.Once
17+ initialized atomic.Bool
18+)
15
19
16−func Init(logFile string, debug bool) {
20+func Setup(logFile string, debug bool) {
17 initOnce.Do(func() {
21 initOnce.Do(func() {
18 logRotator := &lumberjack.Logger{
22 logRotator := &lumberjack.Logger{
19 Filename: logFile,
23 Filename: logFile,
@@ -34,9 +38,14 @@ func Init(logFile string, debug bool) {
34 })
38 })
35
39
36 slog.SetDefault(slog.New(logger))
40 slog.SetDefault(slog.New(logger))
41+ initialized.Store(true)
37 })
42 })
38}
43}
39
44
45+func Initialized() bool {
46+ return initialized.Load()
47+}
48+
40func RecoverPanic(name string, cleanup func()) {
49func RecoverPanic(name string, cleanup func()) {
41 if r := recover(); r != nil {
50 if r := recover(); r != nil {
42 // Create a timestamped panic log file
51 // Create a timestamped panic log file