refactor: improve log and error messages (#2708)

This commit is contained in:
CatsDeservePets authored and GitHub committed 2026-10-12 21:33:05 +02:00
1 parent 5d3c7b7025
commit 664d7c88e1
10 files changed
+55 -52

No files matched your search

+5 -2
View File
@@ -54,9 +54,11 @@ func newApp(ui *ui, nav *nav) *app {
signal.Notify(sigChan, os.Interrupt, syscall.SIGHUP, syscall.SIGQUIT, syscall.SIGTERM)
go func() {
for {
switch <-sigChan {
sig := <-sigChan
switch sig {
case os.Interrupt:
case syscall.SIGHUP, syscall.SIGQUIT, syscall.SIGTERM:
log.Printf("received termination signal: %s", sig)
app.quit()
app.ui.screen.Fini()
os.Exit(3)
@@ -537,10 +539,11 @@ func (app *app) runCmdSync(cmd *exec.Cmd, pauseAfter bool) {
app.nav.previewChan <- ""
if err := app.ui.suspend(); err != nil {
log.Printf("suspend: %s", err)
log.Printf("suspending screen: %s", err)
}
defer func() {
if err := app.ui.resume(); err != nil {
log.Printf("resuming screen: %s", err)
app.quit()
os.Exit(3)
}
+3 -3
View File
@@ -31,7 +31,7 @@ func run() {
if gLogPath != "" {
f, err := os.OpenFile(gLogPath, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0o600)
if err != nil {
log.Fatalf("failed to open log file: %s", err)
log.Fatalf("opening log file: %s", err)
}
defer f.Close()
log.SetOutput(f)
@@ -108,7 +108,7 @@ func printPath(label, path string, stdoutIsTerminal bool) {
func writeLastDir(filename, lastDir string) {
if strings.ContainsAny(lastDir, "\n\r") {
log.Printf("last-dir: path contains newline: %q", lastDir)
log.Printf("last-dir: skipping path with newline: %q", lastDir)
return
}
f, err := os.Create(filename)
@@ -153,7 +153,7 @@ func readExpr() <-chan expr {
c, err := net.Dial("unix", gSocketPath)
for err != nil {
log.Printf("connecting server: %s", err)
log.Printf("connecting to server (retrying in %s): %s", duration, err)
time.Sleep(duration)
duration *= 2
c, err = net.Dial("unix", gSocketPath)
+4 -4
View File
@@ -246,7 +246,7 @@ func matchFile(s string, dirOnly bool, escape, unescape func(string) string) (ma
}
files, err := os.ReadDir(d)
if err != nil {
log.Printf("reading directory: %s", err)
log.Printf("completing path: %s", err)
longest = s
return
}
@@ -316,7 +316,7 @@ func matchExec(s string) (matches []compMatch, longest string) {
files, err := os.ReadDir(p)
if err != nil {
if !os.IsNotExist(err) {
log.Printf("reading path: %s", err)
log.Printf("completing shell command: %s", err)
}
continue
}
@@ -328,7 +328,7 @@ func matchExec(s string) (matches []compMatch, longest string) {
finfo, err := f.Info()
if err != nil {
log.Printf("getting file information: %s", err)
log.Printf("completing shell command: %s", err)
continue
}
@@ -346,7 +346,7 @@ func matchExec(s string) (matches []compMatch, longest string) {
func matchSearch(s string) (matches []compMatch, longest string) {
files, err := os.ReadDir(".")
if err != nil {
log.Printf("reading directory: %s", err)
log.Printf("completing search: %s", err)
longest = s
return
}
+6 -6
View File
@@ -786,7 +786,7 @@ func resetIncCmd(app *app) {
dir := app.nav.currDir()
old := dir.ind
if err := app.nav.setFilter(app.nav.prevFilter); err != nil {
log.Printf("reset filter: %s", err)
log.Printf("resetting filter: %s", err)
} else if old != dir.ind {
app.ui.loadFile(app, true)
}
@@ -1011,7 +1011,7 @@ func cd(app *app, path string) error {
preChdir(app)
if err := app.nav.cd(path); err != nil {
return fmt.Errorf("changing directory: %w", err)
return err
}
app.ui.loadFile(app, true)
@@ -1109,7 +1109,7 @@ func (e *callExpr) eval(app *app, _ []string) {
preChdir(app)
err := app.nav.open()
if err != nil {
app.ui.echoerrf("opening directory: %s", err)
app.ui.echoerrf("open: %s", err)
return
}
app.ui.loadFile(app, true)
@@ -1623,7 +1623,7 @@ func (e *callExpr) eval(app *app, _ []string) {
app.ui.echoerr("push: requires an argument")
return
}
log.Println("pushing keys", e.args[0])
log.Printf("pushing keys: %s", e.args[0])
for _, val := range splitKeys(e.args[0]) {
app.ui.evChan <- parseKey(val)
}
@@ -1687,7 +1687,7 @@ func (e *callExpr) eval(app *app, _ []string) {
tty, ok := app.ui.screen.Tty()
if !ok {
log.Print("tty-write: failed to get tty")
log.Print("tty-write: no tty available")
return
}
@@ -1911,7 +1911,7 @@ func (e *callExpr) eval(app *app, _ []string) {
}
app.ui.loadFile(app, true)
default:
log.Printf("entering unknown execution prefix: %q", app.ui.cmdPrefix)
log.Printf("unknown execution prefix: %q", app.ui.cmdPrefix)
}
case "cmd-interrupt":
if app.cmd != nil {
+3 -3
View File
@@ -82,7 +82,7 @@ func exportEnvVars() {
level, err := strconv.Atoi(envLevel)
if err != nil {
log.Printf("reading lf level: %s", err)
log.Printf("parsing $LF_LEVEL: %s", err)
}
level++
@@ -183,7 +183,7 @@ func checkServer() {
startServer()
} else if _, err := net.Dial("unix", gSocketPath); err != nil {
if err := os.Remove(gSocketPath); err != nil {
log.Print(err)
log.Printf("removing socket: %s", err)
}
startServer()
}
@@ -381,7 +381,7 @@ Options:
case 1:
gSelect = flag.Arg(0)
default:
fmt.Fprintln(os.Stderr, "only single file or directory is allowed")
fmt.Fprintln(os.Stderr, "only one file or directory may be specified")
os.Exit(2)
}
+9 -9
View File
@@ -48,7 +48,7 @@ type file struct {
func newFile(path string) *file {
lstat, err := os.Lstat(path)
if err != nil {
log.Printf("getting file information: %s", err)
log.Printf("getting file info: %s", err)
return &file{
FileInfo: &fakeStat{name: filepath.Base(path)},
linkState: notLink,
@@ -100,13 +100,13 @@ func newFile(path string) *file {
if lstat.IsDir() && getDirCounts(filepath.Dir(path)) {
d, err := os.Open(path)
if err != nil {
log.Printf("opening file: %s", err)
log.Printf("counting directory entries: %s", err)
} else {
names, err := d.Readdirnames(10000)
d.Close()
if names == nil && err != io.EOF {
log.Printf("reading directory: %s", err)
log.Printf("counting directory entries: %s", err)
} else {
dirCount = len(names)
}
@@ -907,12 +907,12 @@ func (nav *nav) preview(path string, win *win, mode string) {
if _, ok := errors.AsType[*exec.ExitError](err); ok {
reg.volatile = true
} else {
log.Printf("loading file: %s", err)
log.Printf("previewing file: %s", err)
}
}
if s := strings.TrimSpace(stderr.String()); s != "" {
s = strings.Join(strings.Fields(s), " ")
log.Printf("loading file (stderr): %s", s)
log.Printf("previewing file (stderr): %s", s)
}
}()
defer out.Close()
@@ -920,7 +920,7 @@ func (nav *nav) preview(path string, win *win, mode string) {
} else {
lstat, err := os.Lstat(path)
if err != nil {
log.Printf("lstat: %s", err)
log.Printf("previewing file: %s", err)
return
}
@@ -930,7 +930,7 @@ func (nav *nav) preview(path string, win *win, mode string) {
f, err := os.Open(path)
if err != nil {
log.Printf("opening file: %s", err)
log.Printf("previewing file: %s", err)
return
}
@@ -1151,7 +1151,7 @@ func (nav *nav) open() error {
}
if err := os.Chdir(curr.path); err != nil {
return fmt.Errorf("open: %w", err)
return err
}
nav.dirPaths = append(nav.dirPaths, curr.path)
@@ -1814,7 +1814,7 @@ func isFiltered(f os.FileInfo, filter []string) bool {
for _, pattern := range filter {
matched, err := searchMatch(f.Name(), strings.TrimPrefix(pattern, "!"), gOpts.filtermethod)
if err != nil {
log.Printf("Filter Error: %s", err)
log.Printf("applying filter: %s", err)
return false
}
if strings.HasPrefix(pattern, "!") && matched {
+14 -14
View File
@@ -29,7 +29,7 @@ func serve() {
if gLogPath != "" {
f, err := os.OpenFile(gLogPath, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0o600)
if err != nil {
log.Fatalf("failed to open log file: %s", err)
log.Fatalf("opening log file: %s", err)
}
defer f.Close()
log.SetOutput(f)
@@ -73,7 +73,7 @@ func manage() {
case "broadcast":
for id, c2 := range connList {
if _, err := fmt.Fprintln(c2, cmd.msg); err != nil {
echoerrf(cmd.c, "failed to send command to client %v: %s", id, err)
echoerrf(cmd.c, "sending command to client %v: %s", id, err)
c2.Close()
delete(connList, id)
}
@@ -81,29 +81,29 @@ func manage() {
case "send":
if c2, ok := connList[cmd.id]; ok {
if _, err := fmt.Fprintln(c2, cmd.msg); err != nil {
echoerrf(cmd.c, "failed to send command to client %v: %s", cmd.id, err)
echoerrf(cmd.c, "sending command to client %v: %s", cmd.id, err)
}
} else {
echoerr(cmd.c, "listen: send: no such client id is connected")
echoerrf(cmd.c, "listen: send: no client with ID %v connected", cmd.id)
}
case "query":
c2, ok := connList[cmd.id]
if !ok {
echoerr(cmd.c, "listen: query: no such client id is connected")
echoerrf(cmd.c, "listen: query: no client with ID %v connected", cmd.id)
break
}
if _, err := fmt.Fprintln(c2, "query "+cmd.msg); err != nil {
echoerrf(cmd.c, "failed to send query to client %v: %s", cmd.id, err)
echoerrf(cmd.c, "sending query to client %v: %s", cmd.id, err)
break
}
s2 := bufio.NewScanner(c2)
for s2.Scan() && s2.Text() != "" {
if _, err := fmt.Fprintln(cmd.c, s2.Text()); err != nil {
log.Printf("failed to forward query response from client %v: %s", cmd.id, err)
log.Printf("forwarding query response from client %v: %s", cmd.id, err)
}
}
if s2.Err() != nil {
echoerrf(cmd.c, "failed to read query response from client %v: %s", cmd.id, s2.Err())
echoerrf(cmd.c, "reading query response from client %v: %s", cmd.id, s2.Err())
}
case "quit":
for id, c := range connList {
@@ -180,25 +180,25 @@ Loop:
word2, _ := splitWord(rest)
id, err := strconv.Atoi(word2)
if err != nil {
echoerr(c, "listen: conn: client id should be a number")
echoerr(c, "listen: conn: client ID must be a number")
} else {
send(srvCmd{op: "conn", id: id, c: c})
return
}
} else {
echoerr(c, "listen: conn: requires a client id")
echoerr(c, "listen: conn: requires a client ID")
}
case "drop":
if rest != "" {
word2, _ := splitWord(rest)
id, err := strconv.Atoi(word2)
if err != nil {
echoerr(c, "listen: drop: client id should be a number")
echoerr(c, "listen: drop: client ID must be a number")
} else {
send(srvCmd{op: "drop", id: id})
}
} else {
echoerr(c, "listen: drop: requires a client id")
echoerr(c, "listen: drop: requires a client ID")
}
case "list":
send(srvCmd{op: "list", c: c})
@@ -214,13 +214,13 @@ Loop:
}
case "query":
if rest == "" {
echoerr(c, "listen: query: requires a client id")
echoerr(c, "listen: query: requires a client ID")
break
}
word2, rest2 := splitWord(rest)
id, err := strconv.Atoi(word2)
if err != nil {
echoerr(c, "listen: query: client id should be a number")
echoerr(c, "listen: query: client ID must be a number")
break
}
send(srvCmd{op: "query", id: id, msg: rest2, c: c})
+4 -4
View File
@@ -200,7 +200,7 @@ loop:
case "38":
color, offset, err := parseColor(toks[i+1:])
if err != nil {
log.Printf("error processing ansi code 38: %s", err)
log.Printf("processing ANSI code 38: %s", err)
break loop
}
st = st.Foreground(color)
@@ -214,7 +214,7 @@ loop:
case "48":
color, offset, err := parseColor(toks[i+1:])
if err != nil {
log.Printf("error processing ansi code 48: %s", err)
log.Printf("processing ANSI code 48: %s", err)
break loop
}
st = st.Background(color)
@@ -222,13 +222,13 @@ loop:
case "58":
color, offset, err := parseColor(toks[i+1:])
if err != nil {
log.Printf("error processing ansi code 58: %s", err)
log.Printf("processing ANSI code 58: %s", err)
break loop
}
st = st.Underline(color)
i += offset
default:
log.Printf("unknown ansi code: %s", toks[i])
log.Printf("unknown ANSI code: %s", toks[i])
}
}
+5 -5
View File
@@ -1353,7 +1353,7 @@ func listMarks(marks map[string]string) string {
func listFilesInCurrDir(nav *nav) string {
dir := nav.currDir()
if dir.loading {
log.Printf("listFilesInCurrDir(): %s is still loading, `files` isn't ready for remote query", dir.path)
log.Printf("listFilesInCurrDir: directory contents not loaded yet: %q", dir.path)
return ""
}
@@ -1426,7 +1426,7 @@ func (ui *ui) readNormalEvent(ev tcell.Event, nav *nav) expr {
expr := keys[ui.keyAcc]
if ui.keyCount != "" {
if count, err := strconv.Atoi(ui.keyCount); err != nil {
log.Printf("converting command count: %s", err)
log.Printf("parsing command count: %s", err)
} else if count > 0 {
switch e := expr.(type) {
case *callExpr:
@@ -1540,9 +1540,9 @@ func (ui *ui) readNormalEvent(ev tcell.Event, nav *nav) expr {
case *tcell.EventResize:
return &callExpr{"redraw", nil, 1}
case *tcell.EventError:
log.Printf("Got EventError: '%s' at %s", tev.Error(), tev.When())
log.Printf("event error: %q at %s", tev.Error(), tev.When())
case *tcell.EventInterrupt:
log.Printf("Got EventInterrupt: at %s", tev.When())
log.Printf("event interrupt at %s", tev.When())
case *tcell.EventFocus:
if tev.Focused {
return &callExpr{"on-focus-gained", nil, 1}
@@ -1620,7 +1620,7 @@ func anyKey() {
b := make([]byte, 8)
if _, err := os.Stdin.Read(b); err != nil {
log.Printf("Failed to read key press: %s", err)
log.Printf("reading key press: %s", err)
}
}
+2 -2
View File
@@ -46,7 +46,7 @@ func (watch *watch) start() {
watcher, err := fsnotify.NewWatcher()
if err != nil {
log.Printf("start watcher: %s", err)
log.Printf("starting watcher: %s", err)
return
}
@@ -79,7 +79,7 @@ func (watch *watch) add(path string) {
}
if err := watch.watcher.Add(path); err != nil {
log.Printf("watch path %s: %s", path, err)
log.Printf("watching path %s: %s", path, err)
return
}