Compare commits

...

2 Commits

Author SHA1 Message Date
andrzej 78ad86c594 merge fix 2024-11-04 21:22:29 +01:00
andrzej 09644aeaaf log to file 2024-11-04 21:13:28 +01:00
3 changed files with 17 additions and 12 deletions

View File

@ -1,7 +1,7 @@
package main
import (
"fmt"
"log"
"math/rand"
"os"
"regexp"
@ -20,13 +20,13 @@ func getRandomFile(dir string) (path string, filename string, error error) {
randomImg = files[randomIndex]
//re-roll if you get a directory
if files[randomIndex].IsDir() {
fmt.Printf("%v is a directory. Trying again... \n", randomImg.Name())
log.Printf("%v is a directory. Trying again... \n", randomImg.Name())
continue
}
//re-roll if not a recognised image type
isImage, _ := regexp.Match(".jpg$|.jpeg$|.png$|.bmp$|.webp$", []byte(randomImg.Name()))
if !isImage {
fmt.Printf("%v is not a recognised image format\n", randomImg.Name())
log.Printf("%v is not a recognised image format\n", randomImg.Name())
continue
}
//TODO: re-roll if you get the same image

View File

@ -1,7 +1,6 @@
package main
import (
"fmt"
"image"
"log"
"os"
@ -38,7 +37,7 @@ func hexToHSL(string) [3]int {
func pickRandomImage(dir string) (string, error) {
dir = config.ImagesDir + dir
fmt.Printf("getting random file from %v\n", dir)
log.Printf("getting random file from %v\n", dir)
randomImg, filename, _ := getRandomFile(dir)
filenameNoExt := strings.TrimSuffix(filename, filepath.Ext(filename))
@ -51,7 +50,7 @@ func pickRandomImage(dir string) (string, error) {
if !fileExists {
img, err := loadImage(randomImg)
if err != nil {
fmt.Printf("failed to load image %v", randomImg)
log.Printf("failed to load image %v", randomImg)
return "", err
}
dst := processImage(img, config)

18
main.go
View File

@ -16,12 +16,14 @@ func main() {
log.SetFlags(log.LstdFlags | log.Lshortfile)
err := config.load()
logfile, err := openLogfile()
if err != nil {
log.Println("failed to load config", err)
cleanExit(1)
}
log.Printf("%+v\n", config)
log.SetOutput(logfile)
log.SetFlags(log.LstdFlags | log.Lshortfile | log.Lmicroseconds)
log.Println("Welcome to GoPaper!")
var waitGroup sync.WaitGroup
waitGroup.Add(1)
@ -43,14 +45,18 @@ func main() {
if len(args) > 0 {
dir = args[0]
} else {
dir = ""
dir = config.Default
}
slideshowDir <- dir
waitGroup.Wait()
}
func cleanExit(code int) {
os.Remove(sockfile)
os.Exit(code)
func openLogfile() (*os.File, error) {
//TODO: log rotation
f, err := os.OpenFile(config.Cache+"/logfile", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
return nil, err
}
return f, nil
}