breto

A status bar written in Go.
git clone git://git.swab.dev/breto.git
Log | Files | Refs | README | LICENSE

commit f991edbc63c0665d136b2f3d13750230fb85f1ff
parent cac365d8b4d0118a8fb3e300a3107e33d6310a95
Author: Jaron Swab <jaron@swab.dev>
Date:   Mon, 12 Apr 2021 21:03:34 -0400

Added log file for errors

Diffstat:
Mmain.go | 35+++++++++++++++++++++++++++++------
1 file changed, 29 insertions(+), 6 deletions(-)

diff --git a/main.go b/main.go @@ -3,6 +3,8 @@ package main import ( "log" "math" + "os" + "sync" "time" "git.swab.dev/breto.git/blocks" @@ -54,28 +56,49 @@ func main() { bat.FiveMins = math.Floor(math.Remainder(bat.Passed, 300)) } + var mutex = &sync.Mutex{} select { // updates the go routine channels as they send data case info.Weather = <-cWttr: case info.WttrErr = <-eWttr: - log.Println(info.WttrErr.Error()) + writeToLog(info.WttrErr.Error(), mutex) case info.RamFree = <-cRAM: case info.RamErr = <-eRAM: - log.Println(info.RamErr.Error()) + writeToLog(info.RamErr.Error(), mutex) case info.HomeSpace = <-cHomeDisk: case info.HomeErr = <-eHomeDisk: - log.Println(info.HomeErr.Error()) + writeToLog(info.HomeErr.Error(), mutex) default: } // Status bar information as defined by the CLI flags. // reset status on every run. - finalStatus := o.Output("", info, ico, bat) + status := o.Output("", info, ico, bat) // Output methods as specified by CLI flags. if o.Dwm { - ui.Dwm(finalStatus) + ui.Dwm(status) } else { - ui.Default(finalStatus) + ui.Default(status) } } } + +func writeToLog(errMsg string, mutex *sync.Mutex) { + mutex.Lock() + cache := os.Getenv("XDG_CACHE_HOME") + if cache == "" { + cache = "." + } + + path := fmt.Spritf("%s/breto.log", cache) + f, err := os.OpenFile(path, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) + if err != nil { + log.Println(err) + } + + defer mutex.Unlock() + defer f.Close() + + logger := log.New(f, "prefix", log.LstdFlags) + logger.Println(errMsg) +}