log/util.go
2025-04-10 18:09:34 +08:00

40 lines
836 B
Go

package log
import (
"fmt"
"os"
"path/filepath"
)
// getExeFilename
func getExeFilename() string {
_, logfilename := filepath.Split(os.Args[0])
if logfilename == "" {
panic("get exe filename failed")
}
return logfilename
}
// tryOptLogDirs
func tryOptLogDirs(optLogDirs []string) (string, error) {
optionDirsStr := ""
for i, d := range optLogDirs {
dir := fmt.Sprintf("%s/", d)
optionDirsStr += fmt.Sprintf("%d.[%s];", i, dir)
if ok := canWriteByTest(dir); ok {
return dir, nil
}
}
return "", fmt.Errorf("all optional dir are not exist or writeable: %s", optionDirsStr)
}
// canWriteByTest
func canWriteByTest(dir string) bool {
testFile := filepath.Join(dir, ".write_test.log.tmp")
if err := os.WriteFile(testFile, []byte("test"), 0644); err != nil {
return false
}
os.Remove(testFile)
return true
}