log to file

This commit is contained in:
andrzej 2024-11-04 21:13:28 +01:00
parent 568dc2b8d4
commit 09644aeaaf
3 changed files with 25 additions and 10 deletions

View File

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

View File

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

24
main.go
View File

@ -1,8 +1,7 @@
package main package main
import ( import (
"fmt" "log"
// "log"
"os" "os"
"sync" "sync"
) )
@ -12,7 +11,15 @@ var config Config
func main() { func main() {
config.load() config.load()
fmt.Printf("%+v\n", config)
//TODO: only log to file if flagged
logfile, err := openLogfile()
if err != nil {
log.Printf("error opening logfile %v: %v", config.Cache, err)
}
log.SetOutput(logfile)
log.SetFlags(log.LstdFlags | log.Lshortfile | log.Lmicroseconds)
log.Println("Welcome to GoPaper!")
var waitGroup sync.WaitGroup var waitGroup sync.WaitGroup
waitGroup.Add(1) waitGroup.Add(1)
@ -25,9 +32,18 @@ func main() {
if len(args) > 0 { if len(args) > 0 {
dir = args[0] dir = args[0]
} else { } else {
dir = "" dir = config.Default
} }
slideshowDir <- dir slideshowDir <- dir
waitGroup.Wait() waitGroup.Wait()
} }
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
}