log: move log file to temp directory with fallback
This commit is contained in:
@@ -23,7 +23,7 @@ autopriority [flags]
|
||||
| `-mem` | 536870912 | Memory threshold in bytes |
|
||||
| `-interval` | 1m0s | Scan interval (min 10s) |
|
||||
| `-dry-run` | false | Log only, don't change priority |
|
||||
| `-log` | true | Write `autopriority.log` file |
|
||||
| `-log` | true | Write `autopriority.log` to `%TEMP%` |
|
||||
|
||||
## Auto-start
|
||||
|
||||
|
||||
@@ -161,7 +161,7 @@ func main() {
|
||||
mem := flag.Uint64("mem", 512*1024*1024, "memory threshold in bytes")
|
||||
interval := flag.Duration("interval", time.Minute, "scan interval")
|
||||
dryRun := flag.Bool("dry-run", false, "log only, do not change priorities")
|
||||
doLog := flag.Bool("log", true, "write log file next to executable")
|
||||
doLog := flag.Bool("log", true, "write log file to temp directory")
|
||||
flag.Parse()
|
||||
|
||||
if *interval < 10*time.Second {
|
||||
@@ -170,8 +170,12 @@ func main() {
|
||||
|
||||
var logFile *os.File
|
||||
if *doLog {
|
||||
exePath, _ := os.Executable()
|
||||
f, err := os.OpenFile(filepath.Join(filepath.Dir(exePath), "autopriority.log"),
|
||||
logDir := os.TempDir()
|
||||
if logDir == "" {
|
||||
exePath, _ := os.Executable()
|
||||
logDir = filepath.Dir(exePath)
|
||||
}
|
||||
f, err := os.OpenFile(filepath.Join(logDir, "autopriority.log"),
|
||||
os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644)
|
||||
if err == nil {
|
||||
logFile = f
|
||||
|
||||
Reference in New Issue
Block a user