logging with logrus

This commit is contained in:
piotr
2021-09-17 02:33:39 +02:00
parent 6ca8e6a175
commit b6dd4099fd
6 changed files with 37 additions and 27 deletions

View File

@@ -5,6 +5,7 @@ get:
go get github.com/dlasky/gotk3-layershell/layershell go get github.com/dlasky/gotk3-layershell/layershell
go get github.com/joshuarubin/go-sway go get github.com/joshuarubin/go-sway
go get github.com/allan-simon/go-singleinstance go get github.com/allan-simon/go-singleinstance
go get "github.com/sirupsen/logrus"
build: build:
go build -o bin/nwg-drawer *.go go build -o bin/nwg-drawer *.go

Binary file not shown.

1
go.mod
View File

@@ -7,4 +7,5 @@ require (
github.com/dlasky/gotk3-layershell v0.0.0-20210331230524-5cca0b819261 github.com/dlasky/gotk3-layershell v0.0.0-20210331230524-5cca0b819261
github.com/gotk3/gotk3 v0.6.0 github.com/gotk3/gotk3 v0.6.0
github.com/joshuarubin/go-sway v0.0.4 github.com/joshuarubin/go-sway v0.0.4
github.com/sirupsen/logrus v1.8.1
) )

8
go.sum
View File

@@ -1,7 +1,8 @@
github.com/allan-simon/go-singleinstance v0.0.0-20210120080615-d0997106ab37 h1:28uU3TtuvQ6KRndxg9TrC868jBWmSKgh0GTXkACCXmA= github.com/allan-simon/go-singleinstance v0.0.0-20210120080615-d0997106ab37 h1:28uU3TtuvQ6KRndxg9TrC868jBWmSKgh0GTXkACCXmA=
github.com/allan-simon/go-singleinstance v0.0.0-20210120080615-d0997106ab37/go.mod h1:6AXRstqK+32jeFmw89QGL2748+dj34Av4xc/I9oo9BY= github.com/allan-simon/go-singleinstance v0.0.0-20210120080615-d0997106ab37/go.mod h1:6AXRstqK+32jeFmw89QGL2748+dj34Av4xc/I9oo9BY=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dlasky/gotk3-layershell v0.0.0-20210331230524-5cca0b819261 h1:eoXn91ckLWKMXmQKX34UHEF2XMyQpRnnP80fDiu+kys= github.com/dlasky/gotk3-layershell v0.0.0-20210331230524-5cca0b819261 h1:eoXn91ckLWKMXmQKX34UHEF2XMyQpRnnP80fDiu+kys=
github.com/dlasky/gotk3-layershell v0.0.0-20210331230524-5cca0b819261/go.mod h1:d56Gslp3IaiT8lqxD/lO1Msz1wYgD8D/HTKHgSdg9tU= github.com/dlasky/gotk3-layershell v0.0.0-20210331230524-5cca0b819261/go.mod h1:d56Gslp3IaiT8lqxD/lO1Msz1wYgD8D/HTKHgSdg9tU=
github.com/gotk3/gotk3 v0.5.3-0.20210223154815-289cfb6dbf32/go.mod h1:/hqFpkNa9T3JgNAE2fLvCdov7c5bw//FHNZrZ3Uv9/Q= github.com/gotk3/gotk3 v0.5.3-0.20210223154815-289cfb6dbf32/go.mod h1:/hqFpkNa9T3JgNAE2fLvCdov7c5bw//FHNZrZ3Uv9/Q=
@@ -13,7 +14,10 @@ github.com/joshuarubin/lifecycle v1.0.0 h1:N/lPEC8f+dBZ1Tn99vShqp36LwB+LI7XNAiNa
github.com/joshuarubin/lifecycle v1.0.0/go.mod h1:sRy++ATvR9Ee21tkRdFkQeywAWvDsue66V70K0Dnl54= github.com/joshuarubin/lifecycle v1.0.0/go.mod h1:sRy++ATvR9Ee21tkRdFkQeywAWvDsue66V70K0Dnl54=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE=
github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q= github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
go.uber.org/atomic v1.3.2 h1:2Oa65PReHzfn29GpvgsYwloV9AVFHPDk8tYxt2c2tr4= go.uber.org/atomic v1.3.2 h1:2Oa65PReHzfn29GpvgsYwloV9AVFHPDk8tYxt2c2tr4=
@@ -22,3 +26,5 @@ go.uber.org/multierr v1.1.0 h1:HoEmRHQPVSqub6w2z2d2EOVs2fjyFRGyofhKuyDq0QI=
go.uber.org/multierr v1.1.0/go.mod h1:wR5kodmAFQ0UK8QlbwjlSNy0Z68gJhDJUG5sjR94q/0= go.uber.org/multierr v1.1.0/go.mod h1:wR5kodmAFQ0UK8QlbwjlSNy0Z68gJhDJUG5sjR94q/0=
golang.org/x/sync v0.0.0-20190412183630-56d357773e84 h1:IqXQ59gzdXv58Jmm2xn0tSOR9i6HqroaOFRQ3wR/dJQ= golang.org/x/sync v0.0.0-20190412183630-56d357773e84 h1:IqXQ59gzdXv58Jmm2xn0tSOR9i6HqroaOFRQ3wR/dJQ=
golang.org/x/sync v0.0.0-20190412183630-56d357773e84/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20190412183630-56d357773e84/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 h1:YyJpGZS1sBuBCzLAR1VEpK193GlqGZbnPFnPV/5Rsb4=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=

