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
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)

24
main.go
View File

@ -1,8 +1,7 @@
package main
import (
"fmt"
// "log"
"log"
"os"
"sync"
)
@ -12,7 +11,15 @@ var config Config
func main() {
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
waitGroup.Add(1)
@ -25,9 +32,18 @@ func main() {
if len(args) > 0 {
dir = args[0]
} else {
dir = ""
dir = config.Default
}
slideshowDir <- dir
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
}