Skip to content

Commit

Permalink
switch to charmbracelet logging, do not interrupt backup on broken files
Browse files Browse the repository at this point in the history
  • Loading branch information
poolpOrg committed Nov 7, 2024
1 parent e4b79e1 commit 967b92e
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 8 deletions.
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,10 @@ require (
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
github.com/aymerick/douceur v0.2.0 // indirect
github.com/charmbracelet/lipgloss v0.12.1 // indirect
github.com/charmbracelet/log v0.4.0 // indirect
github.com/charmbracelet/x/ansi v0.1.4 // indirect
github.com/dlclark/regexp2 v1.11.0 // indirect
github.com/go-logfmt/logfmt v0.6.0 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/gorilla/css v1.0.1 // indirect
github.com/json-iterator/go v1.1.12 // indirect
Expand All @@ -58,6 +60,7 @@ require (
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
github.com/yuin/goldmark v1.7.4 // indirect
github.com/yuin/goldmark-emoji v1.0.3 // indirect
golang.org/x/exp v0.0.0-20231006140011-7918f672742d // indirect
golang.org/x/net v0.28.0 // indirect
golang.org/x/sys v0.25.0 // indirect
golang.org/x/text v0.17.0 // indirect
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ github.com/charmbracelet/glamour v0.8.0 h1:tPrjL3aRcQbn++7t18wOpgLyl8wrOHUEDS7IZ
github.com/charmbracelet/glamour v0.8.0/go.mod h1:ViRgmKkf3u5S7uakt2czJ272WSg2ZenlYEZXT2x7Bjw=
github.com/charmbracelet/lipgloss v0.12.1 h1:/gmzszl+pedQpjCOH+wFkZr/N90Snz40J/NR7A0zQcs=
github.com/charmbracelet/lipgloss v0.12.1/go.mod h1:V2CiwIuhx9S1S1ZlADfOj9HmxeMAORuz5izHb0zGbB8=
github.com/charmbracelet/log v0.4.0 h1:G9bQAcx8rWA2T3pWvx7YtPTPwgqpk7D68BX21IRW8ZM=
github.com/charmbracelet/log v0.4.0/go.mod h1:63bXt/djrizTec0l11H20t8FDSvA4CRZJ1KH22MdptM=
github.com/charmbracelet/x/ansi v0.1.4 h1:IEU3D6+dWwPSgZ6HBH+v6oUuZ/nVawMiWj5831KfiLM=
github.com/charmbracelet/x/ansi v0.1.4/go.mod h1:dk73KoMTT5AX5BsX0KrqhsTqAnhZZoCBjs7dGWp4Ktw=
github.com/charmbracelet/x/exp/golden v0.0.0-20240715153702-9ba8adf781c4 h1:6KzMkQeAF56rggw2NZu1L+TH7j9+DM1/2Kmh7KUxg1I=
Expand All @@ -35,6 +37,8 @@ github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+m
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/gabriel-vasile/mimetype v1.4.2 h1:w5qFW6JKBz9Y393Y4q372O9A7cUSequkh1Q7OhCmWKU=
github.com/gabriel-vasile/mimetype v1.4.2/go.mod h1:zApsH/mKG4w07erKIaJPFiX0Tsq9BFQgN3qGY5GnNgA=
github.com/go-logfmt/logfmt v0.6.0 h1:wGYYu3uicYdqXVgoYbvnkrPVXkuLM1p1ifugDMEdRi4=
github.com/go-logfmt/logfmt v0.6.0/go.mod h1:WYhtIu8zTZfxdn5+rREduYbwxfcBr/Vr6KEVveWlfTs=
github.com/gobwas/glob v0.2.3 h1:A4xDbljILXROh+kObIiy5kIaPYD8e96x1tgBhUI5J+Y=
github.com/gobwas/glob v0.2.3/go.mod h1:d3Ez4x06l9bZtSvzIay5+Yzi0fmZzPgnTbPcKjJAkT8=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
Expand Down Expand Up @@ -133,6 +137,8 @@ github.com/yuin/goldmark-emoji v1.0.3 h1:aLRkLHOuBR2czCY4R8olwMjID+tENfhyFDMCRhb
github.com/yuin/goldmark-emoji v1.0.3/go.mod h1:tTkZEbwu5wkPmgTcitqddVxY9osFZiavD+r4AzQrh1U=
golang.org/x/crypto v0.26.0 h1:RrRspgV4mU+YwB4FYnuBoKsUapNIL5cohGAmSH3azsw=
golang.org/x/crypto v0.26.0/go.mod h1:GY7jblb9wI+FOo5y8/S2oY4zWP07AkOJ4+jxCqdqn54=
golang.org/x/exp v0.0.0-20231006140011-7918f672742d h1:jtJma62tbqLibJ5sFQz8bKtEM8rJBtfilJ2qTU199MI=
golang.org/x/exp v0.0.0-20231006140011-7918f672742d/go.mod h1:ldy0pHrwJyGW56pPQzzkH36rKxoZW1tw7ZJpeKx+hdo=
golang.org/x/mod v0.21.0 h1:vvrHzRwRfVKSiLrG+d4FMl/Qi4ukBCE6kZlTUkDYRT0=
golang.org/x/mod v0.21.0/go.mod h1:6SkKJ3Xj0I0BrPOZoBy3bdMptDDU9oJrpohJ3eWZ1fY=
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
Expand Down
49 changes: 42 additions & 7 deletions logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@ import (
"os"
"strings"
"sync"

"github.com/charmbracelet/log"
)

var stdoutChannel chan string
var stderrChannel chan string
var debugChannel chan string
var traceChannel chan string
var profileChannel chan string

var enableInfo = false
var enableTracing = false
Expand All @@ -19,6 +22,28 @@ var enableProfiling = false
var mutraceSubsystems sync.Mutex
var traceSubsystems map[string]bool

var stdoutLogger *log.Logger
var stderrLogger *log.Logger
var debugLogger *log.Logger
var traceLogger *log.Logger
var profileLogger *log.Logger

func init() {
stdoutLogger = log.New(os.Stdout)
stderrLogger = log.NewWithOptions(os.Stdout, log.Options{
Prefix: "warn",
})
debugLogger = log.NewWithOptions(os.Stdout, log.Options{
Prefix: "debug",
})
traceLogger = log.NewWithOptions(os.Stdout, log.Options{
Prefix: "trace",
})
profileLogger = log.NewWithOptions(os.Stdout, log.Options{
Prefix: "profile",
})
}

func Printf(format string, args ...interface{}) {
stdoutChannel <- fmt.Sprintf(format, args...)
}
Expand All @@ -30,7 +55,7 @@ func Info(format string, args ...interface{}) {
}

func Warn(format string, args ...interface{}) {
stderrChannel <- fmt.Sprintf("[warning] "+format, args...)
stderrChannel <- fmt.Sprintf(format, args...)
}

func Error(format string, args ...interface{}) {
Expand All @@ -50,14 +75,14 @@ func Trace(subsystem string, format string, args ...interface{}) {
}
mutraceSubsystems.Unlock()
if exists {
traceChannel <- fmt.Sprintf("[trace]: "+subsystem+": "+format, args...)
traceChannel <- fmt.Sprintf(subsystem+": "+format, args...)
}
}
}

func Profile(format string, args ...interface{}) {
if enableProfiling {
traceChannel <- fmt.Sprintf("[profiling]: "+format, args...)
profileChannel <- fmt.Sprintf(format, args...)
}
}

Expand All @@ -81,37 +106,46 @@ func Start() func() {
stderrChannel = make(chan string)
debugChannel = make(chan string)
traceChannel = make(chan string)
profileChannel = make(chan string)

var wg sync.WaitGroup

wg.Add(1)
go func() {
for msg := range stdoutChannel {
fmt.Println(msg)
stdoutLogger.Print(msg)
}
wg.Done()
}()

wg.Add(1)
go func() {
for msg := range stderrChannel {
fmt.Fprintln(os.Stderr, msg)
stderrLogger.Print(msg)
}
wg.Done()
}()

wg.Add(1)
go func() {
for msg := range debugChannel {
fmt.Println(msg)
debugLogger.Print(msg)
}
wg.Done()
}()

wg.Add(1)
go func() {
for msg := range traceChannel {
fmt.Fprintln(os.Stderr, msg)
traceLogger.Print(msg)
}
wg.Done()
}()

wg.Add(1)
go func() {
for msg := range profileChannel {
profileLogger.Print(msg)
}
wg.Done()
}()
Expand All @@ -121,6 +155,7 @@ func Start() func() {
close(stderrChannel)
close(debugChannel)
close(traceChannel)
close(profileChannel)
wg.Wait()
}
}
3 changes: 2 additions & 1 deletion snapshot/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -529,7 +529,8 @@ func (snap *Snapshot) Backup(scanDir string, options *PushOptions) error {
for _, child := range record.Children {
value, err := sc.GetChecksum(filepath.Join(record.Pathname, child.Name()))
if err != nil {
return err
continue
// return err
}
dirEntry.AddChild(value, child)
}
Expand Down

0 comments on commit 967b92e

Please sign in to comment.