feat: extensive debug logging + file storage

See decentral1se/distribusi-go#7
This commit is contained in:
decentral1se 2022-02-04 20:31:37 +01:00
parent a8f3a0862e
commit 91019f197f
No known key found for this signature in database
GPG Key ID: 03789458B3D0C410
2 changed files with 65 additions and 6 deletions

View File

@ -112,7 +112,12 @@ If you want to ignore certain paths, you can use `-i/--ignore`:
It supports a list of patterns in a comma separated list, wildcards work, more [on pkg.go.dev].
If you run into issues, run with `-d/--debug` to see what is happening under the hood.
If you run into issues, run with `-d/--debug`. All debug logs are stored in a time stamped log file under `/tmp/`.
```bash
➜ ls /tmp/ | grep -i distribusi
distribusi-go-20-25-492637114356
```
:v:

View File

@ -8,6 +8,7 @@ import (
"encoding/base64"
"fmt"
"image/png"
"io"
"io/fs"
"io/ioutil"
"net/http"
@ -17,6 +18,7 @@ import (
"path/filepath"
"sort"
"strings"
"time"
logrusStack "github.com/Gurpartap/logrus-stack"
"github.com/barasher/go-exiftool"
@ -178,7 +180,19 @@ Example:
if c.Bool("debug") {
logrus.SetLevel(logrus.DebugLevel)
logrus.SetFormatter(&logrus.TextFormatter{})
logrus.SetOutput(os.Stderr)
logFile, err := getLogFile()
if err != nil {
logrus.Fatalf("unable to set up a log file, saw: %s", err)
}
logrus.SetOutput(io.MultiWriter(os.Stderr, logFile))
logrus.RegisterExitHandler(func() {
if logFile == nil {
return
}
logFile.Close()
})
logrus.AddHook(logrusStack.StandardHook())
}
@ -190,6 +204,8 @@ Example:
logrus.Fatal(err)
}
logrus.Debugf("selecting %s as distribusi root", root)
var ignore []string
if c.String("ignore") != "" {
ignore = strings.Split(c.String("ignore"), ",")
@ -217,21 +233,25 @@ Example:
exiftoolInstalled = false
}
logrus.Debugf("selecting %s as distribusi root", root)
ch := make(chan error, 2)
go func() {
logrus.Debug("distribuis commencing generation...")
if err := distribusify(c, root, ignore); err != nil {
ch <- err
return
}
logrus.Debug("distribusi finished generation!")
ch <- nil
return
}()
if c.Bool("serve") {
go func() {
logrus.Debug("starting up web server")
if err := serveHTTP(root); err != nil {
ch <- err
return
@ -283,7 +303,8 @@ func distribusify(c *cli.Context, root string, ignore []string) error {
absPath, err := filepath.Abs(fpath)
if err != nil {
return err
logrus.Debugf("unable to read %s", absPath)
return nil
}
contents, err := ioutil.ReadDir(absPath)
@ -297,15 +318,19 @@ func distribusify(c *cli.Context, root string, ignore []string) error {
dirs = append(dirs, path.Join(absPath, content.Name()))
} else {
if content.Name() == "index.html" {
file, err := os.ReadFile(path.Join(absPath, content.Name()))
indexPath := path.Join(absPath, content.Name())
file, err := os.ReadFile(indexPath)
if err != nil {
logrus.Debugf("unable to read %s, skipping", content.Name())
continue
}
if strings.Contains(string(file), generatedInDistribusi) {
logrus.Debugf("%s was not generated by distribusi, skipping", indexPath)
continue
}
}
files = append(files, path.Join(absPath, content.Name()))
}
}
@ -477,6 +502,12 @@ func getCaption(c *cli.Context, fpath string) (string, error) {
}
}
if caption != "" {
logrus.Debug("retrieved caption %s from %s", caption, fpath)
} else {
logrus.Debug("no comment retrieved for %s", fpath)
}
return caption, nil
}
@ -569,6 +600,8 @@ func getHref(c *cli.Context, fpath string, mtype string) (bool, string, error) {
href = fmt.Sprintf("<a class='%s' href=\"%s\">%s</a>", stype, fname, fname)
}
logrus.Debugf("generated href for %s: %s", fname, href)
return unknown, href, nil
}
@ -594,6 +627,8 @@ func mkDiv(c *cli.Context, mtype string, href, fname string, unknown bool) (stri
}
}
logrus.Debugf("generated div wrapper for %s: %s", fname, div)
return div, nil
}
@ -613,6 +648,8 @@ func writeIndex(fpath string, html []string, styles string) error {
return nil
}
logrus.Debugf("loading custom styles from %s", absPath)
body = fmt.Sprintf(htmlBody, generatedInDistribusi, contents, strings.Join(html, "\n"))
}
}
@ -670,6 +707,7 @@ func wipeGeneratedFiles(dir string) error {
if err := removeIndex(fpath); err != nil {
return err
}
logrus.Debugf("wiping %s as requested", fpath)
}
return nil
}); err != nil {
@ -704,3 +742,19 @@ func shouldSkip(c *cli.Context, fpath string, ignore []string) (bool, error) {
return false, nil
}
// getLogFile creates a new log file for debug output.
func getLogFile() (*os.File, error) {
cTime := time.Now()
timeNow := fmt.Sprintf("%v-%v-%v", cTime.Hour(), cTime.Minute(), cTime.Second())
prefix := fmt.Sprintf("distribusi-go-%s", timeNow)
file, err := ioutil.TempFile("/tmp", prefix)
if err != nil {
return nil, err
}
logrus.Debugf("creating %s as debug log file", file.Name())
return file, nil
}