Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ func NewAzTuiState() *AzTuiState {
a.App.SetFocus(subscriptionList.List)
})

tl := resourceviews.NewTUILogView(a.AppLayout)
a.AppLayout.AppendPrimitiveView(subscriptionList.List, true, 1)
a.AppLayout.AppendPrimitiveView(tl.View, false, 3)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will shift indexes for going back functionality, could we add this view to the Grid directly instead of layout?

return &a
}

Expand Down
35 changes: 34 additions & 1 deletion src/pkg/logger/logger.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,55 @@
package logger

import (
"bytes"
"io"
"log"
"os"
"sync"
)

// Logger is the global logger instance.
var Logger *log.Logger

// buffer holds the in-memory log messages.
var buffer bytes.Buffer

// bufferMutex protects access to the buffer.
var bufferMutex sync.Mutex

// InitLogger initializes the logger to write to both a file and an in-memory buffer.
func InitLogger() error {
// Open or create the log file.
f, err := os.OpenFile("aztui.log", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
return err
}
Logger = log.New(f, "", log.LstdFlags)

// Create a MultiWriter to write to both the file and the buffer.
mw := io.MultiWriter(f, &buffer)

// Initialize the logger.
Logger = log.New(mw, "", log.LstdFlags)
Logger.Println("Starting log")

return nil
}

// Println logs a message using the global Logger.
func Println(v ...interface{}) {
Logger.Println(v...)
}

// GetLogs returns the current contents of the in-memory log buffer.
func GetLogs() string {
bufferMutex.Lock()
defer bufferMutex.Unlock()
return buffer.String()
}

// ClearLogs clears the in-memory log buffer.
func ClearLogs() {
bufferMutex.Lock()
defer bufferMutex.Unlock()
buffer.Reset()
}
32 changes: 32 additions & 0 deletions src/pkg/resourceviews/tuilog.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package resourceviews

import (
"time"
"github.com/rivo/tview"

"github.com/brendank310/aztui/pkg/layout"
"github.com/brendank310/aztui/pkg/logger"
)

type TUILogView struct {
View *tview.TextView
Parent *layout.AppLayout
}

func NewTUILogView(appLayout *layout.AppLayout) *TUILogView {
tl := TUILogView{
View: tview.NewTextView(),
Parent: appLayout,
}

tl.View.SetBorder(true)
tl.View.SetTitle("AzTUI Log")
go func(t *TUILogView) {
logs := logger.GetLogs()
t.View.Write([]byte(logs))
logger.ClearLogs()
time.Sleep(1*time.Second)
}(&tl)

return &tl
}