From 7f5a75dd94abb3aab728367c206574b948f30324 Mon Sep 17 00:00:00 2001
From: HikariKnight <2557889+HikariKnight@users.noreply.github.com>
Date: Mon, 10 Apr 2023 13:10:19 +0200
Subject: [PATCH] Start logging to file for debugging

---
 .gitignore                  |  3 ++-
 internal/configs/configs.go |  7 +++++++
 internal/logger/logger.go   | 12 ++++++++++++
 internal/ui_main.go         | 10 +++++++++-
 4 files changed, 30 insertions(+), 2 deletions(-)
 create mode 100644 internal/logger/logger.go

diff --git a/.gitignore b/.gitignore
index db11c2e..f0eea18 100644
--- a/.gitignore
+++ b/.gitignore
@@ -5,4 +5,5 @@ utils.old/
 bin/
 dist/
 main
-quickpassthrough
\ No newline at end of file
+quickpassthrough
+debug.log
\ No newline at end of file
diff --git a/internal/configs/configs.go b/internal/configs/configs.go
index 15d43dd..e2c49b3 100644
--- a/internal/configs/configs.go
+++ b/internal/configs/configs.go
@@ -6,6 +6,7 @@ import (
 	"regexp"
 
 	"github.com/HikariKnight/ls-iommu/pkg/errorcheck"
+	"github.com/HikariKnight/quickpassthrough/internal/logger"
 	"github.com/HikariKnight/quickpassthrough/pkg/fileio"
 	"github.com/HikariKnight/quickpassthrough/pkg/uname"
 	"github.com/klauspost/cpuid/v2"
@@ -83,6 +84,9 @@ func InitConfigs() {
 
 		// If the path exists
 		if fileio.FileExist(syspath) {
+			// Write to log
+			logger.Printf("%s found on the system\nCreating %s", syspath, confpath)
+
 			// Create the directories for our configs
 			err := os.MkdirAll(confpath, os.ModePerm)
 			errorcheck.ErrorCheck(err)
@@ -103,6 +107,9 @@ func InitConfigs() {
 
 		// If the file exists
 		if fileio.FileExist(sysfile) {
+			// Write to log
+			logger.Printf("%s found on the system\nCreating %s", sysfile, conffile)
+
 			// Create the directories for our configs
 			file, err := os.Create(conffile)
 			errorcheck.ErrorCheck(err)
diff --git a/internal/logger/logger.go b/internal/logger/logger.go
new file mode 100644
index 0000000..522feb7
--- /dev/null
+++ b/internal/logger/logger.go
@@ -0,0 +1,12 @@
+package logger
+
+import (
+	"fmt"
+	"log"
+)
+
+// Formats our log output to \n%s\n\n for readability
+func Printf(content string, v ...any) {
+	content = fmt.Sprintf("\n%s\n\n", content)
+	log.Printf(content, v...)
+}
diff --git a/internal/ui_main.go b/internal/ui_main.go
index a8f19c4..05410e7 100644
--- a/internal/ui_main.go
+++ b/internal/ui_main.go
@@ -4,17 +4,25 @@ package internal
 // from the Bubbles component library.
 
 import (
+	"os"
+
 	"github.com/HikariKnight/ls-iommu/pkg/errorcheck"
 	tea "github.com/charmbracelet/bubbletea"
 )
 
 // This is where we build everything
 func Tui() {
+	// Log all errors to a new logfile
+	os.Remove("debug.log")
+	logfile, err := tea.LogToFile("debug.log", "")
+	errorcheck.ErrorCheck(err, "Error creating log file")
+	defer logfile.Close()
+
 	// Make a blank model to keep our state in
 	m := NewModel()
 
 	// Start the program with the model
 	p := tea.NewProgram(m, tea.WithAltScreen())
-	_, err := p.Run()
+	_, err = p.Run()
 	errorcheck.ErrorCheck(err, "Failed to initialize UI")
 }