From 47620fa680c18fcf8e712944eaea2d5b01e8d9df Mon Sep 17 00:00:00 2001 From: Gokcehan Date: Sun, 6 Feb 2022 19:43:01 +0300 Subject: [PATCH] add command line option for log file cc #739 --- app.go | 1 - client.go | 15 ++++++++++----- etc/lf.bash | 1 + etc/lf.csh | 2 +- etc/lf.fish | 1 + etc/lf.ps1 | 1 + etc/lf.zsh | 1 + gen/man.sh | 1 + lf.1 | 1 + main.go | 17 ++++++++++++++--- server.go | 12 +++++++----- 11 files changed, 38 insertions(+), 15 deletions(-) diff --git a/app.go b/app.go index a35c5ba..011eddb 100644 --- a/app.go +++ b/app.go @@ -72,7 +72,6 @@ func (app *app) quit() { } } } - os.Remove(gLogPath) } func (app *app) readFile(path string) { diff --git a/client.go b/client.go index a12b032..ae2906a 100644 --- a/client.go +++ b/client.go @@ -4,6 +4,7 @@ import ( "bufio" "fmt" "io" + "io/ioutil" "log" "net" "os" @@ -25,12 +26,16 @@ func run() { screen.EnableMouse() } - f, err := os.OpenFile(gLogPath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600) - if err != nil { - panic(err) + if gLogPath != "" { + f, err := os.OpenFile(gLogPath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600) + if err != nil { + panic(err) + } + defer f.Close() + log.SetOutput(f) + } else { + log.SetOutput(ioutil.Discard) } - defer f.Close() - log.SetOutput(f) log.Print("hi!") diff --git a/etc/lf.bash b/etc/lf.bash index d87e96e..23d3c3a 100644 --- a/etc/lf.bash +++ b/etc/lf.bash @@ -13,6 +13,7 @@ _lf () { -cpuprofile -doc -last-dir-path + -log -memprofile -remote -selection-path diff --git a/etc/lf.csh b/etc/lf.csh index ceeb2dd..21c0882 100644 --- a/etc/lf.csh +++ b/etc/lf.csh @@ -9,7 +9,7 @@ # endif # -set LF_ARGS = "-command -config -cpuprofile -doc -last-dir-path -memprofile -remote -selection-path -server -single -version -help " +set LF_ARGS = "-command -config -cpuprofile -doc -last-dir-path -log -memprofile -remote -selection-path -server -single -version -help " complete lf "C/-*/(${LF_ARGS})/" complete lfcd "C/-*/(${LF_ARGS})/" diff --git a/etc/lf.fish b/etc/lf.fish index 35b8726..4087c8f 100644 --- a/etc/lf.fish +++ b/etc/lf.fish @@ -11,6 +11,7 @@ complete -c lf -o config -r -d 'path to the config file (instead of the usual pa complete -c lf -o cpuprofile -r -d 'path to the file to write the CPU profile' complete -c lf -o doc -d 'show documentation' complete -c lf -o last-dir-path -r -d 'path to the file to write the last dir on exit (to use for cd)' +complete -c lf -o log -r -d 'path to the log file to write messages' complete -c lf -o memprofile -r -d 'path to the file to write the memory profile' complete -c lf -o remote -x -d 'send remote command to server' complete -c lf -o selection-path -r -d 'path to the file to write selected files on open (to use as open file dialog)' diff --git a/etc/lf.ps1 b/etc/lf.ps1 index 5ff575c..ed33cb0 100644 --- a/etc/lf.ps1 +++ b/etc/lf.ps1 @@ -14,6 +14,7 @@ Register-ArgumentCompleter -Native -CommandName 'lf' -ScriptBlock { [CompletionResult]::new('-cpuprofile ', '-cpuprofile', [CompletionResultType]::ParameterName, 'path to the file to write the CPU profile') [CompletionResult]::new('-doc', '-doc', [CompletionResultType]::ParameterName, 'show documentation') [CompletionResult]::new('-last-dir-path ', '-last-dir-path', [CompletionResultType]::ParameterName, 'path to the file to write the last dir on exit (to use for cd)') + [CompletionResult]::new('-log ', '-log', [CompletionResultType]::ParameterName, 'path to the log file to write messages') [CompletionResult]::new('-memprofile ', '-memprofile', [CompletionResultType]::ParameterName, 'path to the file to write the memory profile') [CompletionResult]::new('-remote ', '-remote', [CompletionResultType]::ParameterName, 'send remote command to server') [CompletionResult]::new('-selection-path ', '-selection-path', [CompletionResultType]::ParameterName, 'path to the file to write selected files on open (to use as open file dialog)') diff --git a/etc/lf.zsh b/etc/lf.zsh index 12d6e5b..54cec9f 100644 --- a/etc/lf.zsh +++ b/etc/lf.zsh @@ -18,6 +18,7 @@ arguments=( '-cpuprofile[path to the file to write the CPU profile]' '-doc[show documentation]' '-last-dir-path[path to the file to write the last dir on exit (to use for cd)]' + '-log[path to the log file to write messages]' '-memprofile[path to the file to write the memory profile]' '-remote[send remote command to server]' '-selection-path[path to the file to write selected files on open (to use as open file dialog)]' diff --git a/gen/man.sh b/gen/man.sh index f1eda46..eed7c79 100755 --- a/gen/man.sh +++ b/gen/man.sh @@ -28,6 +28,7 @@ lf \- terminal file manager .OP \-cpuprofile path .OP \-doc .OP \-last-dir-path path +.OP \-log path .OP \-memprofile path .OP \-remote command .OP \-selection-path path diff --git a/lf.1 b/lf.1 index 49a294f..5b4efac 100644 --- a/lf.1 +++ b/lf.1 @@ -9,6 +9,7 @@ lf \- terminal file manager .OP \-cpuprofile path .OP \-doc .OP \-last-dir-path path +.OP \-log path .OP \-memprofile path .OP \-remote command .OP \-selection-path path diff --git a/main.go b/main.go index 11f677b..9867460 100644 --- a/main.go +++ b/main.go @@ -30,7 +30,6 @@ var ( gSocketProt string gSocketPath string gLogPath string - gServerLogPath string gSelect string gConfigPath string gCommands arrayFlag @@ -251,6 +250,11 @@ func main() { "command", "command to execute on client initialization") + flag.StringVar(&gLogPath, + "log", + "", + "path to the log file to write messages") + flag.Parse() gSocketProt = gDefaultSocketProt @@ -277,8 +281,15 @@ func main() { log.Fatalf("remote command: %s", err) } case *serverMode: + if gLogPath != "" && !filepath.IsAbs(gLogPath) { + wd, err := os.Getwd() + if err != nil { + log.Fatalf("getting current directory: %s", err) + } else { + gLogPath = filepath.Join(wd, gLogPath) + } + } os.Chdir(gUser.HomeDir) - gServerLogPath = filepath.Join(os.TempDir(), fmt.Sprintf("lf.%s.server.log", gUser.Username)) serve() default: gSingleMode = *singleMode @@ -288,7 +299,7 @@ func main() { } gClientID = os.Getpid() - gLogPath = filepath.Join(os.TempDir(), fmt.Sprintf("lf.%s.%d.log", gUser.Username, gClientID)) + switch flag.NArg() { case 0: _, err := os.Getwd() diff --git a/server.go b/server.go index c2a9c91..0b17221 100644 --- a/server.go +++ b/server.go @@ -16,12 +16,14 @@ var ( ) func serve() { - f, err := os.OpenFile(gServerLogPath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600) - if err != nil { - panic(err) + if gLogPath != "" { + f, err := os.OpenFile(gLogPath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600) + if err != nil { + panic(err) + } + defer f.Close() + log.SetOutput(f) } - defer f.Close() - log.SetOutput(f) log.Print("hi!")