From dd45054b77f9c98b150889fd27682f05753db07a Mon Sep 17 00:00:00 2001 From: eater Date: Tue, 21 Nov 2017 11:32:28 +0100 Subject: [PATCH 1/2] Support aarch64/arm64 by using dup3 instead of dup2 --- log/log.go | 15 --------------- log/log_redirect.go | 25 +++++++++++++++++++++++++ log/log_redirect_arm64.go | 23 +++++++++++++++++++++++ 3 files changed, 48 insertions(+), 15 deletions(-) create mode 100644 log/log_redirect.go create mode 100644 log/log_redirect_arm64.go diff --git a/log/log.go b/log/log.go index 4036d9d..09d2acd 100644 --- a/log/log.go +++ b/log/log.go @@ -5,7 +5,6 @@ import ( "log" "log/syslog" "os" - "syscall" ) type Logger struct { @@ -52,18 +51,4 @@ 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) - } - syscall.Dup2(int(logFile.Fd()), 1) - syscall.Dup2(int(logFile.Fd()), 2) -} diff --git a/log/log_redirect.go b/log/log_redirect.go new file mode 100644 index 0000000..d0573d1 --- /dev/null +++ b/log/log_redirect.go @@ -0,0 +1,25 @@ +// +build !arm64 + +package log + +import ( + "os" + "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 diff --git a/log/log_redirect_arm64.go b/log/log_redirect_arm64.go new file mode 100644 index 0000000..bb6a66f --- /dev/null +++ b/log/log_redirect_arm64.go @@ -0,0 +1,23 @@ +package log + +import ( + "os" + "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 From 9d3a74746fce6e2c29515a2313bf6151070ddd53 Mon Sep 17 00:00:00 2001 From: eater Date: Tue, 21 Nov 2017 13:12:13 +0100 Subject: [PATCH 2/2] use single function that does platform specific things --- log/log.go | 18 ++++++++++++++++-- log/log_redirect.go | 25 +++++-------------------- log/log_redirect_arm64.go | 25 +++++-------------------- 3 files changed, 26 insertions(+), 42 deletions(-) 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) +}