Add --log option to add a logfile
ADD
logging/logging.go
+package logging
+
+import (
+ "io"
+ "log"
+ "os"
+)
+
+var (
+ defaultWriter io.Writer
+ additionalWriter io.Writer
+)
+
+type doubleLogger struct {
+ defaultLogger io.Writer
+ additionalLogger io.Writer
+}
+
+// SetAdditionalOutput defines where we log to. This wraps the writer with another
+// writer to allow a second logging location.
+func SetAdditionalOutput(newAdditionalWriter io.Writer) {
+ additionalWriter = newAdditionalWriter
+ updateLogger()
+}
+
+// SetDefaultOutput defines the default location for logoutput. This can't
+// be overriden by calling SetOutput.
+func SetDefaultOutput(newDefaultWriter io.Writer) {
+ defaultWriter = newDefaultWriter
+ updateLogger()
+
+}
+
+func updateLogger() {
+ if defaultWriter != nil && additionalWriter != nil {
+ log.SetOutput(&doubleLogger{
+ defaultLogger: defaultWriter,
+ additionalLogger: additionalWriter,
+ })
+ } else if defaultWriter != nil {
+ log.SetOutput(defaultWriter)
+ } else if additionalWriter != nil {
+ log.SetOutput(additionalWriter)
+ } else {
+ log.SetOutput(os.Stdout)
+ }
+}
+
+// Write redirects the output to both the default logger and the additional
+// logger. If any is null, it is skipped. If any errors, an error is returned.
+func (l *doubleLogger) Write(p []byte) (n int, err error) {
+ var (
+ count int
+ writeErrorDefault error
+ writeErrorAdditional error
+ )
+ if l.defaultLogger != nil {
+ count, writeErrorDefault = l.defaultLogger.Write(p)
+ }
+ if l.additionalLogger != nil {
+ count, writeErrorAdditional = l.additionalLogger.Write(p)
+ }
+
+ if writeErrorDefault != nil {
+ return 0, writeErrorDefault
+ }
+
+ if writeErrorAdditional != nil {
+ return 0, writeErrorAdditional
+ }
+
+ return count, nil
+}

MODIFY
main.go
main.go
@@ -5,9 +5,11 @@ package main
import (
"flag"
"fmt"
+ "os"
"github.com/Bios-Marcel/cordless/app"
"github.com/Bios-Marcel/cordless/config"
+ "github.com/Bios-Marcel/cordless/logging"
"github.com/Bios-Marcel/cordless/ui/shortcutdialog"
"github.com/Bios-Marcel/cordless/version"
)
@@ -19,8 +21,18 @@ func main() {
setScriptDirectory := flag.String("script-dir", "", "Sets the script directory")
setConfigFilePath := flag.String("config-file", "", "Sets exact path of the configuration file")
accountToUse := flag.String("account", "", "Defines which account cordless tries to load")
+ logPath := flag.String("log", "", "Defines what file we log to")
flag.Parse()
+ if logPath != nil {
+ logFile, openError := os.OpenFile(*logPath, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
+ if openError != nil {
+ panic(openError)
+ }
+ defer logFile.Close()
+ logging.SetDefaultOutput(logFile)
+ }
+
if setConfigDirectory != nil {
config.SetConfigDirectory(*setConfigDirectory)
}

MODIFY
ui/window.go
ui/window.go
@@ -12,6 +12,7 @@ import (
"github.com/mdp/qrterminal/v3"
"github.com/Bios-Marcel/cordless/fileopen"
+ "github.com/Bios-Marcel/cordless/logging"
"github.com/Bios-Marcel/cordless/util/files"
"github.com/Bios-Marcel/cordless/util/fuzzy"
"github.com/Bios-Marcel/cordless/util/text"
@@ -125,7 +126,7 @@ func NewWindow(doRestart chan bool, app *tview.Application, session *discordgo.S
}
window.commandView = NewCommandView(window.ExecuteCommand)
- log.SetOutput(window.commandView)
+ logging.SetAdditionalOutput(window.commandView)
for _, engine := range window.extensionEngines {
initError := window.initExtensionEngine(engine)

Add --log option to add a logfile