1

log: move log file to temp directory with fallback

This commit is contained in:
2026-06-19 21:39:45 +03:00
parent c3cb8ae847
commit d0cafe1332
2 changed files with 8 additions and 4 deletions
+1 -1
View File
@@ -23,7 +23,7 @@ autopriority [flags]
| `-mem` | 536870912 | Memory threshold in bytes | | `-mem` | 536870912 | Memory threshold in bytes |
| `-interval` | 1m0s | Scan interval (min 10s) | | `-interval` | 1m0s | Scan interval (min 10s) |
| `-dry-run` | false | Log only, don't change priority | | `-dry-run` | false | Log only, don't change priority |
| `-log` | true | Write `autopriority.log` file | | `-log` | true | Write `autopriority.log` to `%TEMP%` |
## Auto-start ## Auto-start
+6 -2
View File
@@ -161,7 +161,7 @@ func main() {
mem := flag.Uint64("mem", 512*1024*1024, "memory threshold in bytes") mem := flag.Uint64("mem", 512*1024*1024, "memory threshold in bytes")
interval := flag.Duration("interval", time.Minute, "scan interval") interval := flag.Duration("interval", time.Minute, "scan interval")
dryRun := flag.Bool("dry-run", false, "log only, do not change priorities") 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() flag.Parse()
if *interval < 10*time.Second { if *interval < 10*time.Second {
@@ -170,8 +170,12 @@ func main() {
var logFile *os.File var logFile *os.File
if *doLog { if *doLog {
logDir := os.TempDir()
if logDir == "" {
exePath, _ := os.Executable() exePath, _ := os.Executable()
f, err := os.OpenFile(filepath.Join(filepath.Dir(exePath), "autopriority.log"), logDir = filepath.Dir(exePath)
}
f, err := os.OpenFile(filepath.Join(logDir, "autopriority.log"),
os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644) os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644)
if err == nil { if err == nil {
logFile = f logFile = f