change logging format
This commit is contained in:
13
main.go
13
main.go
@@ -6,6 +6,7 @@ import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"os"
|
||||
"runtime"
|
||||
|
||||
"github.com/fatih/color"
|
||||
|
||||
@@ -30,9 +31,12 @@ func init() {
|
||||
config.Docker = flag.Bool("docker", false, "Create a docker project")
|
||||
config.Lib = lib
|
||||
config.Doc = documentation
|
||||
config.Verbose = flag.Bool("verbose", false, "Enable verbose logging")
|
||||
}
|
||||
|
||||
func main() {
|
||||
var m runtime.MemStats
|
||||
|
||||
flag.Usage = func() {
|
||||
fmt.Fprintf(flag.CommandLine.Output(), "Usage: %s [options] <command> <project_dir>\n", os.Args[0])
|
||||
fmt.Fprintln(flag.CommandLine.Output(), "Commands:")
|
||||
@@ -61,6 +65,10 @@ func main() {
|
||||
color.NoColor = true
|
||||
}
|
||||
|
||||
if *config.Port == 3000 {
|
||||
ui.WARNING("Using default port, this may lead to conflicts with other services")
|
||||
}
|
||||
|
||||
args := flag.Args()
|
||||
if len(args) < 1 {
|
||||
flag.Usage()
|
||||
@@ -90,7 +98,12 @@ func main() {
|
||||
os.Exit(1)
|
||||
}
|
||||
case "run":
|
||||
ui.Log("Fes is starting")
|
||||
ui.Log("Fes version=%s, commit=%s, just started", version.VERSION, version.GetCommit())
|
||||
|
||||
runtime.ReadMemStats(&m)
|
||||
ui.Log("FRE memory usage when created %v Mb", m.TotalAlloc/1024/1024)
|
||||
|
||||
if err := server.Start(dir); err != nil {
|
||||
if errors.Is(err, os.ErrNotExist) {
|
||||
fmt.Fprintf(os.Stderr, "%s does not exist\n", dir)
|
||||
|
||||
@@ -11,6 +11,7 @@ var Port *int
|
||||
var Color *bool
|
||||
var Static *bool
|
||||
var Docker *bool
|
||||
var Verbose *bool
|
||||
|
||||
type AppConfig struct {
|
||||
App struct {
|
||||
|
||||
@@ -424,6 +424,8 @@ func Start(dir string) error {
|
||||
return ui.Error(fmt.Sprintf("failed to change directory to %s", dir), err)
|
||||
}
|
||||
|
||||
ui.Log("Running root=%s, port=%d.", filepath.Clean(dir), *config.Port)
|
||||
|
||||
cfg := parseConfig()
|
||||
notFoundData := generateNotFoundData(&cfg)
|
||||
routes := loadDirs()
|
||||
@@ -475,7 +477,8 @@ func Start(dir string) error {
|
||||
|
||||
w.Write(data)
|
||||
})
|
||||
ui.Log("Server initialized")
|
||||
|
||||
ui.Log("Ready to accept connections on http://localhost:%d", *config.Port)
|
||||
ui.Log("Ready to accept connections tcp")
|
||||
return http.ListenAndServe(fmt.Sprintf("0.0.0.0:%d", *config.Port), nil)
|
||||
}
|
||||
|
||||
@@ -45,6 +45,12 @@ func WARN(msg string, args ...any) {
|
||||
color.Magenta("%s # %s\n", formatTimestamp(), formatted)
|
||||
}
|
||||
|
||||
// Warning message (magenta)
|
||||
func WARNING(msg string, args ...any) {
|
||||
formatted := fmt.Sprintf(msg, args...)
|
||||
color.Magenta("%s # WARNING %s\n", formatTimestamp(), formatted)
|
||||
}
|
||||
|
||||
// Error message (red)
|
||||
func ERROR(msg string, args ...any) {
|
||||
formatted := fmt.Sprintf(msg, args...)
|
||||
@@ -102,3 +108,10 @@ func Fatal(msg string, err error) error {
|
||||
FATAL("%s: %v", msg, err)
|
||||
return err
|
||||
}
|
||||
|
||||
// Log on Verbose
|
||||
func LogVerbose(msg string, args ...any) {
|
||||
if *config.Verbose {
|
||||
Log(msg, args...)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,7 +9,7 @@ var gitCommit string = "devel"
|
||||
|
||||
const PROGRAM_NAME string = "fes"
|
||||
const PROGRAM_NAME_LONG string = "fes/fSD"
|
||||
const VERSION string = "0.1.0"
|
||||
const VERSION string = "0.2.0"
|
||||
|
||||
func Version() {
|
||||
fmt.Printf("%s version %s\n", PROGRAM_NAME_LONG, VERSION)
|
||||
|
||||
Reference in New Issue
Block a user