all repos — website @ f628605bcd0c3f7fbd45a09bfebe4c7b44bc743a

My website

add debug logging
Alan Pearce alan@alanpearce.eu
Tue, 16 Apr 2024 20:33:57 +0200
commit

f628605bcd0c3f7fbd45a09bfebe4c7b44bc743a

parent

00557c355c12458e65ae5cd9e0fa5ebfba229b3a

2 files changed, 17 insertions(+), 2 deletions(-)

jump to
M cmd/build/main.gocmd/build/main.go
@@ -92,6 +92,7 @@ pathFromRoot := filepath.Join(subdir, f.Name()) 		if !f.IsDir() && path.Ext(pathFromRoot) == ".md" {
 			output := outputReplacer.Replace(pathFromRoot)
 			url := urlReplacer.Replace(pathFromRoot)
+			slog.Debug("reading post", "post", pathFromRoot)
 			matter, content, err := getPost(pathFromRoot)
 			if err != nil {
 				return nil, nil, err
@@ -102,6 +103,7 @@ tags.Add(strings.ToLower(tag)) 			}
 
 			var buf bytes.Buffer
+			slog.Debug("rendering markdown in post", "post", pathFromRoot)
 			if err := markdown.Convert(*content, &buf); err != nil {
 				return nil, nil, errors.WithMessage(err, "could not convert markdown content")
 			}
@@ -134,6 +136,7 @@ func purgeCSS(htmlFilename string, cssFilename string) (string, error) { 	if purgedCSS == nil {
 		purgedCSS = make(map[string]string)
 	} else if purgedCSS[htmlFilename] == "" {
+		slog.Debug("running purgecss", "html", htmlFilename, "css", cssFilename)
 		bytes, err := exec.Command("bun", "./node_modules/.bin/purgecss", "--css", cssFilename, "--content", htmlFilename).Output()
 		if err != nil {
 			return "", errors.WithMessage(err, "failed running `purgecss` command")
@@ -428,6 +431,7 @@ outDir := "new" 	if err := mkdirp(outDir, "post"); err != nil {
 		return errors.WithMessage(err, "could not create post output directory")
 	}
+	slog.Debug("reading posts")
 	posts, tags, err := readPosts("content", "post", outDir)
 	if err != nil {
 		return err
@@ -437,6 +441,7 @@ for _, post := range posts { 		if err := mkdirp(outDir, "post", post.Basename); err != nil {
 			return errors.WithMessage(err, "could not create directory for post")
 		}
+		slog.Debug("rendering post", "post", post.Basename)
 		output, err := renderPost(post, *config)
 		if err != nil {
 			return errors.WithMessagef(err, "could not render post %s", post.Input)
@@ -449,6 +454,7 @@ 	if err := mkdirp(outDir, "tags"); err != nil {
 		return errors.WithMessage(err, "could not create directory for tags")
 	}
+	slog.Debug("rendering tags list")
 	output, err := renderTags(tags, *config)
 	if err != nil {
 		return errors.WithMessage(err, "could not render tags")
@@ -467,6 +473,7 @@ } 		if err := mkdirp(outDir, "tags", tag); err != nil {
 			return errors.WithMessage(err, "could not create directory")
 		}
+		slog.Debug("rendering tags page", "tag", tag)
 		output, err := renderListPage(tag, *config, matchingPosts)
 		if err != nil {
 			return errors.WithMessage(err, "could not render tag page")
@@ -475,6 +482,7 @@ if err := outputToFile(output, outDir, "tags", tag, "index.html"); err != nil { 			return err
 		}
 
+		slog.Debug("rendering tags feed", "tag", tag)
 		output, err = renderFeed(fmt.Sprintf("%s - %s", config.Title, tag), *config, matchingPosts, tag)
 		if err != nil {
 			return errors.WithMessage(err, "could not render tag feed page")
@@ -483,6 +491,8 @@ if err := outputToFile(output, outDir, "tags", tag, "atom.xml"); err != nil { 			return err
 		}
 	}
+
+	slog.Debug("rendering list page")
 	listPage, err := renderListPage("", *config, posts)
 	if err != nil {
 		return errors.WithMessage(err, "could not render list page")
@@ -491,6 +501,7 @@ if err := outputToFile(listPage, outDir, "post", "index.html"); err != nil { 		return err
 	}
 
+	slog.Debug("rendering feed")
 	feed, err := renderFeed(config.Title, *config, posts, "feed")
 	if err != nil {
 		return errors.WithMessage(err, "could not render feed")
@@ -499,6 +510,7 @@ if err := outputToFile(feed, outDir, "atom.xml"); err != nil { 		return err
 	}
 
+	slog.Debug("rendering feed styles")
 	feedStyles, err := renderFeedStyles()
 	if err != nil {
 		return errors.WithMessage(err, "could not render feed styles")
@@ -507,6 +519,7 @@ if err := outputToFile(feedStyles, outDir, "feed-styles.xsl"); err != nil { 		return err
 	}
 
+	slog.Debug("rendering homepage")
 	homePage, err := renderHomepage(*config, posts)
 	if err != nil {
 		return errors.WithMessage(err, "could not render homepage")
@@ -515,6 +528,7 @@ if err := outputToFile(homePage, outDir, "index.html"); err != nil { 		return err
 	}
 
+	slog.Debug("rendering 404 page")
 	notFound, err := render404(*config)
 	if err != nil {
 		return errors.WithMessage(err, "could not render 404 page")
@@ -527,7 +541,7 @@ return nil }
 
 func main() {
-	if true {
+	if os.Getenv("DEBUG") != "" {
 		slog.SetLogLoggerLevel(slog.LevelDebug)
 	}
 	slog.Debug("starting build process")
@@ -536,7 +550,6 @@ if err != nil { 		log.Panic(errors.Errorf("working directory does not exist: %v", err))
 	}
 
-	// log.SetFlags(log.Lshortfile)
 	if err := build(); err != nil {
 		switch cause := errors.Cause(err).(type) {
 		case *fs.PathError:
M internal/config/config.gointernal/config/config.go
@@ -2,6 +2,7 @@ package config 
 import (
 	"io/fs"
+	"log/slog"
 
 	"github.com/BurntSushi/toml"
 	"github.com/pkg/errors"
@@ -35,6 +36,7 @@ } 
 func GetConfig() (*Config, error) {
 	config := Config{}
+	slog.Debug("reading config.toml")
 	_, err := toml.DecodeFile("config.toml", &config)
 	if err != nil {
 		var pathError *fs.PathError