From 827a816ae857bd5f8742504b4a75a256ca41bd9b Mon Sep 17 00:00:00 2001 From: theimpostor Date: Thu, 3 Aug 2023 09:09:23 -0500 Subject: fix logging --- main.go | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/main.go b/main.go index 85ae9d0..52d4f5e 100644 --- a/main.go +++ b/main.go @@ -57,7 +57,7 @@ func closetty(tty tcell.Tty) { tty.Close() } -func initLogging() { +func initLogging() (logfile *os.File) { var err error logLevel := &slog.LevelVar{} // INFO logOutput := os.Stdout @@ -66,7 +66,7 @@ func initLogging() { if logOutput, err = os.OpenFile(logfileFlag, os.O_APPEND|os.O_RDWR|os.O_CREATE, 0644); err != nil { log.Fatalf("Failed to open file %v: %v", logfileFlag, err) } else { - defer logOutput.Close() + logfile = logOutput } } @@ -80,6 +80,8 @@ func initLogging() { slog.SetDefault(logger) slog.Debug("logging started") + + return } func identifyTerm() { @@ -215,6 +217,12 @@ func paste() error { return nil } +func closeSilently(f *os.File) { + if f != nil { + f.Close() + } +} + var copyCmd = &cobra.Command{ Use: "copy", Short: "Copies input to the system clipboard", @@ -224,7 +232,8 @@ osc copy [file1 [...fileN]] With no arguments, will read from stdin.`, RunE: func(cmd *cobra.Command, args []string) error { - initLogging() + logfile := initLogging() + defer closeSilently(logfile) identifyTerm() return copy(args) }, @@ -237,7 +246,8 @@ var pasteCmd = &cobra.Command{ osc paste`, RunE: func(cmd *cobra.Command, args []string) error { - initLogging() + logfile := initLogging() + defer closeSilently(logfile) identifyTerm() return paste() }, -- cgit v1.2.3