From 35b46ed9e8eff01b215a7ddd513f51139fddaa51 Mon Sep 17 00:00:00 2001 From: Seednode Date: Sat, 10 Sep 2022 18:03:04 -0500 Subject: [PATCH] Replaced log.Print with fmt.Printf to allow finer control of newlines --- cmd/version.go | 2 +- cmd/web.go | 14 +++++++++++--- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/cmd/version.go b/cmd/version.go index 6a2b188..76dbd19 100644 --- a/cmd/version.go +++ b/cmd/version.go @@ -10,7 +10,7 @@ import ( "github.com/spf13/cobra" ) -var Version = "0.4.1" +var Version = "0.4.2" func init() { rootCmd.AddCommand(versionCmd) diff --git a/cmd/web.go b/cmd/web.go index 68f9917..ee093a4 100644 --- a/cmd/web.go +++ b/cmd/web.go @@ -6,6 +6,7 @@ package cmd import ( "errors" + "fmt" "io" "log" "net/http" @@ -13,8 +14,11 @@ import ( "os" "strconv" "strings" + "time" ) +const LOGDATE string = "2006-01-02T15:04:05.000000000-07:00" + func generatePageHtml(w http.ResponseWriter, r http.Request, paths []string) error { fileList, err := getFileList(paths) if err != nil { @@ -69,7 +73,7 @@ func serveStaticFile(w http.ResponseWriter, r http.Request, paths []string) erro if matchesPrefix == false { if Verbose { - log.Println("Failed to serve file outside specified path(s): " + filePath) + fmt.Printf("%v Failed to serve file outside specified path(s): %v", time.Now().Format(LOGDATE), filePath) } http.NotFound(w, &r) @@ -80,7 +84,7 @@ func serveStaticFile(w http.ResponseWriter, r http.Request, paths []string) erro _, err = os.Stat(filePath) if errors.Is(err, os.ErrNotExist) { if Verbose { - log.Println("Failed to serve non-existent file: " + filePath) + fmt.Printf("%v Failed to serve non-existent file: %v", time.Now().Format(LOGDATE), filePath) } http.NotFound(w, &r) @@ -90,6 +94,10 @@ func serveStaticFile(w http.ResponseWriter, r http.Request, paths []string) erro return err } + if Verbose { + fmt.Printf("%v Serving file: %v", time.Now().Format(LOGDATE), filePath) + } + buf, err := os.ReadFile(filePath) if err != nil { return err @@ -98,7 +106,7 @@ func serveStaticFile(w http.ResponseWriter, r http.Request, paths []string) erro w.Write(buf) if Verbose { - log.Println("Served file: " + filePath) + fmt.Println(" - DONE") } return nil