29
main.go
View File

@@ -3,7 +3,6 @@ package main
import ( import (
"flag" "flag"
"fmt" "fmt"
"log"
"os" "os"
"os/signal" "os/signal"
"path/filepath" "path/filepath"
@@ -12,6 +11,8 @@ import (
"syscall" "syscall"
"time" "time"
log "github.com/sirupsen/logrus"
"github.com/allan-simon/go-singleinstance" "github.com/allan-simon/go-singleinstance"
"github.com/dlasky/gotk3-layershell/layershell" "github.com/dlasky/gotk3-layershell/layershell"
"github.com/gotk3/gotk3/gdk" "github.com/gotk3/gotk3/gdk"
@@ -141,7 +142,7 @@ func main() {
for { for {
s := <-signalChan s := <-signalChan
if s == syscall.SIGTERM { if s == syscall.SIGTERM {
println("SIGTERM received, bye bye!") log.Info("SIGTERM received, bye bye!")
gtk.MainQuit() gtk.MainQuit()
} }
} }
@@ -155,7 +156,7 @@ func main() {
if err == nil { if err == nil {
i, err := strconv.Atoi(pid) i, err := strconv.Atoi(pid)
if err == nil { if err == nil {
println("Running instance found, sending SIGTERM and exiting...") log.Info("Running instance found, sending SIGTERM and exiting...")
syscall.Kill(i, syscall.SIGTERM) syscall.Kill(i, syscall.SIGTERM)
} }
} }
@@ -167,7 +168,7 @@ func main() {
if *lang == "" && os.Getenv("LANG") != "" { if *lang == "" && os.Getenv("LANG") != "" {
*lang = strings.Split(os.Getenv("LANG"), ".")[0] *lang = strings.Split(os.Getenv("LANG"), ".")[0]
} }
println(fmt.Sprintf("lang: %s", *lang)) log.Info(fmt.Sprintf("lang: %s", *lang))
// ENVIRONMENT // ENVIRONMENT
configDirectory = configDir() configDirectory = configDir()
@@ -187,7 +188,7 @@ func main() {
if err != nil { if err != nil {
pinned = nil pinned = nil
} }
println(fmt.Sprintf("Found %v pinned items", len(pinned))) log.Info(fmt.Sprintf("Found %v pinned items", len(pinned)))
cssFile := filepath.Join(configDirectory, *cssFileName) cssFile := filepath.Join(configDirectory, *cssFileName)
@@ -196,7 +197,7 @@ func main() {
setUpCategories() setUpCategories()
desktopFiles := listDesktopFiles() desktopFiles := listDesktopFiles()
println(fmt.Sprintf("Found %v desktop files", len(desktopFiles))) log.Info(fmt.Sprintf("Found %v desktop files", len(desktopFiles)))
status = parseDesktopFiles(desktopFiles) status = parseDesktopFiles(desktopFiles)
@@ -205,9 +206,9 @@ func main() {
paFile := filepath.Join(configDirectory, "preferred-apps.json") paFile := filepath.Join(configDirectory, "preferred-apps.json")
preferredApps, err = loadPreferredApps(paFile) preferredApps, err = loadPreferredApps(paFile)
if err != nil { if err != nil {
println(fmt.Sprintf("Custom associations file %s not found or invalid", paFile)) log.Error(fmt.Sprintf("Custom associations file %s not found or invalid", paFile))
} else { } else {
println(fmt.Sprintf("Found %v associations in %s", len(preferredApps), paFile)) log.Info(fmt.Sprintf("Found %v associations in %s", len(preferredApps), paFile))
} }
// USER INTERFACE // USER INTERFACE
@@ -217,10 +218,10 @@ func main() {
err = cssProvider.LoadFromPath(cssFile) err = cssProvider.LoadFromPath(cssFile)
if err != nil { if err != nil {
println(fmt.Sprintf("ERROR: %s css file not found or erroneous. Using GTK styling.", cssFile)) log.Errorf("ERROR: %s css file not found or erroneous. Using GTK styling.", cssFile)
println(fmt.Sprintf("%s", err)) log.Errorf("%s", err)
} else { } else {
println(fmt.Sprintf("Using style from %s", cssFile)) log.Info(fmt.Sprintf("Using style from %s", cssFile))
screen, _ := gdk.ScreenGetDefault() screen, _ := gdk.ScreenGetDefault()
gtk.AddProviderForScreen(screen, cssProvider, gtk.STYLE_PROVIDER_PRIORITY_APPLICATION) gtk.AddProviderForScreen(screen, cssProvider, gtk.STYLE_PROVIDER_PRIORITY_APPLICATION)
} }
@@ -242,7 +243,7 @@ func main() {
layershell.SetMonitor(win, monitor) layershell.SetMonitor(win, monitor)
} else { } else {
println(fmt.Sprintf("%s", err)) log.Errorf("%s", err)
} }
} }
@@ -307,7 +308,7 @@ func main() {
This feature is not really supported and will stay undocumented. This feature is not really supported and will stay undocumented.
*/ */
if !wayland() { if !wayland() {
println("Not Wayland, oh really?") log.Info("Not Wayland, oh really?")
win.SetDecorated(false) win.SetDecorated(false)
win.Maximize() win.Maximize()
} }
@@ -400,6 +401,6 @@ func main() {
} }
t := time.Now() t := time.Now()
println(fmt.Sprintf("UI created in %v ms. Thank you for your patience.", t.Sub(timeStart).Milliseconds())) log.Info(fmt.Sprintf("UI created in %v ms. Thank you for your patience.", t.Sub(timeStart).Milliseconds()))
gtk.Main() gtk.Main()
} }

View File

@@ -8,7 +8,6 @@ import (
"io" "io"
"io/fs" "io/fs"
"io/ioutil" "io/ioutil"
"log"
"os" "os"
"os/exec" "os/exec"
"path/filepath" "path/filepath"
@@ -17,6 +16,8 @@ import (
"strings" "strings"
"time" "time"
log "github.com/sirupsen/logrus"
"github.com/gotk3/gotk3/gdk" "github.com/gotk3/gotk3/gdk"
"github.com/gotk3/gotk3/glib" "github.com/gotk3/gotk3/glib"
"github.com/gotk3/gotk3/gtk" "github.com/gotk3/gotk3/gtk"
@@ -49,7 +50,7 @@ func createPixbuf(icon string, size int) (*gdk.Pixbuf, error) {
if strings.Contains(icon, "/") { if strings.Contains(icon, "/") {
pixbuf, err := gdk.PixbufNewFromFileAtSize(icon, size, size) pixbuf, err := gdk.PixbufNewFromFileAtSize(icon, size, size)
if err != nil { if err != nil {
println(fmt.Sprintf("%s", err)) log.Errorf("%s", err)
return nil, err return nil, err
} }
return pixbuf, nil return pixbuf, nil
@@ -92,7 +93,7 @@ func mapXdgUserDirs() map[string]string {
userDirsFile := filepath.Join(home, ".config/user-dirs.dirs") userDirsFile := filepath.Join(home, ".config/user-dirs.dirs")
if pathExists(userDirsFile) { if pathExists(userDirsFile) {
println(fmt.Sprintf("Using XDG user dirs from %s", userDirsFile)) log.Info(fmt.Sprintf("Using XDG user dirs from %s", userDirsFile))
lines, _ := loadTextFile(userDirsFile) lines, _ := loadTextFile(userDirsFile)
for _, l := range lines { for _, l := range lines {
if strings.HasPrefix(l, "XDG_DOCUMENTS_DIR") { if strings.HasPrefix(l, "XDG_DOCUMENTS_DIR") {
@@ -116,7 +117,7 @@ func mapXdgUserDirs() map[string]string {
} }
} }
} else { } else {
println(fmt.Sprintf("%s file not found, using defaults", userDirsFile)) log.Warnf("%s file not found, using defaults", userDirsFile)
} }
return result return result
@@ -177,13 +178,13 @@ func createDir(dir string) {
if _, err := os.Stat(dir); os.IsNotExist(err) { if _, err := os.Stat(dir); os.IsNotExist(err) {
err := os.MkdirAll(dir, os.ModePerm) err := os.MkdirAll(dir, os.ModePerm)
if err == nil { if err == nil {
fmt.Println("Creating dir:", dir) log.Infof("Creating dir: %s", dir)
} }
} }
} }
func copyFile(src, dst string) error { func copyFile(src, dst string) error {
fmt.Println("Copying file:", dst) log.Infof("Copying file: %s", dst)
var err error var err error
var srcfd *os.File var srcfd *os.File
@@ -378,7 +379,7 @@ func parseDesktopFiles(desktopFiles []string) string {
return desktopEntries[i].NameLoc < desktopEntries[j].NameLoc return desktopEntries[i].NameLoc < desktopEntries[j].NameLoc
}) })
summary := fmt.Sprintf("%v entries (+%v hidden)", len(desktopEntries)-hidden, hidden) summary := fmt.Sprintf("%v entries (+%v hidden)", len(desktopEntries)-hidden, hidden)
println(fmt.Sprintf("Skipped %v duplicates; %v .desktop entries hidden by \"NoDisplay=true\"", skipped, hidden)) log.Infof("Skipped %v duplicates; %v .desktop entries hidden by \"NoDisplay=true\"", skipped, hidden)
return summary return summary
} }
@@ -471,20 +472,20 @@ func loadTextFile(path string) ([]string, error) {
func pinItem(itemID string) { func pinItem(itemID string) {
for _, item := range pinned { for _, item := range pinned {
if item == itemID { if item == itemID {
println(item, "already pinned") log.Warn(item, "already pinned")
return return
} }
} }
pinned = append(pinned, itemID) pinned = append(pinned, itemID)
savePinned() savePinned()
println(itemID, "pinned") log.Infof("%s pinned", itemID)
} }
func unpinItem(itemID string) { func unpinItem(itemID string) {
if isIn(pinned, itemID) { if isIn(pinned, itemID) {
pinned = remove(pinned, itemID) pinned = remove(pinned, itemID)
savePinned() savePinned()
println(itemID, "unpinned") log.Infof("%s unpinned", itemID)
} }
} }
@@ -510,7 +511,7 @@ func savePinned() {
_, err := f.WriteString(line + "\n") _, err := f.WriteString(line + "\n")
if err != nil { if err != nil {
println("Error saving pinned", err) log.Errorf("Error saving pinned", err)
} }
} }
} }
@@ -560,7 +561,7 @@ func launch(command string, terminal bool) {
} }
msg := fmt.Sprintf("env vars: %s; command: '%s'; args: %s\n", envVars, elements[cmdIdx], elements[1+cmdIdx:]) msg := fmt.Sprintf("env vars: %s; command: '%s'; args: %s\n", envVars, elements[cmdIdx], elements[1+cmdIdx:])
println(msg) log.Info(msg)
go cmd.Run() go cmd.Run()