diff --git a/log/log.go b/log/log.go index 09d2acd..c2b5a43 100644 --- a/log/log.go +++ b/log/log.go @@ -12,7 +12,7 @@ type Logger struct { } var ( - Log *Logger + Log *Logger path string ) @@ -22,7 +22,7 @@ func Open(name, logLevelStr string, prio syslog.Priority) *Logger { log.Fatal("Invalid log level specified") } - Log = &Logger { logging.MustGetLogger(name) } + Log = &Logger{logging.MustGetLogger(name)} var formatStdout = logging.MustStringFormatter( "%{color}%{time:2006-01-02T15:04:05.000} %{shortfunc} ▶ %{level:.4s} %{color:reset} %{message}", @@ -51,4 +51,18 @@ func Reopen() { Log.Notice("Reopened log file per IPC request") } +// If there are any existing fd's (e.g. we're reopening logs), we rely +// on garbage collection to clean them up for us. +func LogRedirectStdOutToFile(logPath string) { + path = logPath + if logPath == "" { + Log.Fatal("Log Path not set") + } + logFile, err := os.OpenFile(logPath, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0644) + if err != nil { + Log.Fatal(err) + } + + stdFdToLogFile(int(logFile.Fd())) +} diff --git a/log/log_redirect.go b/log/log_redirect.go index d0573d1..b6da910 100644 --- a/log/log_redirect.go +++ b/log/log_redirect.go @@ -2,24 +2,9 @@ package log -import ( - "os" - "syscall" -) +import "syscall" -// If there are any existing fd's (e.g. we're reopening logs), we rely -// on garbage collection to clean them up for us. -func LogRedirectStdOutToFile(logPath string) { - path = logPath - if logPath == "" { - Log.Fatal("Log Path not set") - } - - logFile, err := os.OpenFile(logPath, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0644) - if err != nil { - Log.Fatal(err) - } - - syscall.Dup2(int(logFile.Fd()), 1) - syscall.Dup2(int(logFile.Fd()), 2) -} \ No newline at end of file +func stdFdToLogFile(logFileFd int) { + syscall.Dup2(logFileFd, 1) + syscall.Dup2(logFileFd, 2) +} diff --git a/log/log_redirect_arm64.go b/log/log_redirect_arm64.go index bb6a66f..3aeaaaf 100644 --- a/log/log_redirect_arm64.go +++ b/log/log_redirect_arm64.go @@ -1,23 +1,8 @@ package log -import ( - "os" - "syscall" -) +import "syscall" -// If there are any existing fd's (e.g. we're reopening logs), we rely -// on garbage collection to clean them up for us. -func LogRedirectStdOutToFile(logPath string) { - path = logPath - if logPath == "" { - Log.Fatal("Log Path not set") - } - - logFile, err := os.OpenFile(logPath, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0644) - if err != nil { - Log.Fatal(err) - } - - syscall.Dup3(int(logFile.Fd()), 1, 0) - syscall.Dup3(int(logFile.Fd()), 2, 0) -} \ No newline at end of file +func stdFdToLogFile(logFileFd int) { + syscall.Dup3(logFileFd, 1, 0) + syscall.Dup3(logFileFd, 2, 0) +}