add command line option for log file

cc #739
This commit is contained in:
Gokcehan 2022-02-06 19:43:01 +03:00
parent 5d393da572
commit 47620fa680
11 changed files with 38 additions and 15 deletions

1
app.go
View File

@ -72,7 +72,6 @@ func (app *app) quit() {
}
}
}
os.Remove(gLogPath)
}
func (app *app) readFile(path string) {

View File

@ -4,6 +4,7 @@ import (
"bufio"
"fmt"
"io"
"io/ioutil"
"log"
"net"
"os"
@ -25,12 +26,16 @@ func run() {
screen.EnableMouse()
}
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)
}
log.Print("hi!")

View File

@ -13,6 +13,7 @@ _lf () {
-cpuprofile
-doc
-last-dir-path
-log
-memprofile
-remote
-selection-path

View File

@ -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})/"

View File

@ -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)'

View File

@ -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)')

View File

@ -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)]'

View File

@ -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

1
lf.1
View File

@ -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

17
main.go
View File

@ -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()

View File

@ -16,12 +16,14 @@ var (
)
func serve() {
f, err := os.OpenFile(gServerLogPath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
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)
}
log.Print("hi!")