From e599b5583dd821ed26bdfc01f7f67d4a317b9843 Mon Sep 17 00:00:00 2001 From: Ophestra Date: Sun, 16 Feb 2025 17:26:09 +0900 Subject: fmsg: implement suspend in writer This removes the requirement to call fmsg.Exit on every exit path, and enables direct use of the "log" package. However, fmsg.BeforeExit is still encouraged when possible to catch exit on suspended output. Signed-off-by: Ophestra --- parse.go | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) (limited to 'parse.go') diff --git a/parse.go b/parse.go index 85dbfca9..7212c92b 100644 --- a/parse.go +++ b/parse.go @@ -4,6 +4,7 @@ import ( "encoding/json" "errors" "io" + "log" "os" "strconv" "strings" @@ -21,11 +22,10 @@ func tryPath(name string) (config *fst.Config) { if name != "-" { r = tryFd(name) if r == nil { - fmsg.VPrintln("load configuration from file") + fmsg.Verbose("load configuration from file") if f, err := os.Open(name); err != nil { - fmsg.Fatalf("cannot access configuration file %q: %s", name, err) - panic("unreachable") + log.Fatalf("cannot access configuration file %q: %s", name, err) } else { // finalizer closes f r = f @@ -33,7 +33,7 @@ func tryPath(name string) (config *fst.Config) { } else { defer func() { if err := r.(io.ReadCloser).Close(); err != nil { - fmsg.Printf("cannot close config fd: %v", err) + log.Printf("cannot close config fd: %v", err) } }() } @@ -42,8 +42,7 @@ func tryPath(name string) (config *fst.Config) { } if err := json.NewDecoder(r).Decode(&config); err != nil { - fmsg.Fatalf("cannot load configuration: %v", err) - panic("unreachable") + log.Fatalf("cannot load configuration: %v", err) } return @@ -51,7 +50,7 @@ func tryPath(name string) (config *fst.Config) { func tryFd(name string) io.ReadCloser { if v, err := strconv.Atoi(name); err != nil { - fmsg.VPrintf("name cannot be interpreted as int64: %v", err) + fmsg.Verbosef("name cannot be interpreted as int64: %v", err) return nil } else { fd := uintptr(v) @@ -59,7 +58,7 @@ func tryFd(name string) io.ReadCloser { if errors.Is(errno, syscall.EBADF) { return nil } - fmsg.Fatalf("cannot get fd %d: %v", fd, errno) + log.Fatalf("cannot get fd %d: %v", fd, errno) } return os.NewFile(fd, strconv.Itoa(v)) } @@ -83,11 +82,11 @@ func tryShort(name string) (config *fst.Config, instance *state.State) { // try to match from state store if likePrefix && len(name) >= 8 { - fmsg.VPrintln("argument looks like prefix") + fmsg.Verbose("argument looks like prefix") s := state.NewMulti(sys.Paths().RunDirPath) if entries, err := state.Join(s); err != nil { - fmsg.Printf("cannot join store: %v", err) + log.Printf("cannot join store: %v", err) // drop to fetch from file } else { for id := range entries { @@ -99,7 +98,7 @@ func tryShort(name string) (config *fst.Config, instance *state.State) { break } - fmsg.VPrintf("instance %s skipped", v) + fmsg.Verbosef("instance %s skipped", v) } } } -- cgit v1.3.1