updated logging

This commit is contained in:
2026-01-01 23:05:29 -05:00
parent 9364df2645
commit bedcfe781d
4 changed files with 96 additions and 54 deletions

14
main.go
View File

@@ -13,6 +13,7 @@ import (
"fes/modules/doc" "fes/modules/doc"
"fes/modules/new" "fes/modules/new"
"fes/modules/server" "fes/modules/server"
"fes/modules/ui"
"fes/modules/version" "fes/modules/version"
) )
@@ -34,13 +35,13 @@ func init() {
func main() { func main() {
flag.Usage = func() { flag.Usage = func() {
fmt.Fprintf(flag.CommandLine.Output(), "Usage: %s [options] <command> <project_dir>\n", os.Args[0]) fmt.Fprintf(flag.CommandLine.Output(), "Usage: %s [options] <command> <project_dir>\n", os.Args[0])
fmt.Fprintf(os.Stderr, "Commands:") fmt.Fprintln(flag.CommandLine.Output(), "Commands:")
fmt.Fprintf(os.Stderr, " new <project_dir> Create a new project") fmt.Fprintln(flag.CommandLine.Output(), " new <project_dir> Create a new project")
fmt.Fprintf(os.Stderr, " doc Open documentation") fmt.Fprintln(flag.CommandLine.Output(), " doc Open documentation")
fmt.Fprintf(os.Stderr, " run <project_dir> Start the server") fmt.Fprintln(flag.CommandLine.Output(), " run <project_dir> Start the server")
fmt.Fprintf(os.Stderr, "Options:") fmt.Fprintln(flag.CommandLine.Output(), "Options:")
flag.PrintDefaults() flag.PrintDefaults()
fmt.Fprintf(os.Stderr, "For bug reports, contact a developer and describe the issue. Provide the output of the `-V1` flag.") fmt.Fprintln(flag.CommandLine.Output(), "For bug reports, contact a developer and describe the issue. Provide the output of the `-V1` flag.")
} }
showVersion := flag.Bool("version", false, "Show version and exit") showVersion := flag.Bool("version", false, "Show version and exit")
@@ -89,6 +90,7 @@ func main() {
os.Exit(1) os.Exit(1)
} }
case "run": case "run":
ui.Log("Fes version=%s, commit=%s, just started", version.VERSION, version.GetCommit())
if err := server.Start(dir); err != nil { if err := server.Start(dir); err != nil {
if errors.Is(err, os.ErrNotExist) { if errors.Is(err, os.ErrNotExist) {
fmt.Fprintf(os.Stderr, "%s does not exist\n", dir) fmt.Fprintf(os.Stderr, "%s does not exist\n", dir)

View File

@@ -1,11 +1,10 @@
package server package server
import ( import (
"errors"
"fes/modules/config" "fes/modules/config"
"fes/modules/ui" "fes/modules/ui"
"fmt" "fmt"
"github.com/pelletier/go-toml/v2"
lua "github.com/yuin/gopher-lua"
"html/template" "html/template"
"io/fs" "io/fs"
"net/http" "net/http"
@@ -15,6 +14,9 @@ import (
"sort" "sort"
"strings" "strings"
"time" "time"
"github.com/pelletier/go-toml/v2"
lua "github.com/yuin/gopher-lua"
) )
/* this is the request data we pass over the bus to the application, via the fes.bus interface */ /* this is the request data we pass over the bus to the application, via the fes.bus interface */
@@ -378,18 +380,26 @@ func loadDirs() map[string]string {
/* helper to parse the Fes.toml and generate config */ /* helper to parse the Fes.toml and generate config */
func parseConfig() config.AppConfig { func parseConfig() config.AppConfig {
defaultCfg := config.AppConfig{}
defaultCfg.App.Authors = []string{"unknown"}
defaultCfg.App.Name = "unknown"
defaultCfg.App.Version = "unknown"
tomlDocument, err := os.ReadFile("Fes.toml") tomlDocument, err := os.ReadFile("Fes.toml")
if err != nil { if err != nil {
if errors.Is(err, os.ErrNotExist) {
ui.WARN("no config file found, using the default config. In order to specify a config file write to Fes.toml")
return defaultCfg
} else {
ui.Error("failed to read Fes.toml", err) ui.Error("failed to read Fes.toml", err)
os.Exit(1) os.Exit(1)
} }
}
docStr := fixMalformedToml(string(tomlDocument)) docStr := fixMalformedToml(string(tomlDocument))
var cfg config.AppConfig var cfg config.AppConfig
if err := toml.Unmarshal([]byte(docStr), &cfg); err != nil { if err := toml.Unmarshal([]byte(docStr), &cfg); err != nil {
ui.Warning("failed to parse Fes.toml", err) ui.Warning("failed to parse Fes.toml", err)
cfg.App.Authors = []string{"unknown"} cfg = defaultCfg
cfg.App.Name = "unknown"
cfg.App.Version = "unknown"
} }
return cfg return cfg
} }
@@ -466,6 +476,6 @@ func Start(dir string) error {
w.Write(data) w.Write(data)
}) })
fmt.Printf("Server is running on http://localhost:%d\n", *config.Port) ui.Log("Ready to accept connections on http://localhost:%d", *config.Port)
return http.ListenAndServe(fmt.Sprintf("0.0.0.0:%d", *config.Port), nil) return http.ListenAndServe(fmt.Sprintf("0.0.0.0:%d", *config.Port), nil)
} }

View File

@@ -4,75 +4,101 @@ import (
"errors" "errors"
"fmt" "fmt"
"strings" "strings"
"time"
"fes/modules/config" "fes/modules/config"
"fes/modules/version"
"github.com/fatih/color" "github.com/fatih/color"
) )
const ( const (
hint_color = 0xbda02a hintColor = 0xbda02a
) )
/* print out the current path (route) and relevant error */ func formatTimestamp() string {
func Path(path string, err error) { return time.Now().Format("02 Jan 2006 15:04")
path = strings.TrimPrefix(path, "/")
if path == "" {
path = "(null)"
} }
fmt.Printf(" > %s ", path) func logMessage(prefix string, msg string, args ...any) {
if err == nil { formatted := fmt.Sprintf(msg, args...)
OK("ok") if prefix == "" {
return fmt.Printf("%s * %s\n", formatTimestamp(), formatted)
} else if errors.Is(err, config.ErrRouteMiss) {
WARN(config.ErrRouteMiss.Error())
} else { } else {
ERROR("bad") fmt.Printf("%s * %s: %s\n", formatTimestamp(), prefix, formatted)
} }
} }
/* print general system warning */ // Generic log
func Warning(msg string, err error) error { func Log(msg string, args ...any) {
fmt.Printf("%s: %s: %v\n", version.PROGRAM_NAME, color.MagentaString("warning"), err) logMessage("", msg, args...)
return err
} }
/* print general system error */ // OK message (green)
func Error(msg string, err error) error { func OK(msg string, args ...any) {
fmt.Printf("%s: %s: %v\n", version.PROGRAM_NAME, color.RedString("error"), err) formatted := fmt.Sprintf(msg, args...)
return err color.Green("%s * %s\n", formatTimestamp(), formatted)
} }
/* print fatality and panic */ // Warning message (magenta)
func Fatal(msg string, err error) error { func WARN(msg string, args ...any) {
fmt.Printf("%s: %s: %v\n", version.PROGRAM_NAME, color.RedString("fatal"), err) formatted := fmt.Sprintf(msg, args...)
panic(err) color.Magenta("%s # %s\n", formatTimestamp(), formatted)
} }
/* print a useful hint to the user */ // Error message (red)
func Hint(format string, args ...any) { func ERROR(msg string, args ...any) {
formatted := fmt.Sprintf(msg, args...)
color.Red("%s ! %s\n", formatTimestamp(), formatted)
}
// Fatal message and panic
func FATAL(msg string, args ...any) {
formatted := fmt.Sprintf(msg, args...)
color.Red("%s % %s\n", formatTimestamp(), formatted)
panic(formatted)
}
// Hint message (custom color)
func Hint(msg string, args ...any) {
formatted := fmt.Sprintf(msg, args...)
color.RGB(func(hex int) (r, g, b int) { color.RGB(func(hex int) (r, g, b int) {
r = (hex >> 16) & 0xFF r = (hex >> 16) & 0xFF
g = (hex >> 8) & 0xFF g = (hex >> 8) & 0xFF
b = hex & 0xFF b = hex & 0xFF
return return
}(hint_color)).Printf("hint: %s\n", fmt.Sprintf(format, args...)) }(hintColor)).Printf("%s * hint: %s\n", formatTimestamp(), formatted)
} }
/* print message using the ok status color */ // Path logging: prints route and status
func OK(msg string) { func Path(path string, err error) {
color.Green(msg) path = strings.TrimPrefix(path, "/")
if path == "" {
path = "(null)"
} }
/* print message using the warning status color */ if err == nil {
func WARN(msg string) { OK("Route: %s - ok", path)
color.Magenta(msg) } else if errors.Is(err, config.ErrRouteMiss) {
WARN("Route: %s - %s", path, config.ErrRouteMiss.Error())
} else {
ERROR("Route: %s - bad", path)
}
} }
/* print message using the error status color */ // System warning with prefix
func ERROR(msg string) { func Warning(msg string, err error) error {
color.Red(msg) WARN("%s: %v", msg, err)
return err
}
// System error with prefix
func Error(msg string, err error) error {
ERROR("%s: %v", msg, err)
return err
}
// Fatal system error
func Fatal(msg string, err error) error {
FATAL("%s: %v", msg, err)
return err
} }

View File

@@ -20,3 +20,7 @@ func FullVersion() {
fmt.Printf("%s+%s\n", VERSION, gitCommit) fmt.Printf("%s+%s\n", VERSION, gitCommit)
os.Exit(0) os.Exit(0)
} }
func GetCommit() string {
return gitCommit
}