use single function that does platform specific things

This commit is contained in:
eater
2017-11-21 13:12:13 +01:00
parent dd45054b77
commit 9d3a74746f
3 changed files with 26 additions and 42 deletions

View File

@@ -12,7 +12,7 @@ type Logger struct {
} }
var ( var (
Log *Logger Log *Logger
path string path string
) )
@@ -22,7 +22,7 @@ func Open(name, logLevelStr string, prio syslog.Priority) *Logger {
log.Fatal("Invalid log level specified") log.Fatal("Invalid log level specified")
} }
Log = &Logger { logging.MustGetLogger(name) } Log = &Logger{logging.MustGetLogger(name)}
var formatStdout = logging.MustStringFormatter( var formatStdout = logging.MustStringFormatter(
"%{color}%{time:2006-01-02T15:04:05.000} %{shortfunc} ▶ %{level:.4s} %{color:reset} %{message}", "%{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") 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()))
}

View File

@@ -2,24 +2,9 @@
package log package log
import ( import "syscall"
"os"
"syscall"
)
// If there are any existing fd's (e.g. we're reopening logs), we rely func stdFdToLogFile(logFileFd int) {
// on garbage collection to clean them up for us. syscall.Dup2(logFileFd, 1)
func LogRedirectStdOutToFile(logPath string) { syscall.Dup2(logFileFd, 2)
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)
} }

View File

@@ -1,23 +1,8 @@
package log package log
import ( import "syscall"
"os"
"syscall"
)
// If there are any existing fd's (e.g. we're reopening logs), we rely func stdFdToLogFile(logFileFd int) {
// on garbage collection to clean them up for us. syscall.Dup3(logFileFd, 1, 0)
func LogRedirectStdOutToFile(logPath string) { syscall.Dup3(logFileFd, 2, 0)
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)
} }