tidy up logging
This commit is contained in:
parent
7bdef46ab9
commit
22d8316f12
13
config.go
13
config.go
|
@ -3,7 +3,6 @@ package main
|
|||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"github.com/adhocore/jsonc"
|
||||
|
@ -32,7 +31,7 @@ func (config *Config) load() error {
|
|||
|
||||
homeDir, err := os.UserHomeDir()
|
||||
if err != nil {
|
||||
log.Println("could not get home directory!")
|
||||
elog.Println("could not get home directory!")
|
||||
return err
|
||||
}
|
||||
|
||||
|
@ -43,25 +42,25 @@ func (config *Config) load() error {
|
|||
defaultConfig := makeDefaultConfig()
|
||||
defaultConfigJSON, err := json.MarshalIndent(defaultConfig, "", "")
|
||||
if err != nil {
|
||||
log.Println("could not encode default config!")
|
||||
elog.Println("could not encode default config!")
|
||||
return err
|
||||
}
|
||||
//TODO: make parent directories if they don't exist
|
||||
err = os.Mkdir(homeDir+"/.config", 755)
|
||||
if err != nil && !os.IsExist(err) {
|
||||
log.Println("could not create .config directory", err)
|
||||
elog.Println("could not create .config directory", err)
|
||||
return err
|
||||
}
|
||||
err = os.Mkdir(configDir, 755)
|
||||
if err != nil && !os.IsExist(err) {
|
||||
log.Println("could not create .config/gopaper directory")
|
||||
elog.Println("could not create .config/gopaper directory")
|
||||
return err
|
||||
}
|
||||
|
||||
file, err := os.Create(configDir + "config.jsonc")
|
||||
defer file.Close()
|
||||
if err != nil {
|
||||
log.Println("could not create config file!")
|
||||
elog.Println("could not create config file!")
|
||||
return err
|
||||
}
|
||||
file.Write(defaultConfigJSON)
|
||||
|
@ -72,7 +71,7 @@ func (config *Config) load() error {
|
|||
configRaw = j.Strip(configRaw)
|
||||
err = json.Unmarshal(configRaw, &config)
|
||||
if err != nil {
|
||||
log.Println("Couldn't unmarshal config!")
|
||||
elog.Println("Couldn't unmarshal config!")
|
||||
return err
|
||||
}
|
||||
|
||||
|
|
5
image.go
5
image.go
|
@ -37,7 +37,6 @@ func hexToHSL(string) [3]int {
|
|||
func pickRandomImage(dir string) (string, error) {
|
||||
|
||||
dir = config.ImagesDir + dir
|
||||
log.Printf("getting random file from %v\n", dir)
|
||||
randomImg, filename, _ := getRandomFile(dir)
|
||||
filenameNoExt := strings.TrimSuffix(filename, filepath.Ext(filename))
|
||||
|
||||
|
@ -50,13 +49,13 @@ func pickRandomImage(dir string) (string, error) {
|
|||
if !fileExists {
|
||||
img, err := loadImage(randomImg)
|
||||
if err != nil {
|
||||
log.Printf("failed to load image %v", randomImg)
|
||||
elog.Printf("failed to load image %v", randomImg)
|
||||
return "", err
|
||||
}
|
||||
dst := processImage(img, config)
|
||||
saveImage(curr, dst)
|
||||
} else {
|
||||
log.Println("file already exists in cache. skipping image processing")
|
||||
log.Printf("file %v already exists in cache. skipping image processing\n", filename)
|
||||
}
|
||||
return curr, nil
|
||||
}
|
||||
|
|
84
main.go
84
main.go
|
@ -14,44 +14,23 @@ var config Config
|
|||
const sockfile = "/tmp/gopaper.sock"
|
||||
const pidfilePath = "/tmp/gopaper.pid"
|
||||
|
||||
var clog = log.New(os.Stdout, "## ", 0)
|
||||
var elog = log.New(os.Stdout, "ERROR: ", 3)
|
||||
|
||||
func main() {
|
||||
//SETUP FLAGS
|
||||
var debugFlag = flag.Bool("debug", false, "help message for flag n")
|
||||
flag.Parse()
|
||||
|
||||
//INITIALIZE CONFIG
|
||||
clog.Println("Welcome to GoPaper!")
|
||||
|
||||
//INIT CONFIG
|
||||
err := config.load()
|
||||
if err != nil {
|
||||
log.Println("unable to initialize config", err)
|
||||
elog.Println("unable to initialize config", err)
|
||||
}
|
||||
|
||||
//SETUP LOGGER
|
||||
log.SetFlags(log.LstdFlags | log.Lshortfile)
|
||||
if *debugFlag {
|
||||
logfile, err := openLogfile()
|
||||
if err != nil {
|
||||
log.Println("failed to load config", err)
|
||||
cleanExit(1)
|
||||
}
|
||||
log.SetOutput(logfile)
|
||||
}
|
||||
log.SetFlags(log.LstdFlags | log.Lshortfile | log.Lmicroseconds)
|
||||
log.Println("Welcome to GoPaper!")
|
||||
|
||||
err = killPrevProc()
|
||||
if err != nil {
|
||||
log.Println("error attempting to kill pre-existing process: ", err)
|
||||
cleanExit(1)
|
||||
}
|
||||
//create new pidfile
|
||||
pid := strconv.FormatInt(int64(os.Getpid()), 10)
|
||||
log.Printf("pid: %v", pid)
|
||||
pidfile, err := os.Create(pidfilePath)
|
||||
if err != nil {
|
||||
log.Println("error trying to create pidfile:", err)
|
||||
cleanExit(1)
|
||||
}
|
||||
pidfile.WriteString(pid)
|
||||
handleProcesses()
|
||||
setupLogging(*debugFlag)
|
||||
|
||||
var waitGroup sync.WaitGroup
|
||||
waitGroup.Add(1)
|
||||
|
@ -98,7 +77,7 @@ func cleanExit(code int) {
|
|||
|
||||
err := os.Remove(sockfile)
|
||||
if err != nil && !os.IsNotExist(err) {
|
||||
log.Printf("unable to remove sockfile at %v. Error: %v\n", sockfile, err)
|
||||
elog.Printf("unable to remove sockfile at %v. Error: %v\n", sockfile, err)
|
||||
} else if !os.IsNotExist(err) {
|
||||
log.Println("sockfile removed")
|
||||
} else {
|
||||
|
@ -107,7 +86,7 @@ func cleanExit(code int) {
|
|||
|
||||
err = os.Remove(pidfilePath)
|
||||
if err != nil && !os.IsNotExist(err) {
|
||||
log.Printf("unable to remove pidfile at %v. Error: %v\n", pidfilePath, err)
|
||||
elog.Printf("unable to remove pidfile at %v. Error: %v\n", pidfilePath, err)
|
||||
} else if !os.IsNotExist(err) {
|
||||
log.Println("pidfile removed")
|
||||
} else {
|
||||
|
@ -124,18 +103,51 @@ func killPrevProc() error {
|
|||
//if it does, kill the process and delete the pidfile
|
||||
prexPid, err := os.ReadFile(pidfilePath)
|
||||
if err != nil {
|
||||
log.Println("unable to read pre-existing pidfile", err)
|
||||
elog.Println("unable to read pre-existing pidfile", err)
|
||||
return err
|
||||
}
|
||||
//kill process
|
||||
err = exec.Command("kill", string(prexPid)).Run()
|
||||
if err != nil {
|
||||
log.Printf("Error attempting to kill process %v: %v\n", string(prexPid), err)
|
||||
elog.Printf("Error attempting to kill process %v: %v\n", string(prexPid), err)
|
||||
}
|
||||
//remove pidfile
|
||||
err = os.Remove(pidfilePath)
|
||||
if err != nil {
|
||||
log.Printf("unable to remove pidfile %v\n", pidfilePath)
|
||||
if err != nil && !os.IsNotExist(err) {
|
||||
elog.Printf("unable to remove pidfile at %v. Error: %v\n", pidfilePath, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
func handleProcesses() {
|
||||
err := killPrevProc()
|
||||
if err != nil {
|
||||
elog.Println("error attempting to kill pre-existing process: ", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
//create new pidfile
|
||||
pid := strconv.FormatInt(int64(os.Getpid()), 10)
|
||||
pidfile, err := os.Create(pidfilePath)
|
||||
if err != nil {
|
||||
elog.Println("error trying to create pidfile:", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
pidfile.WriteString(pid)
|
||||
|
||||
}
|
||||
|
||||
func setupLogging(debug bool) {
|
||||
|
||||
//SETUP LOGGERS
|
||||
if debug {
|
||||
logfile, err := openLogfile()
|
||||
if err != nil {
|
||||
elog.Println("failed to load config", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
log.SetOutput(logfile)
|
||||
clog.SetOutput(logfile)
|
||||
elog.SetOutput(logfile)
|
||||
}
|
||||
elog.SetFlags(log.LstdFlags | log.Lshortfile)
|
||||
log.SetFlags(log.LstdFlags)
|
||||
}
|
||||
|
|
|
@ -16,11 +16,12 @@ func slideshow(ch <-chan string) {
|
|||
|
||||
go func() {
|
||||
for {
|
||||
err := setRandomWallpaper(dir)
|
||||
img, err := setRandomWallpaper(dir)
|
||||
if err != nil {
|
||||
fmt.Printf("Failed to set random wallpaper from directory %v.", dir)
|
||||
panic(err)
|
||||
}
|
||||
log.Printf("set %v as wallpaper\n", img)
|
||||
select {
|
||||
case dir = <-ch:
|
||||
log.Println("directory set!")
|
||||
|
@ -33,10 +34,10 @@ func slideshow(ch <-chan string) {
|
|||
}()
|
||||
}
|
||||
|
||||
func setRandomWallpaper(dir string) error {
|
||||
func setRandomWallpaper(dir string) (string, error) {
|
||||
img, err := pickRandomImage(dir)
|
||||
if err != nil {
|
||||
return err
|
||||
return "", err
|
||||
}
|
||||
|
||||
desktopEnv, bool := os.LookupEnv("DESKTOP_SESSION")
|
||||
|
@ -53,5 +54,5 @@ func setRandomWallpaper(dir string) error {
|
|||
mode = ""
|
||||
}
|
||||
|
||||
return wallutils.SetWallpaperCustom(img, mode, false)
|
||||
return img, wallutils.SetWallpaperCustom(img, mode, false)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue