Skip to content

Commit

Permalink
Chore: add a new logger
Browse files Browse the repository at this point in the history
  • Loading branch information
Taranveer Bains committed Mar 7, 2024
1 parent 96ba18a commit 65f9407
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 18 deletions.
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
// these are the messages that come in as a stream from the chat gpt api
// we append the content to the viewport and scroll
case sessions.ProcessResult:
log.Println("main ProcessResult: ")
util.Log("main ProcessResult: ")
oldContent := m.sessionModel.GetMessagesAsPrettyString()
styledBufferMessage := sessions.RenderBotMessage(m.sessionModel.CurrentAnswer, m.terminalWidth/3*2)

Expand All @@ -150,7 +150,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
cmds = append(cmds, waitForActivity(m.msgChan))

case util.ErrorEvent:
log.Println("Error: ", msg.Message)
util.Log("Error: ", msg.Message)
m.sessionModel.ProcessingMode = sessions.IDLE
m.error = msg

Expand Down
10 changes: 5 additions & 5 deletions sessions/message-stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func (m *Model) CallChatGpt(resultChan chan ProcessResult) tea.Cmd {
bytes.NewBuffer(body),
)
if err != nil {
log.Println("Error creating request:", err)
util.Log("Error creating request:", err)
resultChan <- ProcessResult{ID: processResultID, Err: err}
}

Expand Down Expand Up @@ -134,7 +134,7 @@ func (m *Model) CallChatGpt(resultChan chan ProcessResult) tea.Cmd {

// This should be a constant (checking to see if the stream is done)
if line == "data: [DONE]\n" {
log.Println("Stream ended with [DONE] message.")
util.Log("Stream ended with [DONE] message.")
return ProcessResult{ID: processResultID, Err: nil, Final: true}
}

Expand All @@ -158,23 +158,23 @@ func constructJsonMessage(arrayOfProcessResult []ProcessResult) (MessageToSend,

for _, aMessage := range arrayOfProcessResult {
if aMessage.Final {
log.Println("Hit final in construct", aMessage.Result)
util.Log("Hit final in construct", aMessage.Result)
break
}

if len(aMessage.Result.Choices) > 0 {
choice := aMessage.Result.Choices[0]
// TODO: we need a helper for this
if choice.FinishReason == "stop" || choice.FinishReason == "length" {
log.Println("Hit stop or length in construct")
util.Log("Hit stop or length in construct")
break
}

if content, ok := choice.Delta["content"].(string); ok {
newMessage.Content += content
} else {
// Handle the case where the type assertion fails, e.g., log an error or return
log.Println("type assertion to string failed for choice.Delta[\"content\"]")
util.Log("type assertion to string failed for choice.Delta[\"content\"]")
formattedError := fmt.Errorf("type assertion to string failed for choice.Delta[\"content\"]")
return MessageToSend{}, formattedError
}
Expand Down
5 changes: 3 additions & 2 deletions sessions/sessions-db.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ package sessions
import (
"database/sql"
"encoding/json"
"log"

"github.com/tearingItUp786/chatgpt-tui/util"
)

type Session struct {
Expand Down Expand Up @@ -118,7 +119,7 @@ func (ss *SessionService) UpdateSessionMessages(id int, messages []MessageToSend

if err != nil {
// TODO: handle better
log.Println("I panic here")
util.Log("I panic here")
panic(err)
}
return nil
Expand Down
7 changes: 3 additions & 4 deletions sessions/sessions.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package sessions
import (
"database/sql"
"fmt"
"log"
"sort"
"strings"
"time"
Expand Down Expand Up @@ -164,7 +163,7 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {

case ProcessResult:
// add the latest message to the array of messages
log.Println("Processing message: ")
util.Log("Processing message: ")
cmd = m.handleMsgProcessing(msg)
return m, cmd

Expand Down Expand Up @@ -361,7 +360,7 @@ func (m *Model) handleFinalChoiceMessage() tea.Cmd {
m.ArrayOfProcessResult = []ProcessResult{}

if err != nil {
log.Println("Error updating session messages", err)
util.Log("Error updating session messages", err)
return m.resetStateAndCreateError(err.Error())
}

Expand Down Expand Up @@ -398,7 +397,7 @@ func (m *Model) handleMsgProcessing(msg ProcessResult) tea.Cmd {

for _, msg := range m.ArrayOfProcessResult {
if msg.Final && areIdsAllThere {
log.Println("-----Final message found-----")
util.Log("-----Final message found-----")
return m.handleFinalChoiceMessage()
}

Expand Down
2 changes: 1 addition & 1 deletion util/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func MigrateFS(db *sql.DB, migrationsFS fs.FS, dir string) error {

func checkErr(err error) {
if err != nil {
log.Println(err)
Log(err)
panic(err)
}
}
12 changes: 12 additions & 0 deletions util/logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package util

import (
"log"
"os"
)

func Log(msgs ...any) {
if os.Getenv("DEBUG") == "1" {
log.Println(msgs...)
}
}
6 changes: 2 additions & 4 deletions util/shared-events.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package util

import (
"log"

tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
)
Expand Down Expand Up @@ -45,7 +43,7 @@ func GetNewFocusMode(mode ViewMode, currentFocus FocusPane) FocusPane {
case ZenMode:
focusModes = ZenFocusModes
default:
log.Println("Invalid mode")
Log("Invalid mode")
return currentFocus
}

Expand All @@ -57,7 +55,7 @@ func GetNewFocusMode(mode ViewMode, currentFocus FocusPane) FocusPane {
}
}

log.Println("Current focus not found in mode", currentFocus)
Log("Current focus not found in mode", currentFocus)
return currentFocus
}

Expand Down

0 comments on commit 65f9407

Please sign in to comment